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 /* TODO: no error checking yet on api V1 */
429 if (h->jtag_api == STLINK_JTAG_API_V1)
430 h->databuf[0] = STLINK_DEBUG_ERR_OK;
432 if (h->transport == HL_TRANSPORT_SWIM) {
433 switch (h->databuf[0]) {
434 case STLINK_SWIM_ERR_OK:
436 case STLINK_SWIM_BUSY:
439 LOG_DEBUG("unknown/unexpected STLINK status code 0x%x", h->databuf[0]);
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("Verify error");
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 if (connect_under_reset) {
908 /* if we are going to use swim we need to switch mode early */
909 if (emode == STLINK_MODE_DEBUG_SWIM) {
910 res = stlink_usb_mode_enter(handle, emode);
914 res = stlink_usb_assert_srst(handle, 0);
919 res = stlink_usb_mode_enter(handle, emode);
924 res = stlink_usb_current_mode(handle, &mode);
929 LOG_DEBUG("MODE: 0x%02X", mode);
934 /* request status from last swim request */
935 static int stlink_swim_status(void *handle)
937 struct stlink_usb_handle_s *h = handle;
940 stlink_usb_init_buffer(handle, h->rx_ep, 4);
941 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_COMMAND;
942 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_READSTATUS;
943 res = stlink_usb_xfer(handle, h->databuf, 4);
949 the purpose of this function is unknown...
950 capabilites? anyway for swim v6 it returns
953 __attribute__((unused))
954 static int stlink_swim_cap(void *handle, uint8_t *cap)
956 struct stlink_usb_handle_s *h = handle;
959 stlink_usb_init_buffer(handle, h->rx_ep, 8);
960 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_COMMAND;
961 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_READ_CAP;
962 h->cmdbuf[h->cmdidx++] = 0x01;
963 res = stlink_usb_xfer(handle, h->databuf, 8);
966 memcpy(cap, h->databuf, 8);
970 /* debug dongle assert/deassert sreset line */
971 static int stlink_swim_assert_reset(void *handle, int reset)
973 struct stlink_usb_handle_s *h = handle;
976 stlink_usb_init_buffer(handle, h->rx_ep, 0);
977 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_COMMAND;
979 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_ASSERT_RESET;
981 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_DEASSERT_RESET;
982 res = stlink_cmd_allow_retry(handle, h->databuf, 0);
990 1.3ms low then 750Hz then 1.5kHz
992 static int stlink_swim_enter(void *handle)
994 struct stlink_usb_handle_s *h = handle;
997 stlink_usb_init_buffer(handle, h->rx_ep, 0);
998 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_COMMAND;
999 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_ENTER_SEQ;
1000 res = stlink_cmd_allow_retry(handle, h->databuf, 0);
1001 if (res != ERROR_OK)
1006 /* switch high/low speed swim */
1007 static int stlink_swim_speed(void *handle, int speed)
1009 struct stlink_usb_handle_s *h = handle;
1012 stlink_usb_init_buffer(handle, h->rx_ep, 0);
1013 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_COMMAND;
1014 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_SPEED;
1016 h->cmdbuf[h->cmdidx++] = 1;
1018 h->cmdbuf[h->cmdidx++] = 0;
1019 res = stlink_cmd_allow_retry(handle, h->databuf, 0);
1020 if (res != ERROR_OK)
1026 initiate srst from swim.
1027 nrst is pulled low for 50us.
1029 static int stlink_swim_generate_rst(void *handle)
1031 struct stlink_usb_handle_s *h = handle;
1034 stlink_usb_init_buffer(handle, h->rx_ep, 0);
1035 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_COMMAND;
1036 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_GEN_RST;
1037 res = stlink_cmd_allow_retry(handle, h->databuf, 0);
1038 if (res != ERROR_OK)
1044 send resyncronize sequence
1045 swim is pulled low for 16us
1046 reply is 64 clks low
1048 static int stlink_swim_resync(void *handle)
1050 struct stlink_usb_handle_s *h = handle;
1053 stlink_usb_init_buffer(handle, h->rx_ep, 0);
1054 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_COMMAND;
1055 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_RESET;
1056 res = stlink_cmd_allow_retry(handle, h->databuf, 0);
1057 if (res != ERROR_OK)
1062 static int stlink_swim_writebytes(void *handle, uint32_t addr, uint32_t len, const uint8_t *data)
1064 struct stlink_usb_handle_s *h = handle;
1067 unsigned int datalen = 0;
1068 int cmdsize = STLINK_CMD_SIZE_V2;
1070 if (len > STLINK_DATA_SIZE)
1073 if (h->version.stlink == 1)
1074 cmdsize = STLINK_SG_SIZE;
1076 stlink_usb_init_buffer(handle, h->tx_ep, 0);
1077 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_COMMAND;
1078 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_WRITEMEM;
1079 h_u16_to_be(h->cmdbuf+h->cmdidx, len);
1081 h_u32_to_be(h->cmdbuf+h->cmdidx, addr);
1083 for (i = 0; i < len; i++) {
1084 if (h->cmdidx == cmdsize)
1085 h->databuf[datalen++] = *(data++);
1087 h->cmdbuf[h->cmdidx++] = *(data++);
1089 if (h->version.stlink == 1)
1090 stlink_usb_set_cbw_transfer_datalength(handle, datalen);
1092 res = stlink_cmd_allow_retry(handle, h->databuf, datalen);
1093 if (res != ERROR_OK)
1098 static int stlink_swim_readbytes(void *handle, uint32_t addr, uint32_t len, uint8_t *data)
1100 struct stlink_usb_handle_s *h = handle;
1103 if (len > STLINK_DATA_SIZE)
1106 stlink_usb_init_buffer(handle, h->rx_ep, 0);
1107 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_COMMAND;
1108 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_READMEM;
1109 h_u16_to_be(h->cmdbuf+h->cmdidx, len);
1111 h_u32_to_be(h->cmdbuf+h->cmdidx, addr);
1113 res = stlink_cmd_allow_retry(handle, h->databuf, 0);
1114 if (res != ERROR_OK)
1117 stlink_usb_init_buffer(handle, h->rx_ep, len);
1118 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_COMMAND;
1119 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_READBUF;
1120 res = stlink_usb_xfer(handle, data, len);
1121 if (res != ERROR_OK)
1128 static int stlink_usb_idcode(void *handle, uint32_t *idcode)
1131 struct stlink_usb_handle_s *h = handle;
1133 assert(handle != NULL);
1135 /* there is no swim read core id cmd */
1136 if (h->transport == HL_TRANSPORT_SWIM) {
1141 stlink_usb_init_buffer(handle, h->rx_ep, 4);
1143 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1144 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_READCOREID;
1146 res = stlink_usb_xfer(handle, h->databuf, 4);
1148 if (res != ERROR_OK)
1151 *idcode = le_to_h_u32(h->databuf);
1153 LOG_DEBUG("IDCODE: 0x%08" PRIX32, *idcode);
1158 static int stlink_usb_v2_read_debug_reg(void *handle, uint32_t addr, uint32_t *val)
1160 struct stlink_usb_handle_s *h = handle;
1163 assert(handle != NULL);
1165 stlink_usb_init_buffer(handle, h->rx_ep, 8);
1167 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1168 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_READDEBUGREG;
1169 h_u32_to_le(h->cmdbuf+h->cmdidx, addr);
1172 res = stlink_cmd_allow_retry(handle, h->databuf, 8);
1173 if (res != ERROR_OK)
1176 *val = le_to_h_u32(h->databuf + 4);
1180 static int stlink_usb_write_debug_reg(void *handle, uint32_t addr, uint32_t val)
1182 struct stlink_usb_handle_s *h = handle;
1184 assert(handle != NULL);
1186 stlink_usb_init_buffer(handle, h->rx_ep, 2);
1188 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1189 if (h->jtag_api == STLINK_JTAG_API_V1)
1190 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_WRITEDEBUGREG;
1192 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_WRITEDEBUGREG;
1193 h_u32_to_le(h->cmdbuf+h->cmdidx, addr);
1195 h_u32_to_le(h->cmdbuf+h->cmdidx, val);
1198 return stlink_cmd_allow_retry(handle, h->databuf, 2);
1202 static int stlink_usb_trace_read(void *handle, uint8_t *buf, size_t *size)
1204 struct stlink_usb_handle_s *h = handle;
1206 assert(handle != NULL);
1208 if (h->trace.enabled && h->version.jtag >= STLINK_TRACE_MIN_VERSION) {
1211 stlink_usb_init_buffer(handle, h->rx_ep, 10);
1213 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1214 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_GET_TRACE_NB;
1216 res = stlink_usb_xfer(handle, h->databuf, 2);
1217 if (res != ERROR_OK)
1220 size_t bytes_avail = le_to_h_u16(h->databuf);
1221 *size = bytes_avail < *size ? bytes_avail : *size - 1;
1224 res = stlink_usb_read_trace(handle, buf, *size);
1225 if (res != ERROR_OK)
1234 static enum target_state stlink_usb_v2_get_status(void *handle)
1239 result = stlink_usb_v2_read_debug_reg(handle, DCB_DHCSR, &status);
1240 if (result != ERROR_OK)
1241 return TARGET_UNKNOWN;
1243 if (status & S_HALT)
1244 return TARGET_HALTED;
1245 else if (status & S_RESET_ST)
1246 return TARGET_RESET;
1248 return TARGET_RUNNING;
1252 static enum target_state stlink_usb_state(void *handle)
1255 struct stlink_usb_handle_s *h = handle;
1257 assert(handle != NULL);
1259 if (h->transport == HL_TRANSPORT_SWIM) {
1260 res = stlink_usb_mode_enter(handle, stlink_get_mode(h->transport));
1261 if (res != ERROR_OK)
1262 return TARGET_UNKNOWN;
1264 res = stlink_swim_resync(handle);
1265 if (res != ERROR_OK)
1266 return TARGET_UNKNOWN;
1271 if (h->reconnect_pending) {
1272 LOG_INFO("Previous state query failed, trying to reconnect");
1273 res = stlink_usb_mode_enter(handle, stlink_get_mode(h->transport));
1275 if (res != ERROR_OK)
1276 return TARGET_UNKNOWN;
1278 h->reconnect_pending = false;
1281 if (h->jtag_api == STLINK_JTAG_API_V2) {
1282 res = stlink_usb_v2_get_status(handle);
1283 if (res == TARGET_UNKNOWN)
1284 h->reconnect_pending = true;
1288 stlink_usb_init_buffer(handle, h->rx_ep, 2);
1290 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1291 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_GETSTATUS;
1293 res = stlink_usb_xfer(handle, h->databuf, 2);
1295 if (res != ERROR_OK)
1296 return TARGET_UNKNOWN;
1298 if (h->databuf[0] == STLINK_CORE_RUNNING)
1299 return TARGET_RUNNING;
1300 if (h->databuf[0] == STLINK_CORE_HALTED)
1301 return TARGET_HALTED;
1303 h->reconnect_pending = true;
1305 return TARGET_UNKNOWN;
1308 static int stlink_usb_assert_srst(void *handle, int srst)
1310 struct stlink_usb_handle_s *h = handle;
1312 assert(handle != NULL);
1314 if (h->transport == HL_TRANSPORT_SWIM)
1315 return stlink_swim_assert_reset(handle, srst);
1317 if (h->version.stlink == 1)
1318 return ERROR_COMMAND_NOTFOUND;
1320 stlink_usb_init_buffer(handle, h->rx_ep, 2);
1322 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1323 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_DRIVE_NRST;
1324 h->cmdbuf[h->cmdidx++] = srst;
1326 return stlink_cmd_allow_retry(handle, h->databuf, 2);
1330 static void stlink_usb_trace_disable(void *handle)
1333 struct stlink_usb_handle_s *h = handle;
1335 assert(handle != NULL);
1337 assert(h->version.jtag >= STLINK_TRACE_MIN_VERSION);
1339 LOG_DEBUG("Tracing: disable");
1341 stlink_usb_init_buffer(handle, h->rx_ep, 2);
1342 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1343 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_STOP_TRACE_RX;
1344 res = stlink_usb_xfer(handle, h->databuf, 2);
1346 if (res == ERROR_OK)
1347 h->trace.enabled = false;
1352 static int stlink_usb_trace_enable(void *handle)
1355 struct stlink_usb_handle_s *h = handle;
1357 assert(handle != NULL);
1359 if (h->version.jtag >= STLINK_TRACE_MIN_VERSION) {
1360 stlink_usb_init_buffer(handle, h->rx_ep, 10);
1362 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1363 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_START_TRACE_RX;
1364 h_u16_to_le(h->cmdbuf+h->cmdidx, (uint16_t)STLINK_TRACE_SIZE);
1366 h_u32_to_le(h->cmdbuf+h->cmdidx, h->trace.source_hz);
1369 res = stlink_usb_xfer(handle, h->databuf, 2);
1371 if (res == ERROR_OK) {
1372 h->trace.enabled = true;
1373 LOG_DEBUG("Tracing: recording at %" PRIu32 "Hz", h->trace.source_hz);
1376 LOG_ERROR("Tracing is not supported by this version.");
1384 static int stlink_usb_reset(void *handle)
1386 struct stlink_usb_handle_s *h = handle;
1389 assert(handle != NULL);
1391 if (h->transport == HL_TRANSPORT_SWIM)
1392 return stlink_swim_generate_rst(handle);
1394 stlink_usb_init_buffer(handle, h->rx_ep, 2);
1396 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1398 if (h->jtag_api == STLINK_JTAG_API_V1)
1399 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_RESETSYS;
1401 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_RESETSYS;
1403 retval = stlink_cmd_allow_retry(handle, h->databuf, 2);
1404 if (retval != ERROR_OK)
1407 if (h->trace.enabled) {
1408 stlink_usb_trace_disable(h);
1409 return stlink_usb_trace_enable(h);
1416 static int stlink_usb_run(void *handle)
1419 struct stlink_usb_handle_s *h = handle;
1421 assert(handle != NULL);
1423 if (h->jtag_api == STLINK_JTAG_API_V2) {
1424 res = stlink_usb_write_debug_reg(handle, DCB_DHCSR, DBGKEY|C_DEBUGEN);
1429 stlink_usb_init_buffer(handle, h->rx_ep, 2);
1431 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1432 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_RUNCORE;
1434 return stlink_cmd_allow_retry(handle, h->databuf, 2);
1438 static int stlink_usb_halt(void *handle)
1441 struct stlink_usb_handle_s *h = handle;
1443 assert(handle != NULL);
1445 if (h->jtag_api == STLINK_JTAG_API_V2) {
1446 res = stlink_usb_write_debug_reg(handle, DCB_DHCSR, DBGKEY|C_HALT|C_DEBUGEN);
1451 stlink_usb_init_buffer(handle, h->rx_ep, 2);
1453 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1454 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_FORCEDEBUG;
1456 return stlink_cmd_allow_retry(handle, h->databuf, 2);
1460 static int stlink_usb_step(void *handle)
1462 struct stlink_usb_handle_s *h = handle;
1464 assert(handle != NULL);
1466 if (h->jtag_api == STLINK_JTAG_API_V2) {
1467 /* TODO: this emulates the v1 api, it should really use a similar auto mask isr
1468 * that the Cortex-M3 currently does. */
1469 stlink_usb_write_debug_reg(handle, DCB_DHCSR, DBGKEY|C_HALT|C_MASKINTS|C_DEBUGEN);
1470 stlink_usb_write_debug_reg(handle, DCB_DHCSR, DBGKEY|C_STEP|C_MASKINTS|C_DEBUGEN);
1471 return stlink_usb_write_debug_reg(handle, DCB_DHCSR, DBGKEY|C_HALT|C_DEBUGEN);
1474 stlink_usb_init_buffer(handle, h->rx_ep, 2);
1476 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1477 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_STEPCORE;
1479 return stlink_cmd_allow_retry(handle, h->databuf, 2);
1483 static int stlink_usb_read_regs(void *handle)
1486 struct stlink_usb_handle_s *h = handle;
1488 assert(handle != NULL);
1490 stlink_usb_init_buffer(handle, h->rx_ep, 84);
1492 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1493 if (h->jtag_api == STLINK_JTAG_API_V1)
1494 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_READALLREGS;
1496 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_READALLREGS;
1498 res = stlink_usb_xfer(handle, h->databuf, 84);
1500 if (res != ERROR_OK)
1507 static int stlink_usb_read_reg(void *handle, int num, uint32_t *val)
1510 struct stlink_usb_handle_s *h = handle;
1512 assert(handle != NULL);
1514 stlink_usb_init_buffer(handle, h->rx_ep, h->jtag_api == STLINK_JTAG_API_V1 ? 4 : 8);
1516 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1517 if (h->jtag_api == STLINK_JTAG_API_V1)
1518 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_READREG;
1520 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_READREG;
1521 h->cmdbuf[h->cmdidx++] = num;
1523 if (h->jtag_api == STLINK_JTAG_API_V1) {
1524 res = stlink_usb_xfer(handle, h->databuf, 4);
1525 if (res != ERROR_OK)
1527 *val = le_to_h_u32(h->databuf);
1530 res = stlink_cmd_allow_retry(handle, h->databuf, 8);
1531 if (res != ERROR_OK)
1533 *val = le_to_h_u32(h->databuf + 4);
1539 static int stlink_usb_write_reg(void *handle, int num, uint32_t val)
1541 struct stlink_usb_handle_s *h = handle;
1543 assert(handle != NULL);
1545 stlink_usb_init_buffer(handle, h->rx_ep, 2);
1547 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1548 if (h->jtag_api == STLINK_JTAG_API_V1)
1549 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_WRITEREG;
1551 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_WRITEREG;
1552 h->cmdbuf[h->cmdidx++] = num;
1553 h_u32_to_le(h->cmdbuf+h->cmdidx, val);
1556 return stlink_cmd_allow_retry(handle, h->databuf, 2);
1559 static int stlink_usb_get_rw_status(void *handle)
1562 struct stlink_usb_handle_s *h = handle;
1564 assert(handle != NULL);
1566 if (h->jtag_api == STLINK_JTAG_API_V1)
1569 stlink_usb_init_buffer(handle, h->rx_ep, 2);
1571 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1572 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_GETLASTRWSTATUS;
1574 res = stlink_usb_xfer(handle, h->databuf, 2);
1576 if (res != ERROR_OK)
1579 return stlink_usb_error_check(h);
1583 static int stlink_usb_read_mem8(void *handle, uint32_t addr, uint16_t len,
1587 uint16_t read_len = len;
1588 struct stlink_usb_handle_s *h = handle;
1590 assert(handle != NULL);
1592 /* max 8bit read/write is 64bytes */
1593 if (len > STLINK_MAX_RW8) {
1594 LOG_DEBUG("max buffer length exceeded");
1598 stlink_usb_init_buffer(handle, h->rx_ep, read_len);
1600 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1601 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_READMEM_8BIT;
1602 h_u32_to_le(h->cmdbuf+h->cmdidx, addr);
1604 h_u16_to_le(h->cmdbuf+h->cmdidx, len);
1607 /* we need to fix read length for single bytes */
1611 res = stlink_usb_xfer(handle, h->databuf, read_len);
1613 if (res != ERROR_OK)
1616 memcpy(buffer, h->databuf, len);
1618 return stlink_usb_get_rw_status(handle);
1622 static int stlink_usb_write_mem8(void *handle, uint32_t addr, uint16_t len,
1623 const uint8_t *buffer)
1626 struct stlink_usb_handle_s *h = handle;
1628 assert(handle != NULL);
1630 /* max 8bit read/write is 64bytes */
1631 if (len > STLINK_MAX_RW8) {
1632 LOG_DEBUG("max buffer length exceeded");
1636 stlink_usb_init_buffer(handle, h->tx_ep, len);
1638 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1639 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_WRITEMEM_8BIT;
1640 h_u32_to_le(h->cmdbuf+h->cmdidx, addr);
1642 h_u16_to_le(h->cmdbuf+h->cmdidx, len);
1645 res = stlink_usb_xfer(handle, buffer, len);
1647 if (res != ERROR_OK)
1650 return stlink_usb_get_rw_status(handle);
1654 static int stlink_usb_read_mem32(void *handle, uint32_t addr, uint16_t len,
1658 struct stlink_usb_handle_s *h = handle;
1660 assert(handle != NULL);
1662 /* data must be a multiple of 4 and word aligned */
1663 if (len % 4 || addr % 4) {
1664 LOG_DEBUG("Invalid data alignment");
1665 return ERROR_TARGET_UNALIGNED_ACCESS;
1668 stlink_usb_init_buffer(handle, h->rx_ep, len);
1670 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1671 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_READMEM_32BIT;
1672 h_u32_to_le(h->cmdbuf+h->cmdidx, addr);
1674 h_u16_to_le(h->cmdbuf+h->cmdidx, len);
1677 res = stlink_usb_xfer(handle, h->databuf, len);
1679 if (res != ERROR_OK)
1682 memcpy(buffer, h->databuf, len);
1684 return stlink_usb_get_rw_status(handle);
1688 static int stlink_usb_write_mem32(void *handle, uint32_t addr, uint16_t len,
1689 const uint8_t *buffer)
1692 struct stlink_usb_handle_s *h = handle;
1694 assert(handle != NULL);
1696 /* data must be a multiple of 4 and word aligned */
1697 if (len % 4 || addr % 4) {
1698 LOG_DEBUG("Invalid data alignment");
1699 return ERROR_TARGET_UNALIGNED_ACCESS;
1702 stlink_usb_init_buffer(handle, h->tx_ep, len);
1704 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1705 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_WRITEMEM_32BIT;
1706 h_u32_to_le(h->cmdbuf+h->cmdidx, addr);
1708 h_u16_to_le(h->cmdbuf+h->cmdidx, len);
1711 res = stlink_usb_xfer(handle, buffer, len);
1713 if (res != ERROR_OK)
1716 return stlink_usb_get_rw_status(handle);
1719 static uint32_t stlink_max_block_size(uint32_t tar_autoincr_block, uint32_t address)
1721 uint32_t max_tar_block = (tar_autoincr_block - ((tar_autoincr_block - 1) & address));
1722 if (max_tar_block == 0)
1724 return max_tar_block;
1727 static int stlink_usb_read_mem(void *handle, uint32_t addr, uint32_t size,
1728 uint32_t count, uint8_t *buffer)
1730 int retval = ERROR_OK;
1731 uint32_t bytes_remaining;
1733 struct stlink_usb_handle_s *h = handle;
1735 /* calculate byte count */
1740 bytes_remaining = (size == 4) ? \
1741 stlink_max_block_size(h->max_mem_packet, addr) : STLINK_MAX_RW8;
1743 if (count < bytes_remaining)
1744 bytes_remaining = count;
1746 if (h->transport == HL_TRANSPORT_SWIM) {
1747 retval = stlink_swim_readbytes(handle, addr, bytes_remaining, buffer);
1748 if (retval != ERROR_OK)
1751 /* the stlink only supports 8/32bit memory read/writes
1752 * honour 32bit, all others will be handled as 8bit access */
1755 /* When in jtag mode the stlink uses the auto-increment functinality.
1756 * However it expects us to pass the data correctly, this includes
1757 * alignment and any page boundaries. We already do this as part of the
1758 * adi_v5 implementation, but the stlink is a hla adapter and so this
1759 * needs implementiong manually.
1760 * currently this only affects jtag mode, according to ST they do single
1761 * access in SWD mode - but this may change and so we do it for both modes */
1763 /* we first need to check for any unaligned bytes */
1766 uint32_t head_bytes = 4 - (addr % 4);
1767 retval = stlink_usb_read_mem8(handle, addr, head_bytes, buffer);
1768 if (retval == ERROR_WAIT && retries < MAX_WAIT_RETRIES) {
1769 usleep((1<<retries++) * 1000);
1772 if (retval != ERROR_OK)
1774 buffer += head_bytes;
1776 count -= head_bytes;
1777 bytes_remaining -= head_bytes;
1780 if (bytes_remaining % 4)
1781 retval = stlink_usb_read_mem(handle, addr, 1, bytes_remaining, buffer);
1783 retval = stlink_usb_read_mem32(handle, addr, bytes_remaining, buffer);
1785 retval = stlink_usb_read_mem8(handle, addr, bytes_remaining, buffer);
1787 if (retval == ERROR_WAIT && retries < MAX_WAIT_RETRIES) {
1788 usleep((1<<retries++) * 1000);
1791 if (retval != ERROR_OK)
1794 buffer += bytes_remaining;
1795 addr += bytes_remaining;
1796 count -= bytes_remaining;
1802 static int stlink_usb_write_mem(void *handle, uint32_t addr, uint32_t size,
1803 uint32_t count, const uint8_t *buffer)
1805 int retval = ERROR_OK;
1806 uint32_t bytes_remaining;
1808 struct stlink_usb_handle_s *h = handle;
1810 /* calculate byte count */
1815 bytes_remaining = (size == 4) ? \
1816 stlink_max_block_size(h->max_mem_packet, addr) : STLINK_MAX_RW8;
1818 if (count < bytes_remaining)
1819 bytes_remaining = count;
1821 if (h->transport == HL_TRANSPORT_SWIM) {
1822 retval = stlink_swim_writebytes(handle, addr, bytes_remaining, buffer);
1823 if (retval != ERROR_OK)
1826 /* the stlink only supports 8/32bit memory read/writes
1827 * honour 32bit, all others will be handled as 8bit access */
1830 /* When in jtag mode the stlink uses the auto-increment functinality.
1831 * However it expects us to pass the data correctly, this includes
1832 * alignment and any page boundaries. We already do this as part of the
1833 * adi_v5 implementation, but the stlink is a hla adapter and so this
1834 * needs implementiong manually.
1835 * currently this only affects jtag mode, according to ST they do single
1836 * access in SWD mode - but this may change and so we do it for both modes */
1838 /* we first need to check for any unaligned bytes */
1841 uint32_t head_bytes = 4 - (addr % 4);
1842 retval = stlink_usb_write_mem8(handle, addr, head_bytes, buffer);
1843 if (retval == ERROR_WAIT && retries < MAX_WAIT_RETRIES) {
1844 usleep((1<<retries++) * 1000);
1847 if (retval != ERROR_OK)
1849 buffer += head_bytes;
1851 count -= head_bytes;
1852 bytes_remaining -= head_bytes;
1855 if (bytes_remaining % 4)
1856 retval = stlink_usb_write_mem(handle, addr, 1, bytes_remaining, buffer);
1858 retval = stlink_usb_write_mem32(handle, addr, bytes_remaining, buffer);
1861 retval = stlink_usb_write_mem8(handle, addr, bytes_remaining, buffer);
1862 if (retval == ERROR_WAIT && retries < MAX_WAIT_RETRIES) {
1863 usleep((1<<retries++) * 1000);
1866 if (retval != ERROR_OK)
1869 buffer += bytes_remaining;
1870 addr += bytes_remaining;
1871 count -= bytes_remaining;
1878 static int stlink_usb_override_target(const char *targetname)
1880 return !strcmp(targetname, "cortex_m");
1883 static int stlink_speed(void *handle, int khz, bool query)
1886 int speed_index = -1;
1887 int speed_diff = INT_MAX;
1888 struct stlink_usb_handle_s *h = handle;
1890 if (h && (h->transport == HL_TRANSPORT_SWIM)) {
1892 we dont care what the khz rate is
1893 we only have low and high speed...
1894 before changing speed the SWIM_CSR HS bit
1898 stlink_swim_speed(handle, 0);
1900 stlink_swim_speed(handle, 1);
1904 /* only supported by stlink/v2 and for firmware >= 22 */
1905 if (h && (h->version.stlink == 1 || h->version.jtag < 22))
1908 for (i = 0; i < ARRAY_SIZE(stlink_khz_to_speed_map); i++) {
1909 if (khz == stlink_khz_to_speed_map[i].speed) {
1913 int current_diff = khz - stlink_khz_to_speed_map[i].speed;
1914 /* get abs value for comparison */
1915 current_diff = (current_diff > 0) ? current_diff : -current_diff;
1916 if ((current_diff < speed_diff) && khz >= stlink_khz_to_speed_map[i].speed) {
1917 speed_diff = current_diff;
1925 if (speed_index == -1) {
1926 /* this will only be here if we cannot match the slow speed.
1927 * use the slowest speed we support.*/
1928 speed_index = ARRAY_SIZE(stlink_khz_to_speed_map) - 1;
1930 } else if (i == ARRAY_SIZE(stlink_khz_to_speed_map))
1933 if (!match && query) {
1934 LOG_INFO("Unable to match requested speed %d kHz, using %d kHz", \
1935 khz, stlink_khz_to_speed_map[speed_index].speed);
1939 int result = stlink_usb_set_swdclk(h, stlink_khz_to_speed_map[speed_index].speed_divisor);
1940 if (result != ERROR_OK) {
1941 LOG_ERROR("Unable to set adapter speed");
1946 return stlink_khz_to_speed_map[speed_index].speed;
1950 static int stlink_usb_close(void *handle)
1952 struct stlink_usb_handle_s *h = handle;
1955 jtag_libusb_close(h->fd);
1963 static int stlink_usb_open(struct hl_interface_param_s *param, void **fd)
1965 int err, retry_count = 1;
1966 struct stlink_usb_handle_s *h;
1967 enum stlink_jtag_api_version api;
1969 LOG_DEBUG("stlink_usb_open");
1971 h = calloc(1, sizeof(struct stlink_usb_handle_s));
1974 LOG_DEBUG("malloc failed");
1978 h->transport = param->transport;
1980 for (unsigned i = 0; param->vid[i]; i++) {
1981 LOG_DEBUG("transport: %d vid: 0x%04x pid: 0x%04x serial: %s",
1982 param->transport, param->vid[i], param->pid[i],
1983 param->serial ? param->serial : "");
1987 On certain host USB configurations(e.g. MacBook Air)
1988 STLINKv2 dongle seems to have its FW in a funky state if,
1989 after plugging it in, you try to use openocd with it more
1990 then once (by launching and closing openocd). In cases like
1991 that initial attempt to read the FW info via
1992 stlink_usb_version will fail and the device has to be reset
1993 in order to become operational.
1996 if (jtag_libusb_open(param->vid, param->pid, param->serial, &h->fd) != ERROR_OK) {
1997 LOG_ERROR("open failed");
2001 jtag_libusb_set_configuration(h->fd, 0);
2003 if (jtag_libusb_claim_interface(h->fd, 0) != ERROR_OK) {
2004 LOG_DEBUG("claim interface failed");
2008 /* RX EP is common for all versions */
2009 h->rx_ep = STLINK_RX_EP;
2012 if (jtag_libusb_get_pid(jtag_libusb_get_device(h->fd), &pid) != ERROR_OK) {
2013 LOG_DEBUG("libusb_get_pid failed");
2017 /* wrap version for first read */
2020 h->version.stlink = 1;
2021 h->tx_ep = STLINK_TX_EP;
2022 h->trace_ep = STLINK_TRACE_EP;
2024 case STLINK_V2_1_PID:
2025 h->version.stlink = 2;
2026 h->tx_ep = STLINK_V2_1_TX_EP;
2027 h->trace_ep = STLINK_V2_1_TRACE_EP;
2030 /* fall through - we assume V2 to be the default version*/
2032 h->version.stlink = 2;
2033 h->tx_ep = STLINK_TX_EP;
2034 h->trace_ep = STLINK_TRACE_EP;
2038 /* get the device version */
2039 err = stlink_usb_version(h);
2041 if (err == ERROR_OK) {
2043 } else if (h->version.stlink == 1 ||
2045 LOG_ERROR("read version failed");
2048 err = jtag_libusb_release_interface(h->fd, 0);
2049 if (err != ERROR_OK) {
2050 LOG_ERROR("release interface failed");
2054 err = jtag_libusb_reset_device(h->fd);
2055 if (err != ERROR_OK) {
2056 LOG_ERROR("reset device failed");
2060 jtag_libusb_close(h->fd);
2062 Give the device one second to settle down and
2065 usleep(1 * 1000 * 1000);
2070 /* check if mode is supported */
2073 switch (h->transport) {
2074 case HL_TRANSPORT_SWD:
2075 case HL_TRANSPORT_JTAG:
2076 if (h->version.jtag == 0)
2079 case HL_TRANSPORT_SWIM:
2080 if (h->version.swim == 0)
2088 if (err != ERROR_OK) {
2089 LOG_ERROR("mode (transport) not supported by device");
2093 api = h->version.jtag_api_max;
2095 LOG_INFO("using stlink api v%d", api);
2097 /* set the used jtag api, this will default to the newest supported version */
2100 /* initialize the debug hardware */
2101 err = stlink_usb_init_mode(h, param->connect_under_reset);
2103 if (err != ERROR_OK) {
2104 LOG_ERROR("init mode failed (unable to connect to the target)");
2108 if (h->transport == HL_TRANSPORT_SWIM) {
2109 err = stlink_swim_enter(h);
2110 if (err != ERROR_OK) {
2111 LOG_ERROR("stlink_swim_enter_failed (unable to connect to the target)");
2115 h->max_mem_packet = STLINK_DATA_SIZE;
2119 /* clock speed only supported by stlink/v2 and for firmware >= 22 */
2120 if (h->version.stlink >= 2 && h->version.jtag >= 22) {
2121 LOG_DEBUG("Supported clock speeds are:");
2123 for (unsigned i = 0; i < ARRAY_SIZE(stlink_khz_to_speed_map); i++)
2124 LOG_DEBUG("%d kHz", stlink_khz_to_speed_map[i].speed);
2126 stlink_speed(h, param->initial_interface_speed, false);
2129 /* get cpuid, so we can determine the max page size
2130 * start with a safe default */
2131 h->max_mem_packet = (1 << 10);
2134 err = stlink_usb_read_mem32(h, CPUID, 4, buffer);
2135 if (err == ERROR_OK) {
2136 uint32_t cpuid = le_to_h_u32(buffer);
2137 int i = (cpuid >> 4) & 0xf;
2138 if (i == 4 || i == 3) {
2139 /* Cortex-M3/M4 has 4096 bytes autoincrement range */
2140 h->max_mem_packet = (1 << 12);
2144 LOG_DEBUG("Using TAR autoincrement: %" PRIu32, h->max_mem_packet);
2151 stlink_usb_close(h);
2156 int stlink_config_trace(void *handle, bool enabled, enum tpio_pin_protocol pin_protocol,
2157 uint32_t port_size, unsigned int *trace_freq)
2159 struct stlink_usb_handle_s *h = handle;
2161 if (enabled && (h->jtag_api < 2 || pin_protocol != ASYNC_UART)) {
2162 LOG_ERROR("The attached ST-LINK version doesn't support this trace mode");
2167 stlink_usb_trace_disable(h);
2171 if (*trace_freq > STLINK_TRACE_MAX_HZ) {
2172 LOG_ERROR("ST-LINK doesn't support SWO frequency higher than %u",
2173 STLINK_TRACE_MAX_HZ);
2177 stlink_usb_trace_disable(h);
2180 *trace_freq = STLINK_TRACE_MAX_HZ;
2181 h->trace.source_hz = *trace_freq;
2183 return stlink_usb_trace_enable(h);
2187 struct hl_layout_api_s stlink_usb_layout_api = {
2189 .open = stlink_usb_open,
2191 .close = stlink_usb_close,
2193 .idcode = stlink_usb_idcode,
2195 .state = stlink_usb_state,
2197 .reset = stlink_usb_reset,
2199 .assert_srst = stlink_usb_assert_srst,
2201 .run = stlink_usb_run,
2203 .halt = stlink_usb_halt,
2205 .step = stlink_usb_step,
2207 .read_regs = stlink_usb_read_regs,
2209 .read_reg = stlink_usb_read_reg,
2211 .write_reg = stlink_usb_write_reg,
2213 .read_mem = stlink_usb_read_mem,
2215 .write_mem = stlink_usb_write_mem,
2217 .write_debug_reg = stlink_usb_write_debug_reg,
2219 .override_target = stlink_usb_override_target,
2221 .speed = stlink_speed,
2223 .config_trace = stlink_config_trace,
2225 .poll_trace = stlink_usb_trace_read,