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)
67 /* the current implementation of the stlink limits
68 * 8bit read/writes to max 64 bytes. */
69 #define STLINK_MAX_RW8 (64)
71 /* "WAIT" responses will be retried (with exponential backoff) at
72 * most this many times before failing to caller.
74 #define MAX_WAIT_RETRIES 8
76 enum stlink_jtag_api_version {
77 STLINK_JTAG_API_V1 = 1,
82 struct stlink_usb_version {
89 /** highest supported jtag api version */
90 enum stlink_jtag_api_version jtag_api_max;
94 struct stlink_usb_handle_s {
96 struct jtag_libusb_device_handle *fd;
98 struct libusb_transfer *trans;
106 uint8_t cmdbuf[STLINK_SG_SIZE];
112 uint8_t databuf[STLINK_DATA_SIZE];
114 uint32_t max_mem_packet;
116 enum hl_transports transport;
118 struct stlink_usb_version version;
123 /** this is the currently used jtag api */
124 enum stlink_jtag_api_version jtag_api;
127 /** whether SWO tracing is enabled or not */
129 /** trace module source clock */
132 /** reconnect is needed next time we try to query the
134 bool reconnect_pending;
137 #define STLINK_SWIM_ERR_OK 0x00
138 #define STLINK_SWIM_BUSY 0x01
139 #define STLINK_DEBUG_ERR_OK 0x80
140 #define STLINK_DEBUG_ERR_FAULT 0x81
141 #define STLINK_SWD_AP_WAIT 0x10
142 #define STLINK_SWD_AP_FAULT 0x11
143 #define STLINK_SWD_AP_ERROR 0x12
144 #define STLINK_SWD_AP_PARITY_ERROR 0x13
145 #define STLINK_JTAG_WRITE_ERROR 0x0c
146 #define STLINK_JTAG_WRITE_VERIF_ERROR 0x0d
147 #define STLINK_SWD_DP_WAIT 0x14
148 #define STLINK_SWD_DP_FAULT 0x15
149 #define STLINK_SWD_DP_ERROR 0x16
150 #define STLINK_SWD_DP_PARITY_ERROR 0x17
152 #define STLINK_SWD_AP_WDATA_ERROR 0x18
153 #define STLINK_SWD_AP_STICKY_ERROR 0x19
154 #define STLINK_SWD_AP_STICKYORUN_ERROR 0x1a
156 #define STLINK_CORE_RUNNING 0x80
157 #define STLINK_CORE_HALTED 0x81
158 #define STLINK_CORE_STAT_UNKNOWN -1
160 #define STLINK_GET_VERSION 0xF1
161 #define STLINK_DEBUG_COMMAND 0xF2
162 #define STLINK_DFU_COMMAND 0xF3
163 #define STLINK_SWIM_COMMAND 0xF4
164 #define STLINK_GET_CURRENT_MODE 0xF5
165 #define STLINK_GET_TARGET_VOLTAGE 0xF7
167 #define STLINK_DEV_DFU_MODE 0x00
168 #define STLINK_DEV_MASS_MODE 0x01
169 #define STLINK_DEV_DEBUG_MODE 0x02
170 #define STLINK_DEV_SWIM_MODE 0x03
171 #define STLINK_DEV_BOOTLOADER_MODE 0x04
172 #define STLINK_DEV_UNKNOWN_MODE -1
174 #define STLINK_DFU_EXIT 0x07
177 STLINK_SWIM_ENTER_SEQ
178 1.3ms low then 750Hz then 1.5kHz
181 STM8 DM pulls reset pin low 50us
184 uint8_t (0=low|1=high)
191 send syncronization seq (16us low, response 64 clocks low)
193 #define STLINK_SWIM_ENTER 0x00
194 #define STLINK_SWIM_EXIT 0x01
195 #define STLINK_SWIM_READ_CAP 0x02
196 #define STLINK_SWIM_SPEED 0x03
197 #define STLINK_SWIM_ENTER_SEQ 0x04
198 #define STLINK_SWIM_GEN_RST 0x05
199 #define STLINK_SWIM_RESET 0x06
200 #define STLINK_SWIM_ASSERT_RESET 0x07
201 #define STLINK_SWIM_DEASSERT_RESET 0x08
202 #define STLINK_SWIM_READSTATUS 0x09
203 #define STLINK_SWIM_WRITEMEM 0x0a
204 #define STLINK_SWIM_READMEM 0x0b
205 #define STLINK_SWIM_READBUF 0x0c
207 #define STLINK_DEBUG_ENTER_JTAG 0x00
208 #define STLINK_DEBUG_GETSTATUS 0x01
209 #define STLINK_DEBUG_FORCEDEBUG 0x02
210 #define STLINK_DEBUG_APIV1_RESETSYS 0x03
211 #define STLINK_DEBUG_APIV1_READALLREGS 0x04
212 #define STLINK_DEBUG_APIV1_READREG 0x05
213 #define STLINK_DEBUG_APIV1_WRITEREG 0x06
214 #define STLINK_DEBUG_READMEM_32BIT 0x07
215 #define STLINK_DEBUG_WRITEMEM_32BIT 0x08
216 #define STLINK_DEBUG_RUNCORE 0x09
217 #define STLINK_DEBUG_STEPCORE 0x0a
218 #define STLINK_DEBUG_APIV1_SETFP 0x0b
219 #define STLINK_DEBUG_READMEM_8BIT 0x0c
220 #define STLINK_DEBUG_WRITEMEM_8BIT 0x0d
221 #define STLINK_DEBUG_APIV1_CLEARFP 0x0e
222 #define STLINK_DEBUG_APIV1_WRITEDEBUGREG 0x0f
223 #define STLINK_DEBUG_APIV1_SETWATCHPOINT 0x10
225 #define STLINK_DEBUG_ENTER_JTAG 0x00
226 #define STLINK_DEBUG_ENTER_SWD 0xa3
228 #define STLINK_DEBUG_APIV1_ENTER 0x20
229 #define STLINK_DEBUG_EXIT 0x21
230 #define STLINK_DEBUG_READCOREID 0x22
232 #define STLINK_DEBUG_APIV2_ENTER 0x30
233 #define STLINK_DEBUG_APIV2_READ_IDCODES 0x31
234 #define STLINK_DEBUG_APIV2_RESETSYS 0x32
235 #define STLINK_DEBUG_APIV2_READREG 0x33
236 #define STLINK_DEBUG_APIV2_WRITEREG 0x34
237 #define STLINK_DEBUG_APIV2_WRITEDEBUGREG 0x35
238 #define STLINK_DEBUG_APIV2_READDEBUGREG 0x36
240 #define STLINK_DEBUG_APIV2_READALLREGS 0x3A
241 #define STLINK_DEBUG_APIV2_GETLASTRWSTATUS 0x3B
242 #define STLINK_DEBUG_APIV2_DRIVE_NRST 0x3C
244 #define STLINK_DEBUG_APIV2_START_TRACE_RX 0x40
245 #define STLINK_DEBUG_APIV2_STOP_TRACE_RX 0x41
246 #define STLINK_DEBUG_APIV2_GET_TRACE_NB 0x42
247 #define STLINK_DEBUG_APIV2_SWD_SET_FREQ 0x43
249 #define STLINK_DEBUG_APIV2_DRIVE_NRST_LOW 0x00
250 #define STLINK_DEBUG_APIV2_DRIVE_NRST_HIGH 0x01
251 #define STLINK_DEBUG_APIV2_DRIVE_NRST_PULSE 0x02
253 #define STLINK_TRACE_SIZE 4096
254 #define STLINK_TRACE_MAX_HZ 2000000
255 #define STLINK_TRACE_MIN_VERSION 13
259 STLINK_MODE_UNKNOWN = 0,
262 STLINK_MODE_DEBUG_JTAG,
263 STLINK_MODE_DEBUG_SWD,
264 STLINK_MODE_DEBUG_SWIM
267 #define REQUEST_SENSE 0x03
268 #define REQUEST_SENSE_LENGTH 18
270 static const struct {
273 } stlink_khz_to_speed_map[] = {
275 {1800, 1}, /* default */
288 static void stlink_usb_init_buffer(void *handle, uint8_t direction, uint32_t size);
289 static int stlink_swim_status(void *handle);
292 static int stlink_usb_xfer_v1_get_status(void *handle)
294 struct stlink_usb_handle_s *h = handle;
296 assert(handle != NULL);
299 memset(h->cmdbuf, 0, STLINK_SG_SIZE);
301 if (jtag_libusb_bulk_read(h->fd, h->rx_ep, (char *)h->cmdbuf,
302 13, STLINK_READ_TIMEOUT) != 13)
307 t1 = buf_get_u32(h->cmdbuf, 0, 32);
310 if (t1 != 0x53425355)
318 if (h->cmdbuf[12] != 0)
325 static int stlink_usb_xfer_rw(void *handle, int cmdsize, const uint8_t *buf, int size)
327 struct stlink_usb_handle_s *h = handle;
329 assert(handle != NULL);
331 if (jtag_libusb_bulk_write(h->fd, h->tx_ep, (char *)h->cmdbuf, cmdsize,
332 STLINK_WRITE_TIMEOUT) != cmdsize) {
336 if (h->direction == h->tx_ep && size) {
337 if (jtag_libusb_bulk_write(h->fd, h->tx_ep, (char *)buf,
338 size, STLINK_WRITE_TIMEOUT) != size) {
339 LOG_DEBUG("bulk write failed");
342 } else if (h->direction == h->rx_ep && size) {
343 if (jtag_libusb_bulk_read(h->fd, h->rx_ep, (char *)buf,
344 size, STLINK_READ_TIMEOUT) != size) {
345 LOG_DEBUG("bulk read failed");
354 static int stlink_usb_xfer_v1_get_sense(void *handle)
357 struct stlink_usb_handle_s *h = handle;
359 assert(handle != NULL);
361 stlink_usb_init_buffer(handle, h->rx_ep, 16);
363 h->cmdbuf[h->cmdidx++] = REQUEST_SENSE;
364 h->cmdbuf[h->cmdidx++] = 0;
365 h->cmdbuf[h->cmdidx++] = 0;
366 h->cmdbuf[h->cmdidx++] = 0;
367 h->cmdbuf[h->cmdidx++] = REQUEST_SENSE_LENGTH;
369 res = stlink_usb_xfer_rw(handle, REQUEST_SENSE_LENGTH, h->databuf, 16);
374 if (stlink_usb_xfer_v1_get_status(handle) != ERROR_OK)
381 transfers block in cmdbuf
382 <size> indicates number of bytes in the following
385 static int stlink_usb_xfer(void *handle, const uint8_t *buf, int size)
387 int err, cmdsize = STLINK_CMD_SIZE_V2;
388 struct stlink_usb_handle_s *h = handle;
390 assert(handle != NULL);
392 if (h->version.stlink == 1) {
393 cmdsize = STLINK_SG_SIZE;
394 /* put length in bCBWCBLength */
395 h->cmdbuf[14] = h->cmdidx-15;
398 err = stlink_usb_xfer_rw(handle, cmdsize, buf, size);
403 if (h->version.stlink == 1) {
404 if (stlink_usb_xfer_v1_get_status(handle) != ERROR_OK) {
405 /* check csw status */
406 if (h->cmdbuf[12] == 1) {
407 LOG_DEBUG("get sense");
408 if (stlink_usb_xfer_v1_get_sense(handle) != ERROR_OK)
419 Converts an STLINK status code held in the first byte of a response
420 to an openocd error, logs any error/wait status as debug output.
422 static int stlink_usb_error_check(void *handle)
424 struct stlink_usb_handle_s *h = handle;
426 assert(handle != NULL);
428 if (h->transport == HL_TRANSPORT_SWIM) {
429 switch (h->databuf[0]) {
430 case STLINK_SWIM_ERR_OK:
432 case STLINK_SWIM_BUSY:
435 LOG_DEBUG("unknown/unexpected STLINK status code 0x%x", h->databuf[0]);
440 /* TODO: no error checking yet on api V1 */
441 if (h->jtag_api == STLINK_JTAG_API_V1)
442 h->databuf[0] = STLINK_DEBUG_ERR_OK;
444 switch (h->databuf[0]) {
445 case STLINK_DEBUG_ERR_OK:
447 case STLINK_DEBUG_ERR_FAULT:
448 LOG_DEBUG("SWD fault response (0x%x)", STLINK_DEBUG_ERR_FAULT);
450 case STLINK_SWD_AP_WAIT:
451 LOG_DEBUG("wait status SWD_AP_WAIT (0x%x)", STLINK_SWD_AP_WAIT);
453 case STLINK_SWD_DP_WAIT:
454 LOG_DEBUG("wait status SWD_DP_WAIT (0x%x)", STLINK_SWD_DP_WAIT);
456 case STLINK_JTAG_WRITE_ERROR:
457 LOG_DEBUG("Write error");
459 case STLINK_JTAG_WRITE_VERIF_ERROR:
460 LOG_DEBUG("Write verify error, ignoring");
462 case STLINK_SWD_AP_FAULT:
463 /* git://git.ac6.fr/openocd commit 657e3e885b9ee10
464 * returns ERROR_OK with the comment:
465 * Change in error status when reading outside RAM.
466 * This fix allows CDT plugin to visualize memory.
468 LOG_DEBUG("STLINK_SWD_AP_FAULT");
470 case STLINK_SWD_AP_ERROR:
471 LOG_DEBUG("STLINK_SWD_AP_ERROR");
473 case STLINK_SWD_AP_PARITY_ERROR:
474 LOG_DEBUG("STLINK_SWD_AP_PARITY_ERROR");
476 case STLINK_SWD_DP_FAULT:
477 LOG_DEBUG("STLINK_SWD_DP_FAULT");
479 case STLINK_SWD_DP_ERROR:
480 LOG_DEBUG("STLINK_SWD_DP_ERROR");
482 case STLINK_SWD_DP_PARITY_ERROR:
483 LOG_DEBUG("STLINK_SWD_DP_PARITY_ERROR");
485 case STLINK_SWD_AP_WDATA_ERROR:
486 LOG_DEBUG("STLINK_SWD_AP_WDATA_ERROR");
488 case STLINK_SWD_AP_STICKY_ERROR:
489 LOG_DEBUG("STLINK_SWD_AP_STICKY_ERROR");
491 case STLINK_SWD_AP_STICKYORUN_ERROR:
492 LOG_DEBUG("STLINK_SWD_AP_STICKYORUN_ERROR");
495 LOG_DEBUG("unknown/unexpected STLINK status code 0x%x", h->databuf[0]);
501 /** Issue an STLINK command via USB transfer, with retries on any wait status responses.
503 Works for commands where the STLINK_DEBUG status is returned in the first
504 byte of the response packet. For SWIM a SWIM_READSTATUS is requested instead.
506 Returns an openocd result code.
508 static int stlink_cmd_allow_retry(void *handle, const uint8_t *buf, int size)
512 struct stlink_usb_handle_s *h = handle;
515 if ((h->transport != HL_TRANSPORT_SWIM) || !retries) {
516 res = stlink_usb_xfer(handle, buf, size);
521 if (h->transport == HL_TRANSPORT_SWIM) {
522 res = stlink_swim_status(handle);
527 res = stlink_usb_error_check(handle);
528 if (res == ERROR_WAIT && retries < MAX_WAIT_RETRIES) {
529 usleep((1<<retries++) * 1000);
537 static int stlink_usb_read_trace(void *handle, const uint8_t *buf, int size)
539 struct stlink_usb_handle_s *h = handle;
541 assert(handle != NULL);
543 assert(h->version.stlink >= 2);
545 if (jtag_libusb_bulk_read(h->fd, h->trace_ep, (char *)buf,
546 size, STLINK_READ_TIMEOUT) != size) {
547 LOG_ERROR("bulk trace read failed");
555 this function writes transfer length in
556 the right place in the cb
558 static void stlink_usb_set_cbw_transfer_datalength(void *handle, uint32_t size)
560 struct stlink_usb_handle_s *h = handle;
562 buf_set_u32(h->cmdbuf+8, 0, 32, size);
565 static void stlink_usb_xfer_v1_create_cmd(void *handle, uint8_t direction, uint32_t size)
567 struct stlink_usb_handle_s *h = handle;
569 /* fill the send buffer */
570 strcpy((char *)h->cmdbuf, "USBC");
572 /* csw tag not used */
573 buf_set_u32(h->cmdbuf+h->cmdidx, 0, 32, 0);
575 /* cbw data transfer length (in the following data phase in or out) */
576 buf_set_u32(h->cmdbuf+h->cmdidx, 0, 32, size);
579 h->cmdbuf[h->cmdidx++] = (direction == h->rx_ep ? ENDPOINT_IN : ENDPOINT_OUT);
580 h->cmdbuf[h->cmdidx++] = 0; /* lun */
581 /* cdb clength (is filled in at xfer) */
582 h->cmdbuf[h->cmdidx++] = 0;
586 static void stlink_usb_init_buffer(void *handle, uint8_t direction, uint32_t size)
588 struct stlink_usb_handle_s *h = handle;
590 h->direction = direction;
594 memset(h->cmdbuf, 0, STLINK_SG_SIZE);
595 memset(h->databuf, 0, STLINK_DATA_SIZE);
597 if (h->version.stlink == 1)
598 stlink_usb_xfer_v1_create_cmd(handle, direction, size);
602 static int stlink_usb_version(void *handle)
606 struct stlink_usb_handle_s *h = handle;
608 assert(handle != NULL);
610 stlink_usb_init_buffer(handle, h->rx_ep, 6);
612 h->cmdbuf[h->cmdidx++] = STLINK_GET_VERSION;
614 res = stlink_usb_xfer(handle, h->databuf, 6);
619 v = (h->databuf[0] << 8) | h->databuf[1];
621 h->version.stlink = (v >> 12) & 0x0f;
622 h->version.jtag = (v >> 6) & 0x3f;
623 h->version.swim = v & 0x3f;
624 h->vid = buf_get_u32(h->databuf, 16, 16);
625 h->pid = buf_get_u32(h->databuf, 32, 16);
627 /* set the supported jtag api version
628 * API V2 is supported since JTAG V11
630 if (h->version.jtag >= 11)
631 h->version.jtag_api_max = STLINK_JTAG_API_V2;
633 h->version.jtag_api_max = STLINK_JTAG_API_V1;
635 LOG_INFO("STLINK v%d JTAG v%d API v%d SWIM v%d VID 0x%04X PID 0x%04X",
638 (h->version.jtag_api_max == STLINK_JTAG_API_V1) ? 1 : 2,
646 static int stlink_usb_check_voltage(void *handle, float *target_voltage)
648 struct stlink_usb_handle_s *h = handle;
649 uint32_t adc_results[2];
651 /* only supported by stlink/v2 and for firmware >= 13 */
652 if (h->version.stlink == 1 || h->version.jtag < 13)
653 return ERROR_COMMAND_NOTFOUND;
655 stlink_usb_init_buffer(handle, h->rx_ep, 8);
657 h->cmdbuf[h->cmdidx++] = STLINK_GET_TARGET_VOLTAGE;
659 int result = stlink_usb_xfer(handle, h->databuf, 8);
661 if (result != ERROR_OK)
665 adc_results[0] = le_to_h_u32(h->databuf);
666 adc_results[1] = le_to_h_u32(h->databuf + 4);
671 *target_voltage = 2 * ((float)adc_results[1]) * (float)(1.2 / adc_results[0]);
673 LOG_INFO("Target voltage: %f", (double)*target_voltage);
678 static int stlink_usb_set_swdclk(void *handle, uint16_t clk_divisor)
680 struct stlink_usb_handle_s *h = handle;
682 assert(handle != NULL);
684 /* only supported by stlink/v2 and for firmware >= 22 */
685 if (h->version.stlink == 1 || h->version.jtag < 22)
686 return ERROR_COMMAND_NOTFOUND;
688 stlink_usb_init_buffer(handle, h->rx_ep, 2);
690 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
691 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_SWD_SET_FREQ;
692 h_u16_to_le(h->cmdbuf+h->cmdidx, clk_divisor);
695 int result = stlink_cmd_allow_retry(handle, h->databuf, 2);
697 if (result != ERROR_OK)
704 static int stlink_usb_current_mode(void *handle, uint8_t *mode)
707 struct stlink_usb_handle_s *h = handle;
709 assert(handle != NULL);
711 stlink_usb_init_buffer(handle, h->rx_ep, 2);
713 h->cmdbuf[h->cmdidx++] = STLINK_GET_CURRENT_MODE;
715 res = stlink_usb_xfer(handle, h->databuf, 2);
720 *mode = h->databuf[0];
726 static int stlink_usb_mode_enter(void *handle, enum stlink_mode type)
729 struct stlink_usb_handle_s *h = handle;
731 assert(handle != NULL);
733 /* on api V2 we are able the read the latest command
735 * TODO: we need the test on api V1 too
737 if (h->jtag_api == STLINK_JTAG_API_V2)
740 stlink_usb_init_buffer(handle, h->rx_ep, rx_size);
743 case STLINK_MODE_DEBUG_JTAG:
744 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
745 if (h->jtag_api == STLINK_JTAG_API_V1)
746 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_ENTER;
748 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_ENTER;
749 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_ENTER_JTAG;
751 case STLINK_MODE_DEBUG_SWD:
752 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
753 if (h->jtag_api == STLINK_JTAG_API_V1)
754 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_ENTER;
756 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_ENTER;
757 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_ENTER_SWD;
759 case STLINK_MODE_DEBUG_SWIM:
760 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_COMMAND;
761 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_ENTER;
762 /* no answer for this function... */
765 case STLINK_MODE_DFU:
766 case STLINK_MODE_MASS:
771 return stlink_cmd_allow_retry(handle, h->databuf, rx_size);
775 static int stlink_usb_mode_leave(void *handle, enum stlink_mode type)
778 struct stlink_usb_handle_s *h = handle;
780 assert(handle != NULL);
782 stlink_usb_init_buffer(handle, STLINK_NULL_EP, 0);
785 case STLINK_MODE_DEBUG_JTAG:
786 case STLINK_MODE_DEBUG_SWD:
787 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
788 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_EXIT;
790 case STLINK_MODE_DEBUG_SWIM:
791 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_COMMAND;
792 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_EXIT;
794 case STLINK_MODE_DFU:
795 h->cmdbuf[h->cmdidx++] = STLINK_DFU_COMMAND;
796 h->cmdbuf[h->cmdidx++] = STLINK_DFU_EXIT;
798 case STLINK_MODE_MASS:
803 res = stlink_usb_xfer(handle, 0, 0);
811 static int stlink_usb_assert_srst(void *handle, int srst);
813 static enum stlink_mode stlink_get_mode(enum hl_transports t)
816 case HL_TRANSPORT_SWD:
817 return STLINK_MODE_DEBUG_SWD;
818 case HL_TRANSPORT_JTAG:
819 return STLINK_MODE_DEBUG_JTAG;
820 case HL_TRANSPORT_SWIM:
821 return STLINK_MODE_DEBUG_SWIM;
823 return STLINK_MODE_UNKNOWN;
828 static int stlink_usb_init_mode(void *handle, bool connect_under_reset)
832 enum stlink_mode emode;
833 struct stlink_usb_handle_s *h = handle;
835 assert(handle != NULL);
837 res = stlink_usb_current_mode(handle, &mode);
842 LOG_DEBUG("MODE: 0x%02X", mode);
844 /* try to exit current mode */
846 case STLINK_DEV_DFU_MODE:
847 emode = STLINK_MODE_DFU;
849 case STLINK_DEV_DEBUG_MODE:
850 emode = STLINK_MODE_DEBUG_SWD;
852 case STLINK_DEV_SWIM_MODE:
853 emode = STLINK_MODE_DEBUG_SWIM;
855 case STLINK_DEV_BOOTLOADER_MODE:
856 case STLINK_DEV_MASS_MODE:
858 emode = STLINK_MODE_UNKNOWN;
862 if (emode != STLINK_MODE_UNKNOWN) {
863 res = stlink_usb_mode_leave(handle, emode);
869 res = stlink_usb_current_mode(handle, &mode);
874 /* we check the target voltage here as an aid to debugging connection problems.
875 * the stlink requires the target Vdd to be connected for reliable debugging.
876 * this cmd is supported in all modes except DFU
878 if (mode != STLINK_DEV_DFU_MODE) {
880 float target_voltage;
882 /* check target voltage (if supported) */
883 res = stlink_usb_check_voltage(h, &target_voltage);
885 if (res != ERROR_OK) {
886 if (res != ERROR_COMMAND_NOTFOUND)
887 LOG_ERROR("voltage check failed");
888 /* attempt to continue as it is not a catastrophic failure */
890 /* check for a sensible target voltage, operating range is 1.65-5.5v
891 * according to datasheet */
892 if (target_voltage < 1.5)
893 LOG_ERROR("target voltage may be too low for reliable debugging");
897 LOG_DEBUG("MODE: 0x%02X", mode);
899 /* set selected mode */
900 emode = stlink_get_mode(h->transport);
902 if (emode == STLINK_MODE_UNKNOWN) {
903 LOG_ERROR("selected mode (transport) not supported");
907 /* preliminary SRST assert:
908 * We want SRST is asserted before activating debug signals (mode_enter).
909 * As the required mode has not been set, the adapter may not know what pin to use.
910 * Tested firmware STLINK v2 JTAG v29 API v2 SWIM v0 uses T_NRST pin by default
911 * Tested firmware STLINK v2 JTAG v27 API v2 SWIM v6 uses T_NRST pin by default
912 * after power on, SWIM_RST stays unchanged */
913 if (connect_under_reset && emode != STLINK_MODE_DEBUG_SWIM)
914 stlink_usb_assert_srst(handle, 0);
915 /* do not check the return status here, we will
916 proceed and enter the desired mode below
917 and try asserting srst again. */
919 res = stlink_usb_mode_enter(handle, emode);
923 /* assert SRST again: a little bit late but now the adapter knows for sure what pin to use */
924 if (connect_under_reset) {
925 res = stlink_usb_assert_srst(handle, 0);
930 res = stlink_usb_current_mode(handle, &mode);
935 LOG_DEBUG("MODE: 0x%02X", mode);
940 /* request status from last swim request */
941 static int stlink_swim_status(void *handle)
943 struct stlink_usb_handle_s *h = handle;
946 stlink_usb_init_buffer(handle, h->rx_ep, 4);
947 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_COMMAND;
948 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_READSTATUS;
949 res = stlink_usb_xfer(handle, h->databuf, 4);
955 the purpose of this function is unknown...
956 capabilites? anyway for swim v6 it returns
959 __attribute__((unused))
960 static int stlink_swim_cap(void *handle, uint8_t *cap)
962 struct stlink_usb_handle_s *h = handle;
965 stlink_usb_init_buffer(handle, h->rx_ep, 8);
966 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_COMMAND;
967 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_READ_CAP;
968 h->cmdbuf[h->cmdidx++] = 0x01;
969 res = stlink_usb_xfer(handle, h->databuf, 8);
972 memcpy(cap, h->databuf, 8);
976 /* debug dongle assert/deassert sreset line */
977 static int stlink_swim_assert_reset(void *handle, int reset)
979 struct stlink_usb_handle_s *h = handle;
982 stlink_usb_init_buffer(handle, h->rx_ep, 0);
983 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_COMMAND;
985 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_ASSERT_RESET;
987 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_DEASSERT_RESET;
988 res = stlink_cmd_allow_retry(handle, h->databuf, 0);
996 1.3ms low then 750Hz then 1.5kHz
998 static int stlink_swim_enter(void *handle)
1000 struct stlink_usb_handle_s *h = handle;
1003 stlink_usb_init_buffer(handle, h->rx_ep, 0);
1004 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_COMMAND;
1005 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_ENTER_SEQ;
1006 res = stlink_cmd_allow_retry(handle, h->databuf, 0);
1007 if (res != ERROR_OK)
1012 /* switch high/low speed swim */
1013 static int stlink_swim_speed(void *handle, int speed)
1015 struct stlink_usb_handle_s *h = handle;
1018 stlink_usb_init_buffer(handle, h->rx_ep, 0);
1019 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_COMMAND;
1020 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_SPEED;
1022 h->cmdbuf[h->cmdidx++] = 1;
1024 h->cmdbuf[h->cmdidx++] = 0;
1025 res = stlink_cmd_allow_retry(handle, h->databuf, 0);
1026 if (res != ERROR_OK)
1032 initiate srst from swim.
1033 nrst is pulled low for 50us.
1035 static int stlink_swim_generate_rst(void *handle)
1037 struct stlink_usb_handle_s *h = handle;
1040 stlink_usb_init_buffer(handle, h->rx_ep, 0);
1041 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_COMMAND;
1042 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_GEN_RST;
1043 res = stlink_cmd_allow_retry(handle, h->databuf, 0);
1044 if (res != ERROR_OK)
1050 send resyncronize sequence
1051 swim is pulled low for 16us
1052 reply is 64 clks low
1054 static int stlink_swim_resync(void *handle)
1056 struct stlink_usb_handle_s *h = handle;
1059 stlink_usb_init_buffer(handle, h->rx_ep, 0);
1060 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_COMMAND;
1061 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_RESET;
1062 res = stlink_cmd_allow_retry(handle, h->databuf, 0);
1063 if (res != ERROR_OK)
1068 static int stlink_swim_writebytes(void *handle, uint32_t addr, uint32_t len, const uint8_t *data)
1070 struct stlink_usb_handle_s *h = handle;
1073 unsigned int datalen = 0;
1074 int cmdsize = STLINK_CMD_SIZE_V2;
1076 if (len > STLINK_DATA_SIZE)
1079 if (h->version.stlink == 1)
1080 cmdsize = STLINK_SG_SIZE;
1082 stlink_usb_init_buffer(handle, h->tx_ep, 0);
1083 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_COMMAND;
1084 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_WRITEMEM;
1085 h_u16_to_be(h->cmdbuf+h->cmdidx, len);
1087 h_u32_to_be(h->cmdbuf+h->cmdidx, addr);
1089 for (i = 0; i < len; i++) {
1090 if (h->cmdidx == cmdsize)
1091 h->databuf[datalen++] = *(data++);
1093 h->cmdbuf[h->cmdidx++] = *(data++);
1095 if (h->version.stlink == 1)
1096 stlink_usb_set_cbw_transfer_datalength(handle, datalen);
1098 res = stlink_cmd_allow_retry(handle, h->databuf, datalen);
1099 if (res != ERROR_OK)
1104 static int stlink_swim_readbytes(void *handle, uint32_t addr, uint32_t len, uint8_t *data)
1106 struct stlink_usb_handle_s *h = handle;
1109 if (len > STLINK_DATA_SIZE)
1112 stlink_usb_init_buffer(handle, h->rx_ep, 0);
1113 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_COMMAND;
1114 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_READMEM;
1115 h_u16_to_be(h->cmdbuf+h->cmdidx, len);
1117 h_u32_to_be(h->cmdbuf+h->cmdidx, addr);
1119 res = stlink_cmd_allow_retry(handle, h->databuf, 0);
1120 if (res != ERROR_OK)
1123 stlink_usb_init_buffer(handle, h->rx_ep, len);
1124 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_COMMAND;
1125 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_READBUF;
1126 res = stlink_usb_xfer(handle, data, len);
1127 if (res != ERROR_OK)
1134 static int stlink_usb_idcode(void *handle, uint32_t *idcode)
1137 struct stlink_usb_handle_s *h = handle;
1139 assert(handle != NULL);
1141 /* there is no swim read core id cmd */
1142 if (h->transport == HL_TRANSPORT_SWIM) {
1147 stlink_usb_init_buffer(handle, h->rx_ep, 4);
1149 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1150 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_READCOREID;
1152 res = stlink_usb_xfer(handle, h->databuf, 4);
1154 if (res != ERROR_OK)
1157 *idcode = le_to_h_u32(h->databuf);
1159 LOG_DEBUG("IDCODE: 0x%08" PRIX32, *idcode);
1164 static int stlink_usb_v2_read_debug_reg(void *handle, uint32_t addr, uint32_t *val)
1166 struct stlink_usb_handle_s *h = handle;
1169 assert(handle != NULL);
1171 stlink_usb_init_buffer(handle, h->rx_ep, 8);
1173 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1174 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_READDEBUGREG;
1175 h_u32_to_le(h->cmdbuf+h->cmdidx, addr);
1178 res = stlink_cmd_allow_retry(handle, h->databuf, 8);
1179 if (res != ERROR_OK)
1182 *val = le_to_h_u32(h->databuf + 4);
1186 static int stlink_usb_write_debug_reg(void *handle, uint32_t addr, uint32_t val)
1188 struct stlink_usb_handle_s *h = handle;
1190 assert(handle != NULL);
1192 stlink_usb_init_buffer(handle, h->rx_ep, 2);
1194 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1195 if (h->jtag_api == STLINK_JTAG_API_V1)
1196 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_WRITEDEBUGREG;
1198 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_WRITEDEBUGREG;
1199 h_u32_to_le(h->cmdbuf+h->cmdidx, addr);
1201 h_u32_to_le(h->cmdbuf+h->cmdidx, val);
1204 return stlink_cmd_allow_retry(handle, h->databuf, 2);
1208 static int stlink_usb_trace_read(void *handle, uint8_t *buf, size_t *size)
1210 struct stlink_usb_handle_s *h = handle;
1212 assert(handle != NULL);
1214 if (h->trace.enabled && h->version.jtag >= STLINK_TRACE_MIN_VERSION) {
1217 stlink_usb_init_buffer(handle, h->rx_ep, 10);
1219 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1220 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_GET_TRACE_NB;
1222 res = stlink_usb_xfer(handle, h->databuf, 2);
1223 if (res != ERROR_OK)
1226 size_t bytes_avail = le_to_h_u16(h->databuf);
1227 *size = bytes_avail < *size ? bytes_avail : *size - 1;
1230 res = stlink_usb_read_trace(handle, buf, *size);
1231 if (res != ERROR_OK)
1240 static enum target_state stlink_usb_v2_get_status(void *handle)
1245 result = stlink_usb_v2_read_debug_reg(handle, DCB_DHCSR, &status);
1246 if (result != ERROR_OK)
1247 return TARGET_UNKNOWN;
1249 if (status & S_HALT)
1250 return TARGET_HALTED;
1251 else if (status & S_RESET_ST)
1252 return TARGET_RESET;
1254 return TARGET_RUNNING;
1258 static enum target_state stlink_usb_state(void *handle)
1261 struct stlink_usb_handle_s *h = handle;
1263 assert(handle != NULL);
1265 if (h->transport == HL_TRANSPORT_SWIM) {
1266 res = stlink_usb_mode_enter(handle, stlink_get_mode(h->transport));
1267 if (res != ERROR_OK)
1268 return TARGET_UNKNOWN;
1270 res = stlink_swim_resync(handle);
1271 if (res != ERROR_OK)
1272 return TARGET_UNKNOWN;
1277 if (h->reconnect_pending) {
1278 LOG_INFO("Previous state query failed, trying to reconnect");
1279 res = stlink_usb_mode_enter(handle, stlink_get_mode(h->transport));
1281 if (res != ERROR_OK)
1282 return TARGET_UNKNOWN;
1284 h->reconnect_pending = false;
1287 if (h->jtag_api == STLINK_JTAG_API_V2) {
1288 res = stlink_usb_v2_get_status(handle);
1289 if (res == TARGET_UNKNOWN)
1290 h->reconnect_pending = true;
1294 stlink_usb_init_buffer(handle, h->rx_ep, 2);
1296 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1297 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_GETSTATUS;
1299 res = stlink_usb_xfer(handle, h->databuf, 2);
1301 if (res != ERROR_OK)
1302 return TARGET_UNKNOWN;
1304 if (h->databuf[0] == STLINK_CORE_RUNNING)
1305 return TARGET_RUNNING;
1306 if (h->databuf[0] == STLINK_CORE_HALTED)
1307 return TARGET_HALTED;
1309 h->reconnect_pending = true;
1311 return TARGET_UNKNOWN;
1314 static int stlink_usb_assert_srst(void *handle, int srst)
1316 struct stlink_usb_handle_s *h = handle;
1318 assert(handle != NULL);
1320 if (h->transport == HL_TRANSPORT_SWIM)
1321 return stlink_swim_assert_reset(handle, srst);
1323 if (h->version.stlink == 1)
1324 return ERROR_COMMAND_NOTFOUND;
1326 stlink_usb_init_buffer(handle, h->rx_ep, 2);
1328 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1329 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_DRIVE_NRST;
1330 h->cmdbuf[h->cmdidx++] = srst;
1332 return stlink_cmd_allow_retry(handle, h->databuf, 2);
1336 static void stlink_usb_trace_disable(void *handle)
1339 struct stlink_usb_handle_s *h = handle;
1341 assert(handle != NULL);
1343 assert(h->version.jtag >= STLINK_TRACE_MIN_VERSION);
1345 LOG_DEBUG("Tracing: disable");
1347 stlink_usb_init_buffer(handle, h->rx_ep, 2);
1348 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1349 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_STOP_TRACE_RX;
1350 res = stlink_usb_xfer(handle, h->databuf, 2);
1352 if (res == ERROR_OK)
1353 h->trace.enabled = false;
1358 static int stlink_usb_trace_enable(void *handle)
1361 struct stlink_usb_handle_s *h = handle;
1363 assert(handle != NULL);
1365 if (h->version.jtag >= STLINK_TRACE_MIN_VERSION) {
1366 stlink_usb_init_buffer(handle, h->rx_ep, 10);
1368 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1369 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_START_TRACE_RX;
1370 h_u16_to_le(h->cmdbuf+h->cmdidx, (uint16_t)STLINK_TRACE_SIZE);
1372 h_u32_to_le(h->cmdbuf+h->cmdidx, h->trace.source_hz);
1375 res = stlink_usb_xfer(handle, h->databuf, 2);
1377 if (res == ERROR_OK) {
1378 h->trace.enabled = true;
1379 LOG_DEBUG("Tracing: recording at %" PRIu32 "Hz", h->trace.source_hz);
1382 LOG_ERROR("Tracing is not supported by this version.");
1390 static int stlink_usb_reset(void *handle)
1392 struct stlink_usb_handle_s *h = handle;
1395 assert(handle != NULL);
1397 if (h->transport == HL_TRANSPORT_SWIM)
1398 return stlink_swim_generate_rst(handle);
1400 stlink_usb_init_buffer(handle, h->rx_ep, 2);
1402 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1404 if (h->jtag_api == STLINK_JTAG_API_V1)
1405 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_RESETSYS;
1407 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_RESETSYS;
1409 retval = stlink_cmd_allow_retry(handle, h->databuf, 2);
1410 if (retval != ERROR_OK)
1413 if (h->trace.enabled) {
1414 stlink_usb_trace_disable(h);
1415 return stlink_usb_trace_enable(h);
1422 static int stlink_usb_run(void *handle)
1425 struct stlink_usb_handle_s *h = handle;
1427 assert(handle != NULL);
1429 if (h->jtag_api == STLINK_JTAG_API_V2) {
1430 res = stlink_usb_write_debug_reg(handle, DCB_DHCSR, DBGKEY|C_DEBUGEN);
1435 stlink_usb_init_buffer(handle, h->rx_ep, 2);
1437 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1438 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_RUNCORE;
1440 return stlink_cmd_allow_retry(handle, h->databuf, 2);
1444 static int stlink_usb_halt(void *handle)
1447 struct stlink_usb_handle_s *h = handle;
1449 assert(handle != NULL);
1451 if (h->jtag_api == STLINK_JTAG_API_V2) {
1452 res = stlink_usb_write_debug_reg(handle, DCB_DHCSR, DBGKEY|C_HALT|C_DEBUGEN);
1457 stlink_usb_init_buffer(handle, h->rx_ep, 2);
1459 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1460 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_FORCEDEBUG;
1462 return stlink_cmd_allow_retry(handle, h->databuf, 2);
1466 static int stlink_usb_step(void *handle)
1468 struct stlink_usb_handle_s *h = handle;
1470 assert(handle != NULL);
1472 if (h->jtag_api == STLINK_JTAG_API_V2) {
1473 /* TODO: this emulates the v1 api, it should really use a similar auto mask isr
1474 * that the Cortex-M3 currently does. */
1475 stlink_usb_write_debug_reg(handle, DCB_DHCSR, DBGKEY|C_HALT|C_MASKINTS|C_DEBUGEN);
1476 stlink_usb_write_debug_reg(handle, DCB_DHCSR, DBGKEY|C_STEP|C_MASKINTS|C_DEBUGEN);
1477 return stlink_usb_write_debug_reg(handle, DCB_DHCSR, DBGKEY|C_HALT|C_DEBUGEN);
1480 stlink_usb_init_buffer(handle, h->rx_ep, 2);
1482 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1483 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_STEPCORE;
1485 return stlink_cmd_allow_retry(handle, h->databuf, 2);
1489 static int stlink_usb_read_regs(void *handle)
1492 struct stlink_usb_handle_s *h = handle;
1494 assert(handle != NULL);
1496 stlink_usb_init_buffer(handle, h->rx_ep, 84);
1498 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1499 if (h->jtag_api == STLINK_JTAG_API_V1)
1500 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_READALLREGS;
1502 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_READALLREGS;
1504 res = stlink_usb_xfer(handle, h->databuf, 84);
1506 if (res != ERROR_OK)
1513 static int stlink_usb_read_reg(void *handle, int num, uint32_t *val)
1516 struct stlink_usb_handle_s *h = handle;
1518 assert(handle != NULL);
1520 stlink_usb_init_buffer(handle, h->rx_ep, h->jtag_api == STLINK_JTAG_API_V1 ? 4 : 8);
1522 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1523 if (h->jtag_api == STLINK_JTAG_API_V1)
1524 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_READREG;
1526 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_READREG;
1527 h->cmdbuf[h->cmdidx++] = num;
1529 if (h->jtag_api == STLINK_JTAG_API_V1) {
1530 res = stlink_usb_xfer(handle, h->databuf, 4);
1531 if (res != ERROR_OK)
1533 *val = le_to_h_u32(h->databuf);
1536 res = stlink_cmd_allow_retry(handle, h->databuf, 8);
1537 if (res != ERROR_OK)
1539 *val = le_to_h_u32(h->databuf + 4);
1545 static int stlink_usb_write_reg(void *handle, int num, uint32_t val)
1547 struct stlink_usb_handle_s *h = handle;
1549 assert(handle != NULL);
1551 stlink_usb_init_buffer(handle, h->rx_ep, 2);
1553 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1554 if (h->jtag_api == STLINK_JTAG_API_V1)
1555 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_WRITEREG;
1557 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_WRITEREG;
1558 h->cmdbuf[h->cmdidx++] = num;
1559 h_u32_to_le(h->cmdbuf+h->cmdidx, val);
1562 return stlink_cmd_allow_retry(handle, h->databuf, 2);
1565 static int stlink_usb_get_rw_status(void *handle)
1568 struct stlink_usb_handle_s *h = handle;
1570 assert(handle != NULL);
1572 if (h->jtag_api == STLINK_JTAG_API_V1)
1575 stlink_usb_init_buffer(handle, h->rx_ep, 2);
1577 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1578 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_GETLASTRWSTATUS;
1580 res = stlink_usb_xfer(handle, h->databuf, 2);
1582 if (res != ERROR_OK)
1585 return stlink_usb_error_check(h);
1589 static int stlink_usb_read_mem8(void *handle, uint32_t addr, uint16_t len,
1593 uint16_t read_len = len;
1594 struct stlink_usb_handle_s *h = handle;
1596 assert(handle != NULL);
1598 /* max 8bit read/write is 64bytes */
1599 if (len > STLINK_MAX_RW8) {
1600 LOG_DEBUG("max buffer length exceeded");
1604 stlink_usb_init_buffer(handle, h->rx_ep, read_len);
1606 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1607 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_READMEM_8BIT;
1608 h_u32_to_le(h->cmdbuf+h->cmdidx, addr);
1610 h_u16_to_le(h->cmdbuf+h->cmdidx, len);
1613 /* we need to fix read length for single bytes */
1617 res = stlink_usb_xfer(handle, h->databuf, read_len);
1619 if (res != ERROR_OK)
1622 memcpy(buffer, h->databuf, len);
1624 return stlink_usb_get_rw_status(handle);
1628 static int stlink_usb_write_mem8(void *handle, uint32_t addr, uint16_t len,
1629 const uint8_t *buffer)
1632 struct stlink_usb_handle_s *h = handle;
1634 assert(handle != NULL);
1636 /* max 8bit read/write is 64bytes */
1637 if (len > STLINK_MAX_RW8) {
1638 LOG_DEBUG("max buffer length exceeded");
1642 stlink_usb_init_buffer(handle, h->tx_ep, len);
1644 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1645 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_WRITEMEM_8BIT;
1646 h_u32_to_le(h->cmdbuf+h->cmdidx, addr);
1648 h_u16_to_le(h->cmdbuf+h->cmdidx, len);
1651 res = stlink_usb_xfer(handle, buffer, len);
1653 if (res != ERROR_OK)
1656 return stlink_usb_get_rw_status(handle);
1660 static int stlink_usb_read_mem32(void *handle, uint32_t addr, uint16_t len,
1664 struct stlink_usb_handle_s *h = handle;
1666 assert(handle != NULL);
1668 /* data must be a multiple of 4 and word aligned */
1669 if (len % 4 || addr % 4) {
1670 LOG_DEBUG("Invalid data alignment");
1671 return ERROR_TARGET_UNALIGNED_ACCESS;
1674 stlink_usb_init_buffer(handle, h->rx_ep, len);
1676 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1677 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_READMEM_32BIT;
1678 h_u32_to_le(h->cmdbuf+h->cmdidx, addr);
1680 h_u16_to_le(h->cmdbuf+h->cmdidx, len);
1683 res = stlink_usb_xfer(handle, h->databuf, len);
1685 if (res != ERROR_OK)
1688 memcpy(buffer, h->databuf, len);
1690 return stlink_usb_get_rw_status(handle);
1694 static int stlink_usb_write_mem32(void *handle, uint32_t addr, uint16_t len,
1695 const uint8_t *buffer)
1698 struct stlink_usb_handle_s *h = handle;
1700 assert(handle != NULL);
1702 /* data must be a multiple of 4 and word aligned */
1703 if (len % 4 || addr % 4) {
1704 LOG_DEBUG("Invalid data alignment");
1705 return ERROR_TARGET_UNALIGNED_ACCESS;
1708 stlink_usb_init_buffer(handle, h->tx_ep, len);
1710 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1711 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_WRITEMEM_32BIT;
1712 h_u32_to_le(h->cmdbuf+h->cmdidx, addr);
1714 h_u16_to_le(h->cmdbuf+h->cmdidx, len);
1717 res = stlink_usb_xfer(handle, buffer, len);
1719 if (res != ERROR_OK)
1722 return stlink_usb_get_rw_status(handle);
1725 static uint32_t stlink_max_block_size(uint32_t tar_autoincr_block, uint32_t address)
1727 uint32_t max_tar_block = (tar_autoincr_block - ((tar_autoincr_block - 1) & address));
1728 if (max_tar_block == 0)
1730 return max_tar_block;
1733 static int stlink_usb_read_mem(void *handle, uint32_t addr, uint32_t size,
1734 uint32_t count, uint8_t *buffer)
1736 int retval = ERROR_OK;
1737 uint32_t bytes_remaining;
1739 struct stlink_usb_handle_s *h = handle;
1741 /* calculate byte count */
1746 bytes_remaining = (size == 4) ? \
1747 stlink_max_block_size(h->max_mem_packet, addr) : STLINK_MAX_RW8;
1749 if (count < bytes_remaining)
1750 bytes_remaining = count;
1752 if (h->transport == HL_TRANSPORT_SWIM) {
1753 retval = stlink_swim_readbytes(handle, addr, bytes_remaining, buffer);
1754 if (retval != ERROR_OK)
1757 /* the stlink only supports 8/32bit memory read/writes
1758 * honour 32bit, all others will be handled as 8bit access */
1761 /* When in jtag mode the stlink uses the auto-increment functinality.
1762 * However it expects us to pass the data correctly, this includes
1763 * alignment and any page boundaries. We already do this as part of the
1764 * adi_v5 implementation, but the stlink is a hla adapter and so this
1765 * needs implementiong manually.
1766 * currently this only affects jtag mode, according to ST they do single
1767 * access in SWD mode - but this may change and so we do it for both modes */
1769 /* we first need to check for any unaligned bytes */
1772 uint32_t head_bytes = 4 - (addr % 4);
1773 retval = stlink_usb_read_mem8(handle, addr, head_bytes, buffer);
1774 if (retval == ERROR_WAIT && retries < MAX_WAIT_RETRIES) {
1775 usleep((1<<retries++) * 1000);
1778 if (retval != ERROR_OK)
1780 buffer += head_bytes;
1782 count -= head_bytes;
1783 bytes_remaining -= head_bytes;
1786 if (bytes_remaining % 4)
1787 retval = stlink_usb_read_mem(handle, addr, 1, bytes_remaining, buffer);
1789 retval = stlink_usb_read_mem32(handle, addr, bytes_remaining, buffer);
1791 retval = stlink_usb_read_mem8(handle, addr, bytes_remaining, buffer);
1793 if (retval == ERROR_WAIT && retries < MAX_WAIT_RETRIES) {
1794 usleep((1<<retries++) * 1000);
1797 if (retval != ERROR_OK)
1800 buffer += bytes_remaining;
1801 addr += bytes_remaining;
1802 count -= bytes_remaining;
1808 static int stlink_usb_write_mem(void *handle, uint32_t addr, uint32_t size,
1809 uint32_t count, const uint8_t *buffer)
1811 int retval = ERROR_OK;
1812 uint32_t bytes_remaining;
1814 struct stlink_usb_handle_s *h = handle;
1816 /* calculate byte count */
1821 bytes_remaining = (size == 4) ? \
1822 stlink_max_block_size(h->max_mem_packet, addr) : STLINK_MAX_RW8;
1824 if (count < bytes_remaining)
1825 bytes_remaining = count;
1827 if (h->transport == HL_TRANSPORT_SWIM) {
1828 retval = stlink_swim_writebytes(handle, addr, bytes_remaining, buffer);
1829 if (retval != ERROR_OK)
1832 /* the stlink only supports 8/32bit memory read/writes
1833 * honour 32bit, all others will be handled as 8bit access */
1836 /* When in jtag mode the stlink uses the auto-increment functinality.
1837 * However it expects us to pass the data correctly, this includes
1838 * alignment and any page boundaries. We already do this as part of the
1839 * adi_v5 implementation, but the stlink is a hla adapter and so this
1840 * needs implementiong manually.
1841 * currently this only affects jtag mode, according to ST they do single
1842 * access in SWD mode - but this may change and so we do it for both modes */
1844 /* we first need to check for any unaligned bytes */
1847 uint32_t head_bytes = 4 - (addr % 4);
1848 retval = stlink_usb_write_mem8(handle, addr, head_bytes, buffer);
1849 if (retval == ERROR_WAIT && retries < MAX_WAIT_RETRIES) {
1850 usleep((1<<retries++) * 1000);
1853 if (retval != ERROR_OK)
1855 buffer += head_bytes;
1857 count -= head_bytes;
1858 bytes_remaining -= head_bytes;
1861 if (bytes_remaining % 4)
1862 retval = stlink_usb_write_mem(handle, addr, 1, bytes_remaining, buffer);
1864 retval = stlink_usb_write_mem32(handle, addr, bytes_remaining, buffer);
1867 retval = stlink_usb_write_mem8(handle, addr, bytes_remaining, buffer);
1868 if (retval == ERROR_WAIT && retries < MAX_WAIT_RETRIES) {
1869 usleep((1<<retries++) * 1000);
1872 if (retval != ERROR_OK)
1875 buffer += bytes_remaining;
1876 addr += bytes_remaining;
1877 count -= bytes_remaining;
1884 static int stlink_usb_override_target(const char *targetname)
1886 return !strcmp(targetname, "cortex_m");
1889 static int stlink_speed(void *handle, int khz, bool query)
1892 int speed_index = -1;
1893 int speed_diff = INT_MAX;
1894 struct stlink_usb_handle_s *h = handle;
1896 if (h && (h->transport == HL_TRANSPORT_SWIM)) {
1898 we dont care what the khz rate is
1899 we only have low and high speed...
1900 before changing speed the SWIM_CSR HS bit
1904 stlink_swim_speed(handle, 0);
1906 stlink_swim_speed(handle, 1);
1910 /* only supported by stlink/v2 and for firmware >= 22 */
1911 if (h && (h->version.stlink == 1 || h->version.jtag < 22))
1914 for (i = 0; i < ARRAY_SIZE(stlink_khz_to_speed_map); i++) {
1915 if (khz == stlink_khz_to_speed_map[i].speed) {
1919 int current_diff = khz - stlink_khz_to_speed_map[i].speed;
1920 /* get abs value for comparison */
1921 current_diff = (current_diff > 0) ? current_diff : -current_diff;
1922 if ((current_diff < speed_diff) && khz >= stlink_khz_to_speed_map[i].speed) {
1923 speed_diff = current_diff;
1931 if (speed_index == -1) {
1932 /* this will only be here if we cannot match the slow speed.
1933 * use the slowest speed we support.*/
1934 speed_index = ARRAY_SIZE(stlink_khz_to_speed_map) - 1;
1936 } else if (i == ARRAY_SIZE(stlink_khz_to_speed_map))
1939 if (!match && query) {
1940 LOG_INFO("Unable to match requested speed %d kHz, using %d kHz", \
1941 khz, stlink_khz_to_speed_map[speed_index].speed);
1945 int result = stlink_usb_set_swdclk(h, stlink_khz_to_speed_map[speed_index].speed_divisor);
1946 if (result != ERROR_OK) {
1947 LOG_ERROR("Unable to set adapter speed");
1952 return stlink_khz_to_speed_map[speed_index].speed;
1956 static int stlink_usb_close(void *handle)
1960 enum stlink_mode emode;
1961 struct stlink_usb_handle_s *h = handle;
1964 res = stlink_usb_current_mode(handle, &mode);
1967 /* do not exit if return code != ERROR_OK,
1968 it prevents us from closing jtag_libusb */
1970 if (res == ERROR_OK) {
1971 /* try to exit current mode */
1973 case STLINK_DEV_DFU_MODE:
1974 emode = STLINK_MODE_DFU;
1976 case STLINK_DEV_DEBUG_MODE:
1977 emode = STLINK_MODE_DEBUG_SWD;
1979 case STLINK_DEV_SWIM_MODE:
1980 emode = STLINK_MODE_DEBUG_SWIM;
1982 case STLINK_DEV_BOOTLOADER_MODE:
1983 case STLINK_DEV_MASS_MODE:
1985 emode = STLINK_MODE_UNKNOWN;
1989 if (emode != STLINK_MODE_UNKNOWN)
1990 stlink_usb_mode_leave(handle, emode);
1991 /* do not check return code, it prevent
1992 us from closing jtag_libusb */
1996 jtag_libusb_close(h->fd);
2004 static int stlink_usb_open(struct hl_interface_param_s *param, void **fd)
2006 int err, retry_count = 1;
2007 struct stlink_usb_handle_s *h;
2008 enum stlink_jtag_api_version api;
2010 LOG_DEBUG("stlink_usb_open");
2012 h = calloc(1, sizeof(struct stlink_usb_handle_s));
2015 LOG_DEBUG("malloc failed");
2019 h->transport = param->transport;
2021 for (unsigned i = 0; param->vid[i]; i++) {
2022 LOG_DEBUG("transport: %d vid: 0x%04x pid: 0x%04x serial: %s",
2023 param->transport, param->vid[i], param->pid[i],
2024 param->serial ? param->serial : "");
2028 On certain host USB configurations(e.g. MacBook Air)
2029 STLINKv2 dongle seems to have its FW in a funky state if,
2030 after plugging it in, you try to use openocd with it more
2031 then once (by launching and closing openocd). In cases like
2032 that initial attempt to read the FW info via
2033 stlink_usb_version will fail and the device has to be reset
2034 in order to become operational.
2037 if (jtag_libusb_open(param->vid, param->pid, param->serial, &h->fd) != ERROR_OK) {
2038 LOG_ERROR("open failed");
2042 jtag_libusb_set_configuration(h->fd, 0);
2044 if (jtag_libusb_claim_interface(h->fd, 0) != ERROR_OK) {
2045 LOG_DEBUG("claim interface failed");
2049 /* RX EP is common for all versions */
2050 h->rx_ep = STLINK_RX_EP;
2053 if (jtag_libusb_get_pid(jtag_libusb_get_device(h->fd), &pid) != ERROR_OK) {
2054 LOG_DEBUG("libusb_get_pid failed");
2058 /* wrap version for first read */
2061 h->version.stlink = 1;
2062 h->tx_ep = STLINK_TX_EP;
2063 h->trace_ep = STLINK_TRACE_EP;
2065 case STLINK_V2_1_PID:
2066 h->version.stlink = 2;
2067 h->tx_ep = STLINK_V2_1_TX_EP;
2068 h->trace_ep = STLINK_V2_1_TRACE_EP;
2071 /* fall through - we assume V2 to be the default version*/
2073 h->version.stlink = 2;
2074 h->tx_ep = STLINK_TX_EP;
2075 h->trace_ep = STLINK_TRACE_EP;
2079 /* get the device version */
2080 err = stlink_usb_version(h);
2082 if (err == ERROR_OK) {
2084 } else if (h->version.stlink == 1 ||
2086 LOG_ERROR("read version failed");
2089 err = jtag_libusb_release_interface(h->fd, 0);
2090 if (err != ERROR_OK) {
2091 LOG_ERROR("release interface failed");
2095 err = jtag_libusb_reset_device(h->fd);
2096 if (err != ERROR_OK) {
2097 LOG_ERROR("reset device failed");
2101 jtag_libusb_close(h->fd);
2103 Give the device one second to settle down and
2106 usleep(1 * 1000 * 1000);
2111 /* check if mode is supported */
2114 switch (h->transport) {
2115 case HL_TRANSPORT_SWD:
2116 case HL_TRANSPORT_JTAG:
2117 if (h->version.jtag == 0)
2120 case HL_TRANSPORT_SWIM:
2121 if (h->version.swim == 0)
2129 if (err != ERROR_OK) {
2130 LOG_ERROR("mode (transport) not supported by device");
2134 api = h->version.jtag_api_max;
2136 LOG_INFO("using stlink api v%d", api);
2138 /* set the used jtag api, this will default to the newest supported version */
2141 /* initialize the debug hardware */
2142 err = stlink_usb_init_mode(h, param->connect_under_reset);
2144 if (err != ERROR_OK) {
2145 LOG_ERROR("init mode failed (unable to connect to the target)");
2149 if (h->transport == HL_TRANSPORT_SWIM) {
2150 err = stlink_swim_enter(h);
2151 if (err != ERROR_OK) {
2152 LOG_ERROR("stlink_swim_enter_failed (unable to connect to the target)");
2156 h->max_mem_packet = STLINK_DATA_SIZE;
2160 /* clock speed only supported by stlink/v2 and for firmware >= 22 */
2161 if (h->version.stlink >= 2 && h->version.jtag >= 22) {
2162 LOG_DEBUG("Supported clock speeds are:");
2164 for (unsigned i = 0; i < ARRAY_SIZE(stlink_khz_to_speed_map); i++)
2165 LOG_DEBUG("%d kHz", stlink_khz_to_speed_map[i].speed);
2167 stlink_speed(h, param->initial_interface_speed, false);
2170 /* get cpuid, so we can determine the max page size
2171 * start with a safe default */
2172 h->max_mem_packet = (1 << 10);
2175 err = stlink_usb_read_mem32(h, CPUID, 4, buffer);
2176 if (err == ERROR_OK) {
2177 uint32_t cpuid = le_to_h_u32(buffer);
2178 int i = (cpuid >> 4) & 0xf;
2179 if (i == 4 || i == 3) {
2180 /* Cortex-M3/M4 has 4096 bytes autoincrement range */
2181 h->max_mem_packet = (1 << 12);
2185 LOG_DEBUG("Using TAR autoincrement: %" PRIu32, h->max_mem_packet);
2192 stlink_usb_close(h);
2197 int stlink_config_trace(void *handle, bool enabled, enum tpio_pin_protocol pin_protocol,
2198 uint32_t port_size, unsigned int *trace_freq)
2200 struct stlink_usb_handle_s *h = handle;
2202 if (enabled && (h->jtag_api < 2 || pin_protocol != ASYNC_UART)) {
2203 LOG_ERROR("The attached ST-LINK version doesn't support this trace mode");
2208 stlink_usb_trace_disable(h);
2212 if (*trace_freq > STLINK_TRACE_MAX_HZ) {
2213 LOG_ERROR("ST-LINK doesn't support SWO frequency higher than %u",
2214 STLINK_TRACE_MAX_HZ);
2218 stlink_usb_trace_disable(h);
2221 *trace_freq = STLINK_TRACE_MAX_HZ;
2222 h->trace.source_hz = *trace_freq;
2224 return stlink_usb_trace_enable(h);
2228 struct hl_layout_api_s stlink_usb_layout_api = {
2230 .open = stlink_usb_open,
2232 .close = stlink_usb_close,
2234 .idcode = stlink_usb_idcode,
2236 .state = stlink_usb_state,
2238 .reset = stlink_usb_reset,
2240 .assert_srst = stlink_usb_assert_srst,
2242 .run = stlink_usb_run,
2244 .halt = stlink_usb_halt,
2246 .step = stlink_usb_step,
2248 .read_regs = stlink_usb_read_regs,
2250 .read_reg = stlink_usb_read_reg,
2252 .write_reg = stlink_usb_write_reg,
2254 .read_mem = stlink_usb_read_mem,
2256 .write_mem = stlink_usb_write_mem,
2258 .write_debug_reg = stlink_usb_write_debug_reg,
2260 .override_target = stlink_usb_override_target,
2262 .speed = stlink_speed,
2264 .config_trace = stlink_config_trace,
2266 .poll_trace = stlink_usb_trace_read,