1 /***************************************************************************
2 * Copyright (C) 2011-2012 by Mathias Kuester *
3 * Mathias Kuester <kesmtp@freenet.de> *
5 * Copyright (C) 2012 by Spencer Oliver *
6 * spen@spen-soft.co.uk *
8 * This code is based on https://github.com/texane/stlink *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
15 * This program is distributed in the hope that it will be useful, *
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
18 * GNU General Public License for more details. *
20 * You should have received a copy of the GNU General Public License *
21 * along with this program; if not, write to the *
22 * Free Software Foundation, Inc., *
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
24 ***************************************************************************/
30 /* project specific includes */
31 #include <helper/binarybuffer.h>
32 #include <jtag/interface.h>
33 #include <jtag/hla/hla_layout.h>
34 #include <jtag/hla/hla_transport.h>
35 #include <jtag/hla/hla_interface.h>
36 #include <target/target.h>
38 #include <target/cortex_m.h>
40 #include "libusb_common.h"
42 #define ENDPOINT_IN 0x80
43 #define ENDPOINT_OUT 0x00
45 #define STLINK_WRITE_TIMEOUT 1000
46 #define STLINK_READ_TIMEOUT 1000
48 #define STLINK_NULL_EP 0
49 #define STLINK_RX_EP (1|ENDPOINT_IN)
50 #define STLINK_TX_EP (2|ENDPOINT_OUT)
51 #define STLINK_TRACE_EP (3|ENDPOINT_IN)
52 #define STLINK_SG_SIZE (31)
53 #define STLINK_DATA_SIZE (4096)
54 #define STLINK_CMD_SIZE_V2 (16)
55 #define STLINK_CMD_SIZE_V1 (10)
57 /* the current implementation of the stlink limits
58 * 8bit read/writes to max 64 bytes. */
59 #define STLINK_MAX_RW8 (64)
61 enum stlink_jtag_api_version {
62 STLINK_JTAG_API_V1 = 1,
67 struct stlink_usb_version {
74 /** highest supported jtag api version */
75 enum stlink_jtag_api_version jtag_api_max;
79 struct stlink_usb_handle_s {
81 struct jtag_libusb_device_handle *fd;
83 struct libusb_transfer *trans;
85 uint8_t cmdbuf[STLINK_SG_SIZE];
91 uint8_t databuf[STLINK_DATA_SIZE];
93 uint32_t max_mem_packet;
95 enum hl_transports transport;
97 struct stlink_usb_version version;
102 /** this is the currently used jtag api */
103 enum stlink_jtag_api_version jtag_api;
106 /** whether SWO tracing is enabled or not */
108 /** trace data destination file */
110 /** trace module source clock (for prescaler) */
112 /** trace module clock prescaler */
117 #define STLINK_DEBUG_ERR_OK 0x80
118 #define STLINK_DEBUG_ERR_FAULT 0x81
119 #define STLINK_SWD_AP_WAIT 0x10
120 #define STLINK_SWD_DP_WAIT 0x14
122 #define STLINK_CORE_RUNNING 0x80
123 #define STLINK_CORE_HALTED 0x81
124 #define STLINK_CORE_STAT_UNKNOWN -1
126 #define STLINK_GET_VERSION 0xF1
127 #define STLINK_DEBUG_COMMAND 0xF2
128 #define STLINK_DFU_COMMAND 0xF3
129 #define STLINK_SWIM_COMMAND 0xF4
130 #define STLINK_GET_CURRENT_MODE 0xF5
131 #define STLINK_GET_TARGET_VOLTAGE 0xF7
133 #define STLINK_DEV_DFU_MODE 0x00
134 #define STLINK_DEV_MASS_MODE 0x01
135 #define STLINK_DEV_DEBUG_MODE 0x02
136 #define STLINK_DEV_SWIM_MODE 0x03
137 #define STLINK_DEV_BOOTLOADER_MODE 0x04
138 #define STLINK_DEV_UNKNOWN_MODE -1
140 #define STLINK_DFU_EXIT 0x07
142 #define STLINK_SWIM_ENTER 0x00
143 #define STLINK_SWIM_EXIT 0x01
145 #define STLINK_DEBUG_ENTER_JTAG 0x00
146 #define STLINK_DEBUG_GETSTATUS 0x01
147 #define STLINK_DEBUG_FORCEDEBUG 0x02
148 #define STLINK_DEBUG_APIV1_RESETSYS 0x03
149 #define STLINK_DEBUG_APIV1_READALLREGS 0x04
150 #define STLINK_DEBUG_APIV1_READREG 0x05
151 #define STLINK_DEBUG_APIV1_WRITEREG 0x06
152 #define STLINK_DEBUG_READMEM_32BIT 0x07
153 #define STLINK_DEBUG_WRITEMEM_32BIT 0x08
154 #define STLINK_DEBUG_RUNCORE 0x09
155 #define STLINK_DEBUG_STEPCORE 0x0a
156 #define STLINK_DEBUG_APIV1_SETFP 0x0b
157 #define STLINK_DEBUG_READMEM_8BIT 0x0c
158 #define STLINK_DEBUG_WRITEMEM_8BIT 0x0d
159 #define STLINK_DEBUG_APIV1_CLEARFP 0x0e
160 #define STLINK_DEBUG_APIV1_WRITEDEBUGREG 0x0f
161 #define STLINK_DEBUG_APIV1_SETWATCHPOINT 0x10
163 #define STLINK_DEBUG_ENTER_JTAG 0x00
164 #define STLINK_DEBUG_ENTER_SWD 0xa3
166 #define STLINK_DEBUG_APIV1_ENTER 0x20
167 #define STLINK_DEBUG_EXIT 0x21
168 #define STLINK_DEBUG_READCOREID 0x22
170 #define STLINK_DEBUG_APIV2_ENTER 0x30
171 #define STLINK_DEBUG_APIV2_READ_IDCODES 0x31
172 #define STLINK_DEBUG_APIV2_RESETSYS 0x32
173 #define STLINK_DEBUG_APIV2_READREG 0x33
174 #define STLINK_DEBUG_APIV2_WRITEREG 0x34
175 #define STLINK_DEBUG_APIV2_WRITEDEBUGREG 0x35
176 #define STLINK_DEBUG_APIV2_READDEBUGREG 0x36
178 #define STLINK_DEBUG_APIV2_READALLREGS 0x3A
179 #define STLINK_DEBUG_APIV2_GETLASTRWSTATUS 0x3B
180 #define STLINK_DEBUG_APIV2_DRIVE_NRST 0x3C
182 #define STLINK_DEBUG_APIV2_START_TRACE_RX 0x40
183 #define STLINK_DEBUG_APIV2_STOP_TRACE_RX 0x41
184 #define STLINK_DEBUG_APIV2_GET_TRACE_NB 0x42
186 #define STLINK_DEBUG_APIV2_DRIVE_NRST_LOW 0x00
187 #define STLINK_DEBUG_APIV2_DRIVE_NRST_HIGH 0x01
188 #define STLINK_DEBUG_APIV2_DRIVE_NRST_PULSE 0x02
190 #define STLINK_TRACE_SIZE 1024
191 #define STLINK_TRACE_MAX_HZ 2000000
192 #define STLINK_TRACE_MIN_VERSION 13
196 STLINK_MODE_UNKNOWN = 0,
199 STLINK_MODE_DEBUG_JTAG,
200 STLINK_MODE_DEBUG_SWD,
201 STLINK_MODE_DEBUG_SWIM
204 #define REQUEST_SENSE 0x03
205 #define REQUEST_SENSE_LENGTH 18
207 static void stlink_usb_init_buffer(void *handle, uint8_t direction, uint32_t size);
210 static int stlink_usb_xfer_v1_get_status(void *handle)
212 struct stlink_usb_handle_s *h = handle;
214 assert(handle != NULL);
217 memset(h->cmdbuf, 0, STLINK_SG_SIZE);
219 if (jtag_libusb_bulk_read(h->fd, STLINK_RX_EP, (char *)h->cmdbuf,
220 13, STLINK_READ_TIMEOUT) != 13)
225 t1 = buf_get_u32(h->cmdbuf, 0, 32);
228 if (t1 != 0x53425355)
236 if (h->cmdbuf[12] != 0)
243 static int stlink_usb_xfer_rw(void *handle, int cmdsize, const uint8_t *buf, int size)
245 struct stlink_usb_handle_s *h = handle;
247 assert(handle != NULL);
249 if (jtag_libusb_bulk_write(h->fd, STLINK_TX_EP, (char *)h->cmdbuf, cmdsize,
250 STLINK_WRITE_TIMEOUT) != cmdsize) {
254 if (h->direction == STLINK_TX_EP && size) {
255 if (jtag_libusb_bulk_write(h->fd, STLINK_TX_EP, (char *)buf,
256 size, STLINK_WRITE_TIMEOUT) != size) {
257 LOG_DEBUG("bulk write failed");
260 } else if (h->direction == STLINK_RX_EP && size) {
261 if (jtag_libusb_bulk_read(h->fd, STLINK_RX_EP, (char *)buf,
262 size, STLINK_READ_TIMEOUT) != size) {
263 LOG_DEBUG("bulk read failed");
272 static int stlink_usb_xfer_v1_get_sense(void *handle)
275 struct stlink_usb_handle_s *h = handle;
277 assert(handle != NULL);
279 stlink_usb_init_buffer(handle, STLINK_RX_EP, 16);
281 h->cmdbuf[h->cmdidx++] = REQUEST_SENSE;
282 h->cmdbuf[h->cmdidx++] = 0;
283 h->cmdbuf[h->cmdidx++] = 0;
284 h->cmdbuf[h->cmdidx++] = 0;
285 h->cmdbuf[h->cmdidx++] = REQUEST_SENSE_LENGTH;
287 res = stlink_usb_xfer_rw(handle, REQUEST_SENSE_LENGTH, h->databuf, 16);
292 if (stlink_usb_xfer_v1_get_status(handle) != ERROR_OK)
299 static int stlink_usb_xfer(void *handle, const uint8_t *buf, int size)
301 int err, cmdsize = STLINK_CMD_SIZE_V2;
302 struct stlink_usb_handle_s *h = handle;
304 assert(handle != NULL);
306 if (h->version.stlink == 1)
307 cmdsize = STLINK_SG_SIZE;
309 err = stlink_usb_xfer_rw(handle, cmdsize, buf, size);
314 if (h->version.stlink == 1) {
315 if (stlink_usb_xfer_v1_get_status(handle) != ERROR_OK) {
316 /* check csw status */
317 if (h->cmdbuf[12] == 1) {
318 LOG_DEBUG("get sense");
319 if (stlink_usb_xfer_v1_get_sense(handle) != ERROR_OK)
330 static int stlink_usb_read_trace(void *handle, const uint8_t *buf, int size)
332 struct stlink_usb_handle_s *h = handle;
334 assert(handle != NULL);
336 assert(h->version.stlink >= 2);
338 if (jtag_libusb_bulk_read(h->fd, STLINK_TRACE_EP, (char *)buf,
339 size, STLINK_READ_TIMEOUT) != size) {
340 LOG_ERROR("bulk trace read failed");
348 static void stlink_usb_xfer_v1_create_cmd(void *handle, uint8_t direction, uint32_t size)
350 struct stlink_usb_handle_s *h = handle;
352 /* fill the send buffer */
353 strcpy((char *)h->cmdbuf, "USBC");
355 /* csw tag not used */
357 buf_set_u32(h->cmdbuf+h->cmdidx, 0, 32, size);
359 h->cmdbuf[h->cmdidx++] = (direction == STLINK_RX_EP ? ENDPOINT_IN : ENDPOINT_OUT);
360 h->cmdbuf[h->cmdidx++] = 0; /* lun */
361 h->cmdbuf[h->cmdidx++] = STLINK_CMD_SIZE_V1;
365 static void stlink_usb_init_buffer(void *handle, uint8_t direction, uint32_t size)
367 struct stlink_usb_handle_s *h = handle;
369 h->direction = direction;
373 memset(h->cmdbuf, 0, STLINK_SG_SIZE);
374 memset(h->databuf, 0, STLINK_DATA_SIZE);
376 if (h->version.stlink == 1)
377 stlink_usb_xfer_v1_create_cmd(handle, direction, size);
380 static const char * const stlink_usb_error_msg[] = {
385 static int stlink_usb_error_check(void *handle)
388 const char *err_msg = 0;
389 struct stlink_usb_handle_s *h = handle;
391 assert(handle != NULL);
393 /* TODO: no error checking yet on api V1 */
394 if (h->jtag_api == STLINK_JTAG_API_V1)
395 h->databuf[0] = STLINK_DEBUG_ERR_OK;
397 switch (h->databuf[0]) {
398 case STLINK_DEBUG_ERR_OK:
401 case STLINK_DEBUG_ERR_FAULT:
403 err_msg = stlink_usb_error_msg[0];
409 LOG_DEBUG("status error: %d ('%s')", h->databuf[0], err_msg);
415 static int stlink_usb_version(void *handle)
419 struct stlink_usb_handle_s *h = handle;
421 assert(handle != NULL);
423 stlink_usb_init_buffer(handle, STLINK_RX_EP, 6);
425 h->cmdbuf[h->cmdidx++] = STLINK_GET_VERSION;
427 res = stlink_usb_xfer(handle, h->databuf, 6);
432 v = (h->databuf[0] << 8) | h->databuf[1];
434 h->version.stlink = (v >> 12) & 0x0f;
435 h->version.jtag = (v >> 6) & 0x3f;
436 h->version.swim = v & 0x3f;
437 h->vid = buf_get_u32(h->databuf, 16, 16);
438 h->pid = buf_get_u32(h->databuf, 32, 16);
440 /* set the supported jtag api version
441 * API V2 is supported since JTAG V11
443 if (h->version.jtag >= 11)
444 h->version.jtag_api_max = STLINK_JTAG_API_V2;
446 h->version.jtag_api_max = STLINK_JTAG_API_V1;
448 LOG_INFO("STLINK v%d JTAG v%d API v%d SWIM v%d VID 0x%04X PID 0x%04X",
451 (h->version.jtag_api_max == STLINK_JTAG_API_V1) ? 1 : 2,
459 static int stlink_usb_check_voltage(void *handle, float *target_voltage)
461 struct stlink_usb_handle_s *h = handle;
462 uint32_t adc_results[2];
464 /* only supported by stlink/v2 and for firmware >= 13 */
465 if (h->version.stlink == 1 || h->version.jtag < 13)
466 return ERROR_COMMAND_NOTFOUND;
468 stlink_usb_init_buffer(handle, STLINK_RX_EP, 8);
470 h->cmdbuf[h->cmdidx++] = STLINK_GET_TARGET_VOLTAGE;
472 int result = stlink_usb_xfer(handle, h->databuf, 8);
474 if (result != ERROR_OK)
478 adc_results[0] = le_to_h_u32(h->databuf);
479 adc_results[1] = le_to_h_u32(h->databuf + 4);
484 *target_voltage = 2 * ((float)adc_results[1]) * (float)(1.2 / adc_results[0]);
486 LOG_INFO("Target voltage: %f", (double)*target_voltage);
492 static int stlink_usb_current_mode(void *handle, uint8_t *mode)
495 struct stlink_usb_handle_s *h = handle;
497 assert(handle != NULL);
499 stlink_usb_init_buffer(handle, STLINK_RX_EP, 2);
501 h->cmdbuf[h->cmdidx++] = STLINK_GET_CURRENT_MODE;
503 res = stlink_usb_xfer(handle, h->databuf, 2);
508 *mode = h->databuf[0];
514 static int stlink_usb_mode_enter(void *handle, enum stlink_mode type)
518 struct stlink_usb_handle_s *h = handle;
520 assert(handle != NULL);
522 /* on api V2 we are able the read the latest command
524 * TODO: we need the test on api V1 too
526 if (h->jtag_api == STLINK_JTAG_API_V2)
529 stlink_usb_init_buffer(handle, STLINK_RX_EP, rx_size);
532 case STLINK_MODE_DEBUG_JTAG:
533 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
534 if (h->jtag_api == STLINK_JTAG_API_V1)
535 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_ENTER;
537 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_ENTER;
538 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_ENTER_JTAG;
540 case STLINK_MODE_DEBUG_SWD:
541 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
542 if (h->jtag_api == STLINK_JTAG_API_V1)
543 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_ENTER;
545 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_ENTER;
546 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_ENTER_SWD;
548 case STLINK_MODE_DEBUG_SWIM:
549 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_COMMAND;
550 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_ENTER;
552 case STLINK_MODE_DFU:
553 case STLINK_MODE_MASS:
558 res = stlink_usb_xfer(handle, h->databuf, rx_size);
563 res = stlink_usb_error_check(h);
569 static int stlink_usb_mode_leave(void *handle, enum stlink_mode type)
572 struct stlink_usb_handle_s *h = handle;
574 assert(handle != NULL);
576 stlink_usb_init_buffer(handle, STLINK_NULL_EP, 0);
579 case STLINK_MODE_DEBUG_JTAG:
580 case STLINK_MODE_DEBUG_SWD:
581 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
582 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_EXIT;
584 case STLINK_MODE_DEBUG_SWIM:
585 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_COMMAND;
586 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_EXIT;
588 case STLINK_MODE_DFU:
589 h->cmdbuf[h->cmdidx++] = STLINK_DFU_COMMAND;
590 h->cmdbuf[h->cmdidx++] = STLINK_DFU_EXIT;
592 case STLINK_MODE_MASS:
597 res = stlink_usb_xfer(handle, 0, 0);
605 static int stlink_usb_assert_srst(void *handle, int srst);
608 static int stlink_usb_init_mode(void *handle, bool connect_under_reset)
612 enum stlink_mode emode;
613 struct stlink_usb_handle_s *h = handle;
615 assert(handle != NULL);
617 res = stlink_usb_current_mode(handle, &mode);
622 LOG_DEBUG("MODE: 0x%02X", mode);
624 /* try to exit current mode */
626 case STLINK_DEV_DFU_MODE:
627 emode = STLINK_MODE_DFU;
629 case STLINK_DEV_DEBUG_MODE:
630 emode = STLINK_MODE_DEBUG_SWD;
632 case STLINK_DEV_SWIM_MODE:
633 emode = STLINK_MODE_DEBUG_SWIM;
635 case STLINK_DEV_BOOTLOADER_MODE:
636 case STLINK_DEV_MASS_MODE:
638 emode = STLINK_MODE_UNKNOWN;
642 if (emode != STLINK_MODE_UNKNOWN) {
643 res = stlink_usb_mode_leave(handle, emode);
649 res = stlink_usb_current_mode(handle, &mode);
654 /* we check the target voltage here as an aid to debugging connection problems.
655 * the stlink requires the target Vdd to be connected for reliable debugging.
656 * this cmd is supported in all modes except DFU
658 if (mode != STLINK_DEV_DFU_MODE) {
660 float target_voltage;
662 /* check target voltage (if supported) */
663 res = stlink_usb_check_voltage(h, &target_voltage);
665 if (res != ERROR_OK) {
666 if (res != ERROR_COMMAND_NOTFOUND)
667 LOG_ERROR("voltage check failed");
668 /* attempt to continue as it is not a catastrophic failure */
670 /* check for a sensible target voltage, operating range is 1.65-5.5v
671 * according to datasheet */
672 if (target_voltage < 1.5)
673 LOG_ERROR("target voltage may be too low for reliable debugging");
677 LOG_DEBUG("MODE: 0x%02X", mode);
679 /* set selected mode */
680 switch (h->transport) {
681 case HL_TRANSPORT_SWD:
682 emode = STLINK_MODE_DEBUG_SWD;
684 case HL_TRANSPORT_JTAG:
685 emode = STLINK_MODE_DEBUG_JTAG;
687 case HL_TRANSPORT_SWIM:
688 emode = STLINK_MODE_DEBUG_SWIM;
691 emode = STLINK_MODE_UNKNOWN;
695 if (emode == STLINK_MODE_UNKNOWN) {
696 LOG_ERROR("selected mode (transport) not supported");
700 if (connect_under_reset) {
701 res = stlink_usb_assert_srst(handle, 0);
706 res = stlink_usb_mode_enter(handle, emode);
711 res = stlink_usb_current_mode(handle, &mode);
716 LOG_DEBUG("MODE: 0x%02X", mode);
722 static int stlink_usb_idcode(void *handle, uint32_t *idcode)
725 struct stlink_usb_handle_s *h = handle;
727 assert(handle != NULL);
729 stlink_usb_init_buffer(handle, STLINK_RX_EP, 4);
731 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
732 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_READCOREID;
734 res = stlink_usb_xfer(handle, h->databuf, 4);
739 *idcode = le_to_h_u32(h->databuf);
741 LOG_DEBUG("IDCODE: 0x%08X", *idcode);
746 static int stlink_usb_v2_read_debug_reg(void *handle, uint32_t addr, uint32_t *val)
748 struct stlink_usb_handle_s *h = handle;
751 assert(handle != NULL);
753 stlink_usb_init_buffer(handle, STLINK_RX_EP, 8);
755 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
756 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_READDEBUGREG;
757 h_u32_to_le(h->cmdbuf+h->cmdidx, addr);
760 res = stlink_usb_xfer(handle, h->databuf, 8);
765 *val = le_to_h_u32(h->databuf + 4);
767 return h->databuf[0] == STLINK_DEBUG_ERR_OK ? ERROR_OK : ERROR_FAIL;
770 static int stlink_usb_write_debug_reg(void *handle, uint32_t addr, uint32_t val)
773 struct stlink_usb_handle_s *h = handle;
775 assert(handle != NULL);
777 stlink_usb_init_buffer(handle, STLINK_RX_EP, 2);
779 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
780 if (h->jtag_api == STLINK_JTAG_API_V1)
781 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_WRITEDEBUGREG;
783 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_WRITEDEBUGREG;
784 h_u32_to_le(h->cmdbuf+h->cmdidx, addr);
786 h_u32_to_le(h->cmdbuf+h->cmdidx, val);
789 res = stlink_usb_xfer(handle, h->databuf, 2);
794 return h->databuf[0] == STLINK_DEBUG_ERR_OK ? ERROR_OK : ERROR_FAIL;
798 static void stlink_usb_trace_read(void *handle)
800 struct stlink_usb_handle_s *h = handle;
802 assert(handle != NULL);
804 if (h->trace.enabled && h->version.jtag >= STLINK_TRACE_MIN_VERSION) {
807 stlink_usb_init_buffer(handle, STLINK_RX_EP, 10);
809 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
810 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_GET_TRACE_NB;
812 res = stlink_usb_xfer(handle, h->databuf, 2);
813 if (res == ERROR_OK) {
814 uint8_t buf[STLINK_TRACE_SIZE];
815 size_t size = le_to_h_u16(h->databuf);
818 size = size < sizeof(buf) ? size : sizeof(buf) - 1;
820 res = stlink_usb_read_trace(handle, buf, size);
821 if (res == ERROR_OK) {
822 if (h->trace.output_f) {
823 /* Log retrieved trace output */
824 if (fwrite(buf, 1, size, h->trace.output_f) > 0)
825 fflush(h->trace.output_f);
833 static enum target_state stlink_usb_v2_get_status(void *handle)
838 result = stlink_usb_v2_read_debug_reg(handle, DCB_DHCSR, &status);
839 if (result != ERROR_OK)
840 return TARGET_UNKNOWN;
843 return TARGET_HALTED;
844 else if (status & S_RESET_ST)
847 stlink_usb_trace_read(handle);
849 return TARGET_RUNNING;
853 static enum target_state stlink_usb_state(void *handle)
856 struct stlink_usb_handle_s *h = handle;
858 assert(handle != NULL);
860 if (h->jtag_api == STLINK_JTAG_API_V2)
861 return stlink_usb_v2_get_status(handle);
863 stlink_usb_init_buffer(handle, STLINK_RX_EP, 2);
865 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
866 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_GETSTATUS;
868 res = stlink_usb_xfer(handle, h->databuf, 2);
871 return TARGET_UNKNOWN;
873 if (h->databuf[0] == STLINK_CORE_RUNNING)
874 return TARGET_RUNNING;
875 if (h->databuf[0] == STLINK_CORE_HALTED)
876 return TARGET_HALTED;
878 return TARGET_UNKNOWN;
882 static int stlink_usb_reset(void *handle)
885 struct stlink_usb_handle_s *h = handle;
887 assert(handle != NULL);
889 stlink_usb_init_buffer(handle, STLINK_RX_EP, 2);
891 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
893 if (h->jtag_api == STLINK_JTAG_API_V1)
894 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_RESETSYS;
896 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_RESETSYS;
898 res = stlink_usb_xfer(handle, h->databuf, 2);
903 LOG_DEBUG("RESET: 0x%08X", h->databuf[0]);
905 /* the following is not a error under swd (using hardware srst), so return success */
906 if (h->databuf[0] == STLINK_SWD_AP_WAIT || h->databuf[0] == STLINK_SWD_DP_WAIT)
909 return h->databuf[0] == STLINK_DEBUG_ERR_OK ? ERROR_OK : ERROR_FAIL;
912 static int stlink_usb_assert_srst(void *handle, int srst)
915 struct stlink_usb_handle_s *h = handle;
917 assert(handle != NULL);
919 if (h->jtag_api == STLINK_JTAG_API_V1)
920 return ERROR_COMMAND_NOTFOUND;
922 stlink_usb_init_buffer(handle, STLINK_RX_EP, 2);
924 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
925 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_DRIVE_NRST;
926 h->cmdbuf[h->cmdidx++] = srst;
928 res = stlink_usb_xfer(handle, h->databuf, 2);
933 return h->databuf[0] == STLINK_DEBUG_ERR_OK ? ERROR_OK : ERROR_FAIL;
937 static int stlink_configure_target_trace_port(void *handle)
941 struct stlink_usb_handle_s *h = handle;
943 assert(handle != NULL);
945 /* configure the TPI */
947 /* enable the trace subsystem */
948 res = stlink_usb_v2_read_debug_reg(handle, DCB_DEMCR, ®);
951 res = stlink_usb_write_debug_reg(handle, DCB_DEMCR, TRCENA|reg);
954 /* set the TPI clock prescaler */
955 res = stlink_usb_write_debug_reg(handle, TPI_ACPR, h->trace.prescale);
958 /* select the pin protocol. The STLinkv2 only supports asynchronous
959 * UART emulation (NRZ) mode, so that's what we pick. */
960 res = stlink_usb_write_debug_reg(handle, TPI_SPPR, 0x02);
963 /* disable continuous formatting */
964 res = stlink_usb_write_debug_reg(handle, TPI_FFCR, (1<<8));
968 /* configure the ITM */
970 /* unlock access to the ITM registers */
971 res = stlink_usb_write_debug_reg(handle, ITM_LAR, 0xC5ACCE55);
974 /* enable trace with ATB ID 1 */
975 res = stlink_usb_write_debug_reg(handle, ITM_TCR, (1<<16)|(1<<0)|(1<<2));
978 /* trace privilege */
979 res = stlink_usb_write_debug_reg(handle, ITM_TPR, 1);
982 /* trace port enable (port 0) */
983 res = stlink_usb_write_debug_reg(handle, ITM_TER, (1<<0));
993 static void stlink_usb_trace_disable(void *handle)
996 struct stlink_usb_handle_s *h = handle;
998 assert(handle != NULL);
1000 assert(h->version.jtag >= STLINK_TRACE_MIN_VERSION);
1002 LOG_DEBUG("Tracing: disable\n");
1004 stlink_usb_init_buffer(handle, STLINK_RX_EP, 2);
1005 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1006 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_STOP_TRACE_RX;
1007 res = stlink_usb_xfer(handle, h->databuf, 2);
1009 if (res == ERROR_OK)
1010 h->trace.enabled = false;
1015 static int stlink_usb_trace_enable(void *handle)
1018 struct stlink_usb_handle_s *h = handle;
1020 assert(handle != NULL);
1022 if (h->version.jtag >= STLINK_TRACE_MIN_VERSION) {
1025 res = stlink_configure_target_trace_port(handle);
1026 if (res != ERROR_OK)
1027 LOG_ERROR("Unable to configure tracing on target\n");
1029 trace_hz = h->trace.prescale > 0 ?
1030 h->trace.source_hz / (h->trace.prescale + 1) :
1033 stlink_usb_init_buffer(handle, STLINK_RX_EP, 10);
1035 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1036 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_START_TRACE_RX;
1037 h_u16_to_le(h->cmdbuf+h->cmdidx, (uint16_t)STLINK_TRACE_SIZE);
1039 h_u32_to_le(h->cmdbuf+h->cmdidx, trace_hz);
1042 res = stlink_usb_xfer(handle, h->databuf, 2);
1044 if (res == ERROR_OK) {
1045 h->trace.enabled = true;
1046 LOG_DEBUG("Tracing: recording at %uHz\n", trace_hz);
1049 LOG_ERROR("Tracing is not supported by this version.");
1057 static int stlink_usb_run(void *handle)
1060 struct stlink_usb_handle_s *h = handle;
1062 assert(handle != NULL);
1064 if (h->jtag_api == STLINK_JTAG_API_V2) {
1065 res = stlink_usb_write_debug_reg(handle, DCB_DHCSR, DBGKEY|C_DEBUGEN);
1067 /* Try to start tracing, if requested */
1068 if (res == ERROR_OK && h->trace.source_hz) {
1069 if (stlink_usb_trace_enable(handle) == ERROR_OK)
1070 LOG_DEBUG("Tracing: enabled\n");
1072 LOG_ERROR("Tracing: enable failed\n");
1078 stlink_usb_init_buffer(handle, STLINK_RX_EP, 2);
1080 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1081 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_RUNCORE;
1083 res = stlink_usb_xfer(handle, h->databuf, 2);
1085 if (res != ERROR_OK)
1088 return h->databuf[0] == STLINK_DEBUG_ERR_OK ? ERROR_OK : ERROR_FAIL;
1092 static int stlink_usb_halt(void *handle)
1095 struct stlink_usb_handle_s *h = handle;
1097 assert(handle != NULL);
1099 if (h->jtag_api == STLINK_JTAG_API_V2) {
1100 res = stlink_usb_write_debug_reg(handle, DCB_DHCSR, DBGKEY|C_HALT|C_DEBUGEN);
1102 if (res == ERROR_OK && h->trace.enabled)
1103 stlink_usb_trace_disable(handle);
1108 stlink_usb_init_buffer(handle, STLINK_RX_EP, 2);
1110 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1111 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_FORCEDEBUG;
1113 res = stlink_usb_xfer(handle, h->databuf, 2);
1115 if (res != ERROR_OK)
1118 return h->databuf[0] == STLINK_DEBUG_ERR_OK ? ERROR_OK : ERROR_FAIL;
1122 static int stlink_usb_step(void *handle)
1125 struct stlink_usb_handle_s *h = handle;
1127 assert(handle != NULL);
1129 if (h->jtag_api == STLINK_JTAG_API_V2) {
1130 /* TODO: this emulates the v1 api, it should really use a similar auto mask isr
1131 * that the cortex-m3 currently does. */
1132 stlink_usb_write_debug_reg(handle, DCB_DHCSR, DBGKEY|C_HALT|C_MASKINTS|C_DEBUGEN);
1133 stlink_usb_write_debug_reg(handle, DCB_DHCSR, DBGKEY|C_STEP|C_MASKINTS|C_DEBUGEN);
1134 return stlink_usb_write_debug_reg(handle, DCB_DHCSR, DBGKEY|C_HALT|C_DEBUGEN);
1137 stlink_usb_init_buffer(handle, STLINK_RX_EP, 2);
1139 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1140 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_STEPCORE;
1142 res = stlink_usb_xfer(handle, h->databuf, 2);
1144 if (res != ERROR_OK)
1147 return h->databuf[0] == STLINK_DEBUG_ERR_OK ? ERROR_OK : ERROR_FAIL;
1151 static int stlink_usb_read_regs(void *handle)
1154 struct stlink_usb_handle_s *h = handle;
1156 assert(handle != NULL);
1158 stlink_usb_init_buffer(handle, STLINK_RX_EP, 84);
1160 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1161 if (h->jtag_api == STLINK_JTAG_API_V1)
1162 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_READALLREGS;
1164 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_READALLREGS;
1166 res = stlink_usb_xfer(handle, h->databuf, 84);
1168 if (res != ERROR_OK)
1175 static int stlink_usb_read_reg(void *handle, int num, uint32_t *val)
1178 struct stlink_usb_handle_s *h = handle;
1180 assert(handle != NULL);
1182 stlink_usb_init_buffer(handle, STLINK_RX_EP, h->jtag_api == STLINK_JTAG_API_V1 ? 4 : 8);
1184 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1185 if (h->jtag_api == STLINK_JTAG_API_V1)
1186 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_READREG;
1188 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_READREG;
1189 h->cmdbuf[h->cmdidx++] = num;
1191 res = stlink_usb_xfer(handle, h->databuf, h->jtag_api == STLINK_JTAG_API_V1 ? 4 : 8);
1193 if (res != ERROR_OK)
1196 if (h->jtag_api == STLINK_JTAG_API_V1)
1197 *val = le_to_h_u32(h->databuf);
1199 *val = le_to_h_u32(h->databuf + 4);
1200 return h->databuf[0] == STLINK_DEBUG_ERR_OK ? ERROR_OK : ERROR_FAIL;
1207 static int stlink_usb_write_reg(void *handle, int num, uint32_t val)
1210 struct stlink_usb_handle_s *h = handle;
1212 assert(handle != NULL);
1214 stlink_usb_init_buffer(handle, STLINK_RX_EP, 2);
1216 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1217 if (h->jtag_api == STLINK_JTAG_API_V1)
1218 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_WRITEREG;
1220 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_WRITEREG;
1221 h->cmdbuf[h->cmdidx++] = num;
1222 h_u32_to_le(h->cmdbuf+h->cmdidx, val);
1225 res = stlink_usb_xfer(handle, h->databuf, 2);
1227 if (res != ERROR_OK)
1230 return h->databuf[0] == STLINK_DEBUG_ERR_OK ? ERROR_OK : ERROR_FAIL;
1233 static int stlink_usb_get_rw_status(void *handle)
1236 struct stlink_usb_handle_s *h = handle;
1238 assert(handle != NULL);
1240 if (h->jtag_api == STLINK_JTAG_API_V1)
1243 stlink_usb_init_buffer(handle, STLINK_RX_EP, 2);
1245 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1246 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_GETLASTRWSTATUS;
1248 res = stlink_usb_xfer(handle, h->databuf, 2);
1250 if (res != ERROR_OK)
1253 return h->databuf[0] == STLINK_DEBUG_ERR_OK ? ERROR_OK : res;
1257 static int stlink_usb_read_mem8(void *handle, uint32_t addr, uint16_t len,
1261 uint16_t read_len = len;
1262 struct stlink_usb_handle_s *h = handle;
1264 assert(handle != NULL);
1266 /* max 8bit read/write is 64bytes */
1267 if (len > STLINK_MAX_RW8) {
1268 LOG_DEBUG("max buffer length exceeded");
1272 stlink_usb_init_buffer(handle, STLINK_RX_EP, read_len);
1274 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1275 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_READMEM_8BIT;
1276 h_u32_to_le(h->cmdbuf+h->cmdidx, addr);
1278 h_u16_to_le(h->cmdbuf+h->cmdidx, len);
1281 /* we need to fix read length for single bytes */
1285 res = stlink_usb_xfer(handle, h->databuf, read_len);
1287 if (res != ERROR_OK)
1290 memcpy(buffer, h->databuf, len);
1292 return stlink_usb_get_rw_status(handle);
1296 static int stlink_usb_write_mem8(void *handle, uint32_t addr, uint16_t len,
1297 const uint8_t *buffer)
1300 struct stlink_usb_handle_s *h = handle;
1302 assert(handle != NULL);
1304 /* max 8bit read/write is 64bytes */
1305 if (len > STLINK_MAX_RW8) {
1306 LOG_DEBUG("max buffer length exceeded");
1310 stlink_usb_init_buffer(handle, STLINK_TX_EP, len);
1312 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1313 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_WRITEMEM_8BIT;
1314 h_u32_to_le(h->cmdbuf+h->cmdidx, addr);
1316 h_u16_to_le(h->cmdbuf+h->cmdidx, len);
1319 res = stlink_usb_xfer(handle, buffer, len);
1321 if (res != ERROR_OK)
1324 return stlink_usb_get_rw_status(handle);
1328 static int stlink_usb_read_mem32(void *handle, uint32_t addr, uint16_t len,
1332 struct stlink_usb_handle_s *h = handle;
1334 assert(handle != NULL);
1336 /* data must be a multiple of 4 and word aligned */
1337 if (len % 4 || addr % 4) {
1338 LOG_DEBUG("Invalid data alignment");
1339 return ERROR_TARGET_UNALIGNED_ACCESS;
1342 stlink_usb_init_buffer(handle, STLINK_RX_EP, len);
1344 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1345 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_READMEM_32BIT;
1346 h_u32_to_le(h->cmdbuf+h->cmdidx, addr);
1348 h_u16_to_le(h->cmdbuf+h->cmdidx, len);
1351 res = stlink_usb_xfer(handle, h->databuf, len);
1353 if (res != ERROR_OK)
1356 memcpy(buffer, h->databuf, len);
1358 return stlink_usb_get_rw_status(handle);
1362 static int stlink_usb_write_mem32(void *handle, uint32_t addr, uint16_t len,
1363 const uint8_t *buffer)
1366 struct stlink_usb_handle_s *h = handle;
1368 assert(handle != NULL);
1370 /* data must be a multiple of 4 and word aligned */
1371 if (len % 4 || addr % 4) {
1372 LOG_DEBUG("Invalid data alignment");
1373 return ERROR_TARGET_UNALIGNED_ACCESS;
1376 stlink_usb_init_buffer(handle, STLINK_TX_EP, len);
1378 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1379 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_WRITEMEM_32BIT;
1380 h_u32_to_le(h->cmdbuf+h->cmdidx, addr);
1382 h_u16_to_le(h->cmdbuf+h->cmdidx, len);
1385 res = stlink_usb_xfer(handle, buffer, len);
1387 if (res != ERROR_OK)
1390 return stlink_usb_get_rw_status(handle);
1393 static uint32_t stlink_max_block_size(uint32_t tar_autoincr_block, uint32_t address)
1395 uint32_t max_tar_block = (tar_autoincr_block - ((tar_autoincr_block - 1) & address));
1396 if (max_tar_block == 0)
1398 return max_tar_block;
1401 static int stlink_usb_read_mem(void *handle, uint32_t addr, uint32_t size,
1402 uint32_t count, uint8_t *buffer)
1404 int retval = ERROR_OK;
1405 uint32_t bytes_remaining;
1406 struct stlink_usb_handle_s *h = handle;
1408 /* calculate byte count */
1413 bytes_remaining = (size == 4) ? \
1414 stlink_max_block_size(h->max_mem_packet, addr) : STLINK_MAX_RW8;
1416 if (count < bytes_remaining)
1417 bytes_remaining = count;
1419 /* the stlink only supports 8/32bit memory read/writes
1420 * honour 32bit, all others will be handled as 8bit access */
1423 /* When in jtag mode the stlink uses the auto-increment functinality.
1424 * However it expects us to pass the data correctly, this includes
1425 * alignment and any page boundaries. We already do this as part of the
1426 * adi_v5 implementation, but the stlink is a hla adapter and so this
1427 * needs implementiong manually.
1428 * currently this only affects jtag mode, according to ST they do single
1429 * access in SWD mode - but this may change and so we do it for both modes */
1431 /* we first need to check for any unaligned bytes */
1434 uint32_t head_bytes = 4 - (addr % 4);
1435 retval = stlink_usb_read_mem8(handle, addr, head_bytes, buffer);
1436 if (retval != ERROR_OK)
1438 buffer += head_bytes;
1440 count -= head_bytes;
1441 bytes_remaining -= head_bytes;
1444 if (bytes_remaining % 4)
1445 retval = stlink_usb_read_mem(handle, addr, 1, bytes_remaining, buffer);
1447 retval = stlink_usb_read_mem32(handle, addr, bytes_remaining, buffer);
1449 retval = stlink_usb_read_mem8(handle, addr, bytes_remaining, buffer);
1451 if (retval != ERROR_OK)
1454 buffer += bytes_remaining;
1455 addr += bytes_remaining;
1456 count -= bytes_remaining;
1462 static int stlink_usb_write_mem(void *handle, uint32_t addr, uint32_t size,
1463 uint32_t count, const uint8_t *buffer)
1465 int retval = ERROR_OK;
1466 uint32_t bytes_remaining;
1467 struct stlink_usb_handle_s *h = handle;
1469 /* calculate byte count */
1474 bytes_remaining = (size == 4) ? \
1475 stlink_max_block_size(h->max_mem_packet, addr) : STLINK_MAX_RW8;
1477 if (count < bytes_remaining)
1478 bytes_remaining = count;
1480 /* the stlink only supports 8/32bit memory read/writes
1481 * honour 32bit, all others will be handled as 8bit access */
1484 /* When in jtag mode the stlink uses the auto-increment functinality.
1485 * However it expects us to pass the data correctly, this includes
1486 * alignment and any page boundaries. We already do this as part of the
1487 * adi_v5 implementation, but the stlink is a hla adapter and so this
1488 * needs implementiong manually.
1489 * currently this only affects jtag mode, according to ST they do single
1490 * access in SWD mode - but this may change and so we do it for both modes */
1492 /* we first need to check for any unaligned bytes */
1495 uint32_t head_bytes = 4 - (addr % 4);
1496 retval = stlink_usb_write_mem8(handle, addr, head_bytes, buffer);
1497 if (retval != ERROR_OK)
1499 buffer += head_bytes;
1501 count -= head_bytes;
1502 bytes_remaining -= head_bytes;
1505 if (bytes_remaining % 4)
1506 retval = stlink_usb_write_mem(handle, addr, 1, bytes_remaining, buffer);
1508 retval = stlink_usb_write_mem32(handle, addr, bytes_remaining, buffer);
1511 retval = stlink_usb_write_mem8(handle, addr, bytes_remaining, buffer);
1512 if (retval != ERROR_OK)
1515 buffer += bytes_remaining;
1516 addr += bytes_remaining;
1517 count -= bytes_remaining;
1524 static int stlink_usb_close(void *fd)
1526 struct stlink_usb_handle_s *h = fd;
1529 jtag_libusb_close(h->fd);
1537 static int stlink_usb_open(struct hl_interface_param_s *param, void **fd)
1539 int err, retry_count = 1;
1540 struct stlink_usb_handle_s *h;
1541 enum stlink_jtag_api_version api;
1543 LOG_DEBUG("stlink_usb_open");
1545 h = calloc(1, sizeof(struct stlink_usb_handle_s));
1548 LOG_DEBUG("malloc failed");
1552 h->transport = param->transport;
1554 const uint16_t vids[] = { param->vid, 0 };
1555 const uint16_t pids[] = { param->pid, 0 };
1557 LOG_DEBUG("transport: %d vid: 0x%04x pid: 0x%04x", param->transport,
1558 param->vid, param->pid);
1561 On certain host USB configurations(e.g. MacBook Air)
1562 STLINKv2 dongle seems to have its FW in a funky state if,
1563 after plugging it in, you try to use openocd with it more
1564 then once (by launching and closing openocd). In cases like
1565 that initial attempt to read the FW info via
1566 stlink_usb_version will fail and the device has to be reset
1567 in order to become operational.
1570 if (jtag_libusb_open(vids, pids, &h->fd) != ERROR_OK) {
1571 LOG_ERROR("open failed");
1575 jtag_libusb_set_configuration(h->fd, 0);
1577 if (jtag_libusb_claim_interface(h->fd, 0) != ERROR_OK) {
1578 LOG_DEBUG("claim interface failed");
1582 /* wrap version for first read */
1583 switch (param->pid) {
1585 h->version.stlink = 1;
1588 h->version.stlink = 2;
1592 /* get the device version */
1593 err = stlink_usb_version(h);
1595 if (err == ERROR_OK) {
1597 } else if (h->version.stlink == 1 ||
1599 LOG_ERROR("read version failed");
1602 err = jtag_libusb_release_interface(h->fd, 0);
1603 if (err != ERROR_OK) {
1604 LOG_ERROR("release interface failed");
1608 err = jtag_libusb_reset_device(h->fd);
1609 if (err != ERROR_OK) {
1610 LOG_ERROR("reset device failed");
1614 jtag_libusb_close(h->fd);
1616 Give the device one second to settle down and
1619 usleep(1 * 1000 * 1000);
1624 /* compare usb vid/pid */
1625 if ((param->vid != h->vid) || (param->pid != h->pid))
1626 LOG_INFO("vid/pid are not identical: 0x%04X/0x%04X 0x%04X/0x%04X",
1627 param->vid, param->pid,
1630 /* check if mode is supported */
1633 switch (h->transport) {
1634 case HL_TRANSPORT_SWD:
1635 case HL_TRANSPORT_JTAG:
1636 if (h->version.jtag == 0)
1639 case HL_TRANSPORT_SWIM:
1640 if (h->version.swim == 0)
1648 if (err != ERROR_OK) {
1649 LOG_ERROR("mode (transport) not supported by device");
1653 api = h->version.jtag_api_max;
1655 /* check that user has not requested certain api version
1656 * and if they have check it is supported */
1657 if ((param->api != 0) && (param->api <= h->version.jtag_api_max)) {
1659 LOG_INFO("using stlink api v%d", api);
1662 /* set the used jtag api, this will default to the newest supported version */
1665 if (h->jtag_api >= 2 && param->trace_source_hz > 0) {
1668 prescale = param->trace_source_hz > STLINK_TRACE_MAX_HZ ?
1669 (param->trace_source_hz / STLINK_TRACE_MAX_HZ) - 1 : 0;
1671 h->trace.output_f = param->trace_f;
1672 h->trace.source_hz = param->trace_source_hz;
1673 h->trace.prescale = prescale;
1676 /* initialize the debug hardware */
1677 err = stlink_usb_init_mode(h, param->connect_under_reset);
1679 if (err != ERROR_OK) {
1680 LOG_ERROR("init mode failed");
1684 /* get cpuid, so we can determine the max page size
1685 * start with a safe default */
1686 h->max_mem_packet = (1 << 10);
1689 err = stlink_usb_read_mem32(h, CPUID, 4, buffer);
1690 if (err == ERROR_OK) {
1691 uint32_t cpuid = le_to_h_u32(buffer);
1692 int i = (cpuid >> 4) & 0xf;
1693 if (i == 4 || i == 3) {
1694 /* Cortex-M3/M4 has 4096 bytes autoincrement range */
1695 h->max_mem_packet = (1 << 12);
1699 LOG_DEBUG("Using TAR autoincrement: %" PRIu32, h->max_mem_packet);
1706 stlink_usb_close(h);
1712 struct hl_layout_api_s stlink_usb_layout_api = {
1714 .open = stlink_usb_open,
1716 .close = stlink_usb_close,
1718 .idcode = stlink_usb_idcode,
1720 .state = stlink_usb_state,
1722 .reset = stlink_usb_reset,
1724 .assert_srst = stlink_usb_assert_srst,
1726 .run = stlink_usb_run,
1728 .halt = stlink_usb_halt,
1730 .step = stlink_usb_step,
1732 .read_regs = stlink_usb_read_regs,
1734 .read_reg = stlink_usb_read_reg,
1736 .write_reg = stlink_usb_write_reg,
1738 .read_mem = stlink_usb_read_mem,
1740 .write_mem = stlink_usb_write_mem,
1742 .write_debug_reg = stlink_usb_write_debug_reg