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)
53 #define STLINK_V2_1_TX_EP (1|ENDPOINT_OUT)
54 #define STLINK_V2_1_TRACE_EP (2|ENDPOINT_IN)
56 #define STLINK_SG_SIZE (31)
57 #define STLINK_DATA_SIZE (4096)
58 #define STLINK_CMD_SIZE_V2 (16)
59 #define STLINK_CMD_SIZE_V1 (10)
61 #define STLINK_V1_PID (0x3744)
62 #define STLINK_V2_PID (0x3748)
63 #define STLINK_V2_1_PID (0x374B)
65 /* the current implementation of the stlink limits
66 * 8bit read/writes to max 64 bytes. */
67 #define STLINK_MAX_RW8 (64)
69 enum stlink_jtag_api_version
{
70 STLINK_JTAG_API_V1
= 1,
75 struct stlink_usb_version
{
82 /** highest supported jtag api version */
83 enum stlink_jtag_api_version jtag_api_max
;
87 struct stlink_usb_handle_s
{
89 struct jtag_libusb_device_handle
*fd
;
91 struct libusb_transfer
*trans
;
99 uint8_t cmdbuf
[STLINK_SG_SIZE
];
105 uint8_t databuf
[STLINK_DATA_SIZE
];
107 uint32_t max_mem_packet
;
109 enum hl_transports transport
;
111 struct stlink_usb_version version
;
116 /** this is the currently used jtag api */
117 enum stlink_jtag_api_version jtag_api
;
120 /** whether SWO tracing is enabled or not */
122 /** trace data destination file */
124 /** trace module source clock (for prescaler) */
126 /** trace module clock prescaler */
129 /** reconnect is needed next time we try to query the
131 bool reconnect_pending
;
134 #define STLINK_DEBUG_ERR_OK 0x80
135 #define STLINK_DEBUG_ERR_FAULT 0x81
136 #define STLINK_SWD_AP_WAIT 0x10
137 #define STLINK_SWD_DP_WAIT 0x14
139 #define STLINK_CORE_RUNNING 0x80
140 #define STLINK_CORE_HALTED 0x81
141 #define STLINK_CORE_STAT_UNKNOWN -1
143 #define STLINK_GET_VERSION 0xF1
144 #define STLINK_DEBUG_COMMAND 0xF2
145 #define STLINK_DFU_COMMAND 0xF3
146 #define STLINK_SWIM_COMMAND 0xF4
147 #define STLINK_GET_CURRENT_MODE 0xF5
148 #define STLINK_GET_TARGET_VOLTAGE 0xF7
150 #define STLINK_DEV_DFU_MODE 0x00
151 #define STLINK_DEV_MASS_MODE 0x01
152 #define STLINK_DEV_DEBUG_MODE 0x02
153 #define STLINK_DEV_SWIM_MODE 0x03
154 #define STLINK_DEV_BOOTLOADER_MODE 0x04
155 #define STLINK_DEV_UNKNOWN_MODE -1
157 #define STLINK_DFU_EXIT 0x07
159 #define STLINK_SWIM_ENTER 0x00
160 #define STLINK_SWIM_EXIT 0x01
162 #define STLINK_DEBUG_ENTER_JTAG 0x00
163 #define STLINK_DEBUG_GETSTATUS 0x01
164 #define STLINK_DEBUG_FORCEDEBUG 0x02
165 #define STLINK_DEBUG_APIV1_RESETSYS 0x03
166 #define STLINK_DEBUG_APIV1_READALLREGS 0x04
167 #define STLINK_DEBUG_APIV1_READREG 0x05
168 #define STLINK_DEBUG_APIV1_WRITEREG 0x06
169 #define STLINK_DEBUG_READMEM_32BIT 0x07
170 #define STLINK_DEBUG_WRITEMEM_32BIT 0x08
171 #define STLINK_DEBUG_RUNCORE 0x09
172 #define STLINK_DEBUG_STEPCORE 0x0a
173 #define STLINK_DEBUG_APIV1_SETFP 0x0b
174 #define STLINK_DEBUG_READMEM_8BIT 0x0c
175 #define STLINK_DEBUG_WRITEMEM_8BIT 0x0d
176 #define STLINK_DEBUG_APIV1_CLEARFP 0x0e
177 #define STLINK_DEBUG_APIV1_WRITEDEBUGREG 0x0f
178 #define STLINK_DEBUG_APIV1_SETWATCHPOINT 0x10
180 #define STLINK_DEBUG_ENTER_JTAG 0x00
181 #define STLINK_DEBUG_ENTER_SWD 0xa3
183 #define STLINK_DEBUG_APIV1_ENTER 0x20
184 #define STLINK_DEBUG_EXIT 0x21
185 #define STLINK_DEBUG_READCOREID 0x22
187 #define STLINK_DEBUG_APIV2_ENTER 0x30
188 #define STLINK_DEBUG_APIV2_READ_IDCODES 0x31
189 #define STLINK_DEBUG_APIV2_RESETSYS 0x32
190 #define STLINK_DEBUG_APIV2_READREG 0x33
191 #define STLINK_DEBUG_APIV2_WRITEREG 0x34
192 #define STLINK_DEBUG_APIV2_WRITEDEBUGREG 0x35
193 #define STLINK_DEBUG_APIV2_READDEBUGREG 0x36
195 #define STLINK_DEBUG_APIV2_READALLREGS 0x3A
196 #define STLINK_DEBUG_APIV2_GETLASTRWSTATUS 0x3B
197 #define STLINK_DEBUG_APIV2_DRIVE_NRST 0x3C
199 #define STLINK_DEBUG_APIV2_START_TRACE_RX 0x40
200 #define STLINK_DEBUG_APIV2_STOP_TRACE_RX 0x41
201 #define STLINK_DEBUG_APIV2_GET_TRACE_NB 0x42
203 #define STLINK_DEBUG_APIV2_DRIVE_NRST_LOW 0x00
204 #define STLINK_DEBUG_APIV2_DRIVE_NRST_HIGH 0x01
205 #define STLINK_DEBUG_APIV2_DRIVE_NRST_PULSE 0x02
207 #define STLINK_TRACE_SIZE 1024
208 #define STLINK_TRACE_MAX_HZ 2000000
209 #define STLINK_TRACE_MIN_VERSION 13
213 STLINK_MODE_UNKNOWN
= 0,
216 STLINK_MODE_DEBUG_JTAG
,
217 STLINK_MODE_DEBUG_SWD
,
218 STLINK_MODE_DEBUG_SWIM
221 #define REQUEST_SENSE 0x03
222 #define REQUEST_SENSE_LENGTH 18
224 static void stlink_usb_init_buffer(void *handle
, uint8_t direction
, uint32_t size
);
227 static int stlink_usb_xfer_v1_get_status(void *handle
)
229 struct stlink_usb_handle_s
*h
= handle
;
231 assert(handle
!= NULL
);
234 memset(h
->cmdbuf
, 0, STLINK_SG_SIZE
);
236 if (jtag_libusb_bulk_read(h
->fd
, h
->rx_ep
, (char *)h
->cmdbuf
,
237 13, STLINK_READ_TIMEOUT
) != 13)
242 t1
= buf_get_u32(h
->cmdbuf
, 0, 32);
245 if (t1
!= 0x53425355)
253 if (h
->cmdbuf
[12] != 0)
260 static int stlink_usb_xfer_rw(void *handle
, int cmdsize
, const uint8_t *buf
, int size
)
262 struct stlink_usb_handle_s
*h
= handle
;
264 assert(handle
!= NULL
);
266 if (jtag_libusb_bulk_write(h
->fd
, h
->tx_ep
, (char *)h
->cmdbuf
, cmdsize
,
267 STLINK_WRITE_TIMEOUT
) != cmdsize
) {
271 if (h
->direction
== h
->tx_ep
&& size
) {
272 if (jtag_libusb_bulk_write(h
->fd
, h
->tx_ep
, (char *)buf
,
273 size
, STLINK_WRITE_TIMEOUT
) != size
) {
274 LOG_DEBUG("bulk write failed");
277 } else if (h
->direction
== h
->rx_ep
&& size
) {
278 if (jtag_libusb_bulk_read(h
->fd
, h
->rx_ep
, (char *)buf
,
279 size
, STLINK_READ_TIMEOUT
) != size
) {
280 LOG_DEBUG("bulk read failed");
289 static int stlink_usb_xfer_v1_get_sense(void *handle
)
292 struct stlink_usb_handle_s
*h
= handle
;
294 assert(handle
!= NULL
);
296 stlink_usb_init_buffer(handle
, h
->rx_ep
, 16);
298 h
->cmdbuf
[h
->cmdidx
++] = REQUEST_SENSE
;
299 h
->cmdbuf
[h
->cmdidx
++] = 0;
300 h
->cmdbuf
[h
->cmdidx
++] = 0;
301 h
->cmdbuf
[h
->cmdidx
++] = 0;
302 h
->cmdbuf
[h
->cmdidx
++] = REQUEST_SENSE_LENGTH
;
304 res
= stlink_usb_xfer_rw(handle
, REQUEST_SENSE_LENGTH
, h
->databuf
, 16);
309 if (stlink_usb_xfer_v1_get_status(handle
) != ERROR_OK
)
316 static int stlink_usb_xfer(void *handle
, const uint8_t *buf
, int size
)
318 int err
, cmdsize
= STLINK_CMD_SIZE_V2
;
319 struct stlink_usb_handle_s
*h
= handle
;
321 assert(handle
!= NULL
);
323 if (h
->version
.stlink
== 1)
324 cmdsize
= STLINK_SG_SIZE
;
326 err
= stlink_usb_xfer_rw(handle
, cmdsize
, buf
, size
);
331 if (h
->version
.stlink
== 1) {
332 if (stlink_usb_xfer_v1_get_status(handle
) != ERROR_OK
) {
333 /* check csw status */
334 if (h
->cmdbuf
[12] == 1) {
335 LOG_DEBUG("get sense");
336 if (stlink_usb_xfer_v1_get_sense(handle
) != ERROR_OK
)
347 static int stlink_usb_read_trace(void *handle
, const uint8_t *buf
, int size
)
349 struct stlink_usb_handle_s
*h
= handle
;
351 assert(handle
!= NULL
);
353 assert(h
->version
.stlink
>= 2);
355 if (jtag_libusb_bulk_read(h
->fd
, h
->trace_ep
, (char *)buf
,
356 size
, STLINK_READ_TIMEOUT
) != size
) {
357 LOG_ERROR("bulk trace read failed");
365 static void stlink_usb_xfer_v1_create_cmd(void *handle
, uint8_t direction
, uint32_t size
)
367 struct stlink_usb_handle_s
*h
= handle
;
369 /* fill the send buffer */
370 strcpy((char *)h
->cmdbuf
, "USBC");
372 /* csw tag not used */
374 buf_set_u32(h
->cmdbuf
+h
->cmdidx
, 0, 32, size
);
376 h
->cmdbuf
[h
->cmdidx
++] = (direction
== h
->rx_ep
? ENDPOINT_IN
: ENDPOINT_OUT
);
377 h
->cmdbuf
[h
->cmdidx
++] = 0; /* lun */
378 h
->cmdbuf
[h
->cmdidx
++] = STLINK_CMD_SIZE_V1
;
382 static void stlink_usb_init_buffer(void *handle
, uint8_t direction
, uint32_t size
)
384 struct stlink_usb_handle_s
*h
= handle
;
386 h
->direction
= direction
;
390 memset(h
->cmdbuf
, 0, STLINK_SG_SIZE
);
391 memset(h
->databuf
, 0, STLINK_DATA_SIZE
);
393 if (h
->version
.stlink
== 1)
394 stlink_usb_xfer_v1_create_cmd(handle
, direction
, size
);
397 static const char * const stlink_usb_error_msg
[] = {
402 static int stlink_usb_error_check(void *handle
)
405 const char *err_msg
= 0;
406 struct stlink_usb_handle_s
*h
= handle
;
408 assert(handle
!= NULL
);
410 /* TODO: no error checking yet on api V1 */
411 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
412 h
->databuf
[0] = STLINK_DEBUG_ERR_OK
;
414 switch (h
->databuf
[0]) {
415 case STLINK_DEBUG_ERR_OK
:
418 case STLINK_DEBUG_ERR_FAULT
:
420 err_msg
= stlink_usb_error_msg
[0];
426 LOG_DEBUG("status error: %d ('%s')", h
->databuf
[0], err_msg
);
432 static int stlink_usb_version(void *handle
)
436 struct stlink_usb_handle_s
*h
= handle
;
438 assert(handle
!= NULL
);
440 stlink_usb_init_buffer(handle
, h
->rx_ep
, 6);
442 h
->cmdbuf
[h
->cmdidx
++] = STLINK_GET_VERSION
;
444 res
= stlink_usb_xfer(handle
, h
->databuf
, 6);
449 v
= (h
->databuf
[0] << 8) | h
->databuf
[1];
451 h
->version
.stlink
= (v
>> 12) & 0x0f;
452 h
->version
.jtag
= (v
>> 6) & 0x3f;
453 h
->version
.swim
= v
& 0x3f;
454 h
->vid
= buf_get_u32(h
->databuf
, 16, 16);
455 h
->pid
= buf_get_u32(h
->databuf
, 32, 16);
457 /* set the supported jtag api version
458 * API V2 is supported since JTAG V11
460 if (h
->version
.jtag
>= 11)
461 h
->version
.jtag_api_max
= STLINK_JTAG_API_V2
;
463 h
->version
.jtag_api_max
= STLINK_JTAG_API_V1
;
465 LOG_INFO("STLINK v%d JTAG v%d API v%d SWIM v%d VID 0x%04X PID 0x%04X",
468 (h
->version
.jtag_api_max
== STLINK_JTAG_API_V1
) ? 1 : 2,
476 static int stlink_usb_check_voltage(void *handle
, float *target_voltage
)
478 struct stlink_usb_handle_s
*h
= handle
;
479 uint32_t adc_results
[2];
481 /* only supported by stlink/v2 and for firmware >= 13 */
482 if (h
->version
.stlink
== 1 || h
->version
.jtag
< 13)
483 return ERROR_COMMAND_NOTFOUND
;
485 stlink_usb_init_buffer(handle
, h
->rx_ep
, 8);
487 h
->cmdbuf
[h
->cmdidx
++] = STLINK_GET_TARGET_VOLTAGE
;
489 int result
= stlink_usb_xfer(handle
, h
->databuf
, 8);
491 if (result
!= ERROR_OK
)
495 adc_results
[0] = le_to_h_u32(h
->databuf
);
496 adc_results
[1] = le_to_h_u32(h
->databuf
+ 4);
501 *target_voltage
= 2 * ((float)adc_results
[1]) * (float)(1.2 / adc_results
[0]);
503 LOG_INFO("Target voltage: %f", (double)*target_voltage
);
509 static int stlink_usb_current_mode(void *handle
, uint8_t *mode
)
512 struct stlink_usb_handle_s
*h
= handle
;
514 assert(handle
!= NULL
);
516 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
518 h
->cmdbuf
[h
->cmdidx
++] = STLINK_GET_CURRENT_MODE
;
520 res
= stlink_usb_xfer(handle
, h
->databuf
, 2);
525 *mode
= h
->databuf
[0];
531 static int stlink_usb_mode_enter(void *handle
, enum stlink_mode type
)
535 struct stlink_usb_handle_s
*h
= handle
;
537 assert(handle
!= NULL
);
539 /* on api V2 we are able the read the latest command
541 * TODO: we need the test on api V1 too
543 if (h
->jtag_api
== STLINK_JTAG_API_V2
)
546 stlink_usb_init_buffer(handle
, h
->rx_ep
, rx_size
);
549 case STLINK_MODE_DEBUG_JTAG
:
550 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
551 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
552 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_ENTER
;
554 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_ENTER
;
555 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_ENTER_JTAG
;
557 case STLINK_MODE_DEBUG_SWD
:
558 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
559 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
560 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_ENTER
;
562 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_ENTER
;
563 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_ENTER_SWD
;
565 case STLINK_MODE_DEBUG_SWIM
:
566 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
567 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_ENTER
;
569 case STLINK_MODE_DFU
:
570 case STLINK_MODE_MASS
:
575 res
= stlink_usb_xfer(handle
, h
->databuf
, rx_size
);
580 res
= stlink_usb_error_check(h
);
586 static int stlink_usb_mode_leave(void *handle
, enum stlink_mode type
)
589 struct stlink_usb_handle_s
*h
= handle
;
591 assert(handle
!= NULL
);
593 stlink_usb_init_buffer(handle
, STLINK_NULL_EP
, 0);
596 case STLINK_MODE_DEBUG_JTAG
:
597 case STLINK_MODE_DEBUG_SWD
:
598 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
599 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_EXIT
;
601 case STLINK_MODE_DEBUG_SWIM
:
602 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
603 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_EXIT
;
605 case STLINK_MODE_DFU
:
606 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DFU_COMMAND
;
607 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DFU_EXIT
;
609 case STLINK_MODE_MASS
:
614 res
= stlink_usb_xfer(handle
, 0, 0);
622 static int stlink_usb_assert_srst(void *handle
, int srst
);
624 static enum stlink_mode
stlink_get_mode(enum hl_transports t
)
627 case HL_TRANSPORT_SWD
:
628 return STLINK_MODE_DEBUG_SWD
;
629 case HL_TRANSPORT_JTAG
:
630 return STLINK_MODE_DEBUG_JTAG
;
631 case HL_TRANSPORT_SWIM
:
632 return STLINK_MODE_DEBUG_SWIM
;
634 return STLINK_MODE_UNKNOWN
;
639 static int stlink_usb_init_mode(void *handle
, bool connect_under_reset
)
643 enum stlink_mode emode
;
644 struct stlink_usb_handle_s
*h
= handle
;
646 assert(handle
!= NULL
);
648 res
= stlink_usb_current_mode(handle
, &mode
);
653 LOG_DEBUG("MODE: 0x%02X", mode
);
655 /* try to exit current mode */
657 case STLINK_DEV_DFU_MODE
:
658 emode
= STLINK_MODE_DFU
;
660 case STLINK_DEV_DEBUG_MODE
:
661 emode
= STLINK_MODE_DEBUG_SWD
;
663 case STLINK_DEV_SWIM_MODE
:
664 emode
= STLINK_MODE_DEBUG_SWIM
;
666 case STLINK_DEV_BOOTLOADER_MODE
:
667 case STLINK_DEV_MASS_MODE
:
669 emode
= STLINK_MODE_UNKNOWN
;
673 if (emode
!= STLINK_MODE_UNKNOWN
) {
674 res
= stlink_usb_mode_leave(handle
, emode
);
680 res
= stlink_usb_current_mode(handle
, &mode
);
685 /* we check the target voltage here as an aid to debugging connection problems.
686 * the stlink requires the target Vdd to be connected for reliable debugging.
687 * this cmd is supported in all modes except DFU
689 if (mode
!= STLINK_DEV_DFU_MODE
) {
691 float target_voltage
;
693 /* check target voltage (if supported) */
694 res
= stlink_usb_check_voltage(h
, &target_voltage
);
696 if (res
!= ERROR_OK
) {
697 if (res
!= ERROR_COMMAND_NOTFOUND
)
698 LOG_ERROR("voltage check failed");
699 /* attempt to continue as it is not a catastrophic failure */
701 /* check for a sensible target voltage, operating range is 1.65-5.5v
702 * according to datasheet */
703 if (target_voltage
< 1.5)
704 LOG_ERROR("target voltage may be too low for reliable debugging");
708 LOG_DEBUG("MODE: 0x%02X", mode
);
710 /* set selected mode */
711 emode
= stlink_get_mode(h
->transport
);
713 if (emode
== STLINK_MODE_UNKNOWN
) {
714 LOG_ERROR("selected mode (transport) not supported");
718 if (connect_under_reset
) {
719 res
= stlink_usb_assert_srst(handle
, 0);
724 res
= stlink_usb_mode_enter(handle
, emode
);
729 res
= stlink_usb_current_mode(handle
, &mode
);
734 LOG_DEBUG("MODE: 0x%02X", mode
);
740 static int stlink_usb_idcode(void *handle
, uint32_t *idcode
)
743 struct stlink_usb_handle_s
*h
= handle
;
745 assert(handle
!= NULL
);
747 stlink_usb_init_buffer(handle
, h
->rx_ep
, 4);
749 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
750 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_READCOREID
;
752 res
= stlink_usb_xfer(handle
, h
->databuf
, 4);
757 *idcode
= le_to_h_u32(h
->databuf
);
759 LOG_DEBUG("IDCODE: 0x%08" PRIX32
, *idcode
);
764 static int stlink_usb_v2_read_debug_reg(void *handle
, uint32_t addr
, uint32_t *val
)
766 struct stlink_usb_handle_s
*h
= handle
;
769 assert(handle
!= NULL
);
771 stlink_usb_init_buffer(handle
, h
->rx_ep
, 8);
773 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
774 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_READDEBUGREG
;
775 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
778 res
= stlink_usb_xfer(handle
, h
->databuf
, 8);
783 *val
= le_to_h_u32(h
->databuf
+ 4);
785 return h
->databuf
[0] == STLINK_DEBUG_ERR_OK
? ERROR_OK
: ERROR_FAIL
;
788 static int stlink_usb_write_debug_reg(void *handle
, uint32_t addr
, uint32_t val
)
791 struct stlink_usb_handle_s
*h
= handle
;
793 assert(handle
!= NULL
);
795 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
797 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
798 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
799 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_WRITEDEBUGREG
;
801 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_WRITEDEBUGREG
;
802 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
804 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, val
);
807 res
= stlink_usb_xfer(handle
, h
->databuf
, 2);
812 return h
->databuf
[0] == STLINK_DEBUG_ERR_OK
? ERROR_OK
: ERROR_FAIL
;
816 static void stlink_usb_trace_read(void *handle
)
818 struct stlink_usb_handle_s
*h
= handle
;
820 assert(handle
!= NULL
);
822 if (h
->trace
.enabled
&& h
->version
.jtag
>= STLINK_TRACE_MIN_VERSION
) {
825 stlink_usb_init_buffer(handle
, h
->rx_ep
, 10);
827 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
828 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_GET_TRACE_NB
;
830 res
= stlink_usb_xfer(handle
, h
->databuf
, 2);
831 if (res
== ERROR_OK
) {
832 uint8_t buf
[STLINK_TRACE_SIZE
];
833 size_t size
= le_to_h_u16(h
->databuf
);
836 size
= size
< sizeof(buf
) ? size
: sizeof(buf
) - 1;
838 res
= stlink_usb_read_trace(handle
, buf
, size
);
839 if (res
== ERROR_OK
) {
840 if (h
->trace
.output_f
) {
841 /* Log retrieved trace output */
842 if (fwrite(buf
, 1, size
, h
->trace
.output_f
) > 0)
843 fflush(h
->trace
.output_f
);
851 static int stlink_usb_trace_read_callback(void *handle
)
853 stlink_usb_trace_read(handle
);
857 static enum target_state
stlink_usb_v2_get_status(void *handle
)
862 result
= stlink_usb_v2_read_debug_reg(handle
, DCB_DHCSR
, &status
);
863 if (result
!= ERROR_OK
)
864 return TARGET_UNKNOWN
;
867 return TARGET_HALTED
;
868 else if (status
& S_RESET_ST
)
871 stlink_usb_trace_read(handle
);
873 return TARGET_RUNNING
;
877 static enum target_state
stlink_usb_state(void *handle
)
880 struct stlink_usb_handle_s
*h
= handle
;
882 assert(handle
!= NULL
);
884 if (h
->reconnect_pending
) {
885 LOG_INFO("Previous state query failed, trying to reconnect");
886 res
= stlink_usb_mode_enter(handle
, stlink_get_mode(h
->transport
));
889 return TARGET_UNKNOWN
;
891 h
->reconnect_pending
= false;
894 if (h
->jtag_api
== STLINK_JTAG_API_V2
) {
895 res
= stlink_usb_v2_get_status(handle
);
896 if (res
== TARGET_UNKNOWN
)
897 h
->reconnect_pending
= true;
901 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
903 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
904 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_GETSTATUS
;
906 res
= stlink_usb_xfer(handle
, h
->databuf
, 2);
909 return TARGET_UNKNOWN
;
911 if (h
->databuf
[0] == STLINK_CORE_RUNNING
)
912 return TARGET_RUNNING
;
913 if (h
->databuf
[0] == STLINK_CORE_HALTED
)
914 return TARGET_HALTED
;
916 h
->reconnect_pending
= true;
918 return TARGET_UNKNOWN
;
922 static int stlink_usb_reset(void *handle
)
925 struct stlink_usb_handle_s
*h
= handle
;
927 assert(handle
!= NULL
);
929 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
931 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
933 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
934 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_RESETSYS
;
936 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_RESETSYS
;
938 res
= stlink_usb_xfer(handle
, h
->databuf
, 2);
943 LOG_DEBUG("RESET: 0x%08X", h
->databuf
[0]);
945 /* the following is not a error under swd (using hardware srst), so return success */
946 if (h
->databuf
[0] == STLINK_SWD_AP_WAIT
|| h
->databuf
[0] == STLINK_SWD_DP_WAIT
)
949 return h
->databuf
[0] == STLINK_DEBUG_ERR_OK
? ERROR_OK
: ERROR_FAIL
;
952 static int stlink_usb_assert_srst(void *handle
, int srst
)
955 struct stlink_usb_handle_s
*h
= handle
;
957 assert(handle
!= NULL
);
959 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
960 return ERROR_COMMAND_NOTFOUND
;
962 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
964 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
965 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_DRIVE_NRST
;
966 h
->cmdbuf
[h
->cmdidx
++] = srst
;
968 res
= stlink_usb_xfer(handle
, h
->databuf
, 2);
973 return h
->databuf
[0] == STLINK_DEBUG_ERR_OK
? ERROR_OK
: ERROR_FAIL
;
977 static int stlink_configure_target_trace_port(void *handle
)
981 struct stlink_usb_handle_s
*h
= handle
;
983 assert(handle
!= NULL
);
985 /* configure the TPI */
987 /* enable the trace subsystem */
988 res
= stlink_usb_v2_read_debug_reg(handle
, DCB_DEMCR
, ®
);
991 res
= stlink_usb_write_debug_reg(handle
, DCB_DEMCR
, TRCENA
|reg
);
994 /* set the TPI clock prescaler */
995 res
= stlink_usb_write_debug_reg(handle
, TPI_ACPR
, h
->trace
.prescale
);
998 /* select the pin protocol. The STLinkv2 only supports asynchronous
999 * UART emulation (NRZ) mode, so that's what we pick. */
1000 res
= stlink_usb_write_debug_reg(handle
, TPI_SPPR
, 0x02);
1001 if (res
!= ERROR_OK
)
1003 /* disable continuous formatting */
1004 res
= stlink_usb_write_debug_reg(handle
, TPI_FFCR
, (1<<8));
1005 if (res
!= ERROR_OK
)
1008 /* configure the ITM */
1010 /* unlock access to the ITM registers */
1011 res
= stlink_usb_write_debug_reg(handle
, ITM_LAR
, 0xC5ACCE55);
1012 if (res
!= ERROR_OK
)
1014 /* enable trace with ATB ID 1 */
1015 res
= stlink_usb_write_debug_reg(handle
, ITM_TCR
, (1<<16)|(1<<0)|(1<<2));
1016 if (res
!= ERROR_OK
)
1018 /* trace privilege */
1019 res
= stlink_usb_write_debug_reg(handle
, ITM_TPR
, 1);
1020 if (res
!= ERROR_OK
)
1022 /* trace port enable (port 0) */
1023 res
= stlink_usb_write_debug_reg(handle
, ITM_TER
, (1<<0));
1024 if (res
!= ERROR_OK
)
1033 static void stlink_usb_trace_disable(void *handle
)
1036 struct stlink_usb_handle_s
*h
= handle
;
1038 assert(handle
!= NULL
);
1040 assert(h
->version
.jtag
>= STLINK_TRACE_MIN_VERSION
);
1042 LOG_DEBUG("Tracing: disable\n");
1044 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1045 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1046 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_STOP_TRACE_RX
;
1047 res
= stlink_usb_xfer(handle
, h
->databuf
, 2);
1049 if (res
== ERROR_OK
) {
1050 h
->trace
.enabled
= false;
1051 target_unregister_timer_callback(stlink_usb_trace_read_callback
, handle
);
1057 static int stlink_usb_trace_enable(void *handle
)
1060 struct stlink_usb_handle_s
*h
= handle
;
1062 assert(handle
!= NULL
);
1064 if (h
->version
.jtag
>= STLINK_TRACE_MIN_VERSION
) {
1067 res
= stlink_configure_target_trace_port(handle
);
1068 if (res
!= ERROR_OK
)
1069 LOG_ERROR("Unable to configure tracing on target\n");
1071 trace_hz
= h
->trace
.prescale
> 0 ?
1072 h
->trace
.source_hz
/ (h
->trace
.prescale
+ 1) :
1075 stlink_usb_init_buffer(handle
, h
->rx_ep
, 10);
1077 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1078 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_START_TRACE_RX
;
1079 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, (uint16_t)STLINK_TRACE_SIZE
);
1081 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, trace_hz
);
1084 res
= stlink_usb_xfer(handle
, h
->databuf
, 2);
1086 if (res
== ERROR_OK
) {
1087 h
->trace
.enabled
= true;
1088 LOG_DEBUG("Tracing: recording at %" PRIu32
"Hz\n", trace_hz
);
1089 /* We need the trace read function to be called at a
1090 * high-enough frequency to ensure reasonable
1091 * "timeliness" in processing ITM/DWT data.
1092 * TODO: An alternative could be using the asynchronous
1093 * features of the libusb-1.0 API to queue up one or more
1094 * reads in advance and requeue them once they are
1096 target_register_timer_callback(stlink_usb_trace_read_callback
, 1, 1, handle
);
1099 LOG_ERROR("Tracing is not supported by this version.");
1107 static int stlink_usb_run(void *handle
)
1110 struct stlink_usb_handle_s
*h
= handle
;
1112 assert(handle
!= NULL
);
1114 if (h
->jtag_api
== STLINK_JTAG_API_V2
) {
1115 res
= stlink_usb_write_debug_reg(handle
, DCB_DHCSR
, DBGKEY
|C_DEBUGEN
);
1117 /* Try to start tracing, if requested */
1118 if (res
== ERROR_OK
&& h
->trace
.source_hz
&& !h
->trace
.enabled
) {
1119 if (stlink_usb_trace_enable(handle
) == ERROR_OK
)
1120 LOG_DEBUG("Tracing: enabled\n");
1122 LOG_ERROR("Tracing: enable failed\n");
1128 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1130 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1131 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_RUNCORE
;
1133 res
= stlink_usb_xfer(handle
, h
->databuf
, 2);
1135 if (res
!= ERROR_OK
)
1138 return h
->databuf
[0] == STLINK_DEBUG_ERR_OK
? ERROR_OK
: ERROR_FAIL
;
1142 static int stlink_usb_halt(void *handle
)
1145 struct stlink_usb_handle_s
*h
= handle
;
1147 assert(handle
!= NULL
);
1149 if (h
->jtag_api
== STLINK_JTAG_API_V2
) {
1150 res
= stlink_usb_write_debug_reg(handle
, DCB_DHCSR
, DBGKEY
|C_HALT
|C_DEBUGEN
);
1152 if (res
== ERROR_OK
&& h
->trace
.enabled
)
1153 stlink_usb_trace_disable(handle
);
1158 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1160 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1161 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_FORCEDEBUG
;
1163 res
= stlink_usb_xfer(handle
, h
->databuf
, 2);
1165 if (res
!= ERROR_OK
)
1168 return h
->databuf
[0] == STLINK_DEBUG_ERR_OK
? ERROR_OK
: ERROR_FAIL
;
1172 static int stlink_usb_step(void *handle
)
1175 struct stlink_usb_handle_s
*h
= handle
;
1177 assert(handle
!= NULL
);
1179 if (h
->jtag_api
== STLINK_JTAG_API_V2
) {
1180 /* TODO: this emulates the v1 api, it should really use a similar auto mask isr
1181 * that the cortex-m3 currently does. */
1182 stlink_usb_write_debug_reg(handle
, DCB_DHCSR
, DBGKEY
|C_HALT
|C_MASKINTS
|C_DEBUGEN
);
1183 stlink_usb_write_debug_reg(handle
, DCB_DHCSR
, DBGKEY
|C_STEP
|C_MASKINTS
|C_DEBUGEN
);
1184 return stlink_usb_write_debug_reg(handle
, DCB_DHCSR
, DBGKEY
|C_HALT
|C_DEBUGEN
);
1187 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1189 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1190 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_STEPCORE
;
1192 res
= stlink_usb_xfer(handle
, h
->databuf
, 2);
1194 if (res
!= ERROR_OK
)
1197 return h
->databuf
[0] == STLINK_DEBUG_ERR_OK
? ERROR_OK
: ERROR_FAIL
;
1201 static int stlink_usb_read_regs(void *handle
)
1204 struct stlink_usb_handle_s
*h
= handle
;
1206 assert(handle
!= NULL
);
1208 stlink_usb_init_buffer(handle
, h
->rx_ep
, 84);
1210 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1211 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
1212 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_READALLREGS
;
1214 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_READALLREGS
;
1216 res
= stlink_usb_xfer(handle
, h
->databuf
, 84);
1218 if (res
!= ERROR_OK
)
1225 static int stlink_usb_read_reg(void *handle
, int num
, uint32_t *val
)
1228 struct stlink_usb_handle_s
*h
= handle
;
1230 assert(handle
!= NULL
);
1232 stlink_usb_init_buffer(handle
, h
->rx_ep
, h
->jtag_api
== STLINK_JTAG_API_V1
? 4 : 8);
1234 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1235 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
1236 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_READREG
;
1238 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_READREG
;
1239 h
->cmdbuf
[h
->cmdidx
++] = num
;
1241 res
= stlink_usb_xfer(handle
, h
->databuf
, h
->jtag_api
== STLINK_JTAG_API_V1
? 4 : 8);
1243 if (res
!= ERROR_OK
)
1246 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
1247 *val
= le_to_h_u32(h
->databuf
);
1249 *val
= le_to_h_u32(h
->databuf
+ 4);
1250 return h
->databuf
[0] == STLINK_DEBUG_ERR_OK
? ERROR_OK
: ERROR_FAIL
;
1257 static int stlink_usb_write_reg(void *handle
, int num
, uint32_t val
)
1260 struct stlink_usb_handle_s
*h
= handle
;
1262 assert(handle
!= NULL
);
1264 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1266 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1267 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
1268 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_WRITEREG
;
1270 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_WRITEREG
;
1271 h
->cmdbuf
[h
->cmdidx
++] = num
;
1272 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, val
);
1275 res
= stlink_usb_xfer(handle
, h
->databuf
, 2);
1277 if (res
!= ERROR_OK
)
1280 return h
->databuf
[0] == STLINK_DEBUG_ERR_OK
? ERROR_OK
: ERROR_FAIL
;
1283 static int stlink_usb_get_rw_status(void *handle
)
1286 struct stlink_usb_handle_s
*h
= handle
;
1288 assert(handle
!= NULL
);
1290 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
1293 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1295 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1296 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_GETLASTRWSTATUS
;
1298 res
= stlink_usb_xfer(handle
, h
->databuf
, 2);
1300 if (res
!= ERROR_OK
)
1303 return h
->databuf
[0] == STLINK_DEBUG_ERR_OK
? ERROR_OK
: res
;
1307 static int stlink_usb_read_mem8(void *handle
, uint32_t addr
, uint16_t len
,
1311 uint16_t read_len
= len
;
1312 struct stlink_usb_handle_s
*h
= handle
;
1314 assert(handle
!= NULL
);
1316 /* max 8bit read/write is 64bytes */
1317 if (len
> STLINK_MAX_RW8
) {
1318 LOG_DEBUG("max buffer length exceeded");
1322 stlink_usb_init_buffer(handle
, h
->rx_ep
, read_len
);
1324 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1325 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_READMEM_8BIT
;
1326 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
1328 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
1331 /* we need to fix read length for single bytes */
1335 res
= stlink_usb_xfer(handle
, h
->databuf
, read_len
);
1337 if (res
!= ERROR_OK
)
1340 memcpy(buffer
, h
->databuf
, len
);
1342 return stlink_usb_get_rw_status(handle
);
1346 static int stlink_usb_write_mem8(void *handle
, uint32_t addr
, uint16_t len
,
1347 const uint8_t *buffer
)
1350 struct stlink_usb_handle_s
*h
= handle
;
1352 assert(handle
!= NULL
);
1354 /* max 8bit read/write is 64bytes */
1355 if (len
> STLINK_MAX_RW8
) {
1356 LOG_DEBUG("max buffer length exceeded");
1360 stlink_usb_init_buffer(handle
, h
->tx_ep
, len
);
1362 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1363 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_WRITEMEM_8BIT
;
1364 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
1366 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
1369 res
= stlink_usb_xfer(handle
, buffer
, len
);
1371 if (res
!= ERROR_OK
)
1374 return stlink_usb_get_rw_status(handle
);
1378 static int stlink_usb_read_mem32(void *handle
, uint32_t addr
, uint16_t len
,
1382 struct stlink_usb_handle_s
*h
= handle
;
1384 assert(handle
!= NULL
);
1386 /* data must be a multiple of 4 and word aligned */
1387 if (len
% 4 || addr
% 4) {
1388 LOG_DEBUG("Invalid data alignment");
1389 return ERROR_TARGET_UNALIGNED_ACCESS
;
1392 stlink_usb_init_buffer(handle
, h
->rx_ep
, len
);
1394 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1395 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_READMEM_32BIT
;
1396 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
1398 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
1401 res
= stlink_usb_xfer(handle
, h
->databuf
, len
);
1403 if (res
!= ERROR_OK
)
1406 memcpy(buffer
, h
->databuf
, len
);
1408 return stlink_usb_get_rw_status(handle
);
1412 static int stlink_usb_write_mem32(void *handle
, uint32_t addr
, uint16_t len
,
1413 const uint8_t *buffer
)
1416 struct stlink_usb_handle_s
*h
= handle
;
1418 assert(handle
!= NULL
);
1420 /* data must be a multiple of 4 and word aligned */
1421 if (len
% 4 || addr
% 4) {
1422 LOG_DEBUG("Invalid data alignment");
1423 return ERROR_TARGET_UNALIGNED_ACCESS
;
1426 stlink_usb_init_buffer(handle
, h
->tx_ep
, len
);
1428 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1429 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_WRITEMEM_32BIT
;
1430 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
1432 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
1435 res
= stlink_usb_xfer(handle
, buffer
, len
);
1437 if (res
!= ERROR_OK
)
1440 return stlink_usb_get_rw_status(handle
);
1443 static uint32_t stlink_max_block_size(uint32_t tar_autoincr_block
, uint32_t address
)
1445 uint32_t max_tar_block
= (tar_autoincr_block
- ((tar_autoincr_block
- 1) & address
));
1446 if (max_tar_block
== 0)
1448 return max_tar_block
;
1451 static int stlink_usb_read_mem(void *handle
, uint32_t addr
, uint32_t size
,
1452 uint32_t count
, uint8_t *buffer
)
1454 int retval
= ERROR_OK
;
1455 uint32_t bytes_remaining
;
1456 struct stlink_usb_handle_s
*h
= handle
;
1458 /* calculate byte count */
1463 bytes_remaining
= (size
== 4) ? \
1464 stlink_max_block_size(h
->max_mem_packet
, addr
) : STLINK_MAX_RW8
;
1466 if (count
< bytes_remaining
)
1467 bytes_remaining
= count
;
1469 /* the stlink only supports 8/32bit memory read/writes
1470 * honour 32bit, all others will be handled as 8bit access */
1473 /* When in jtag mode the stlink uses the auto-increment functinality.
1474 * However it expects us to pass the data correctly, this includes
1475 * alignment and any page boundaries. We already do this as part of the
1476 * adi_v5 implementation, but the stlink is a hla adapter and so this
1477 * needs implementiong manually.
1478 * currently this only affects jtag mode, according to ST they do single
1479 * access in SWD mode - but this may change and so we do it for both modes */
1481 /* we first need to check for any unaligned bytes */
1484 uint32_t head_bytes
= 4 - (addr
% 4);
1485 retval
= stlink_usb_read_mem8(handle
, addr
, head_bytes
, buffer
);
1486 if (retval
!= ERROR_OK
)
1488 buffer
+= head_bytes
;
1490 count
-= head_bytes
;
1491 bytes_remaining
-= head_bytes
;
1494 if (bytes_remaining
% 4)
1495 retval
= stlink_usb_read_mem(handle
, addr
, 1, bytes_remaining
, buffer
);
1497 retval
= stlink_usb_read_mem32(handle
, addr
, bytes_remaining
, buffer
);
1499 retval
= stlink_usb_read_mem8(handle
, addr
, bytes_remaining
, buffer
);
1501 if (retval
!= ERROR_OK
)
1504 buffer
+= bytes_remaining
;
1505 addr
+= bytes_remaining
;
1506 count
-= bytes_remaining
;
1512 static int stlink_usb_write_mem(void *handle
, uint32_t addr
, uint32_t size
,
1513 uint32_t count
, const uint8_t *buffer
)
1515 int retval
= ERROR_OK
;
1516 uint32_t bytes_remaining
;
1517 struct stlink_usb_handle_s
*h
= handle
;
1519 /* calculate byte count */
1524 bytes_remaining
= (size
== 4) ? \
1525 stlink_max_block_size(h
->max_mem_packet
, addr
) : STLINK_MAX_RW8
;
1527 if (count
< bytes_remaining
)
1528 bytes_remaining
= count
;
1530 /* the stlink only supports 8/32bit memory read/writes
1531 * honour 32bit, all others will be handled as 8bit access */
1534 /* When in jtag mode the stlink uses the auto-increment functinality.
1535 * However it expects us to pass the data correctly, this includes
1536 * alignment and any page boundaries. We already do this as part of the
1537 * adi_v5 implementation, but the stlink is a hla adapter and so this
1538 * needs implementiong manually.
1539 * currently this only affects jtag mode, according to ST they do single
1540 * access in SWD mode - but this may change and so we do it for both modes */
1542 /* we first need to check for any unaligned bytes */
1545 uint32_t head_bytes
= 4 - (addr
% 4);
1546 retval
= stlink_usb_write_mem8(handle
, addr
, head_bytes
, buffer
);
1547 if (retval
!= ERROR_OK
)
1549 buffer
+= head_bytes
;
1551 count
-= head_bytes
;
1552 bytes_remaining
-= head_bytes
;
1555 if (bytes_remaining
% 4)
1556 retval
= stlink_usb_write_mem(handle
, addr
, 1, bytes_remaining
, buffer
);
1558 retval
= stlink_usb_write_mem32(handle
, addr
, bytes_remaining
, buffer
);
1561 retval
= stlink_usb_write_mem8(handle
, addr
, bytes_remaining
, buffer
);
1562 if (retval
!= ERROR_OK
)
1565 buffer
+= bytes_remaining
;
1566 addr
+= bytes_remaining
;
1567 count
-= bytes_remaining
;
1574 static int stlink_usb_close(void *fd
)
1576 struct stlink_usb_handle_s
*h
= fd
;
1579 jtag_libusb_close(h
->fd
);
1587 static int stlink_usb_open(struct hl_interface_param_s
*param
, void **fd
)
1589 int err
, retry_count
= 1;
1590 struct stlink_usb_handle_s
*h
;
1591 enum stlink_jtag_api_version api
;
1593 LOG_DEBUG("stlink_usb_open");
1595 h
= calloc(1, sizeof(struct stlink_usb_handle_s
));
1598 LOG_DEBUG("malloc failed");
1602 h
->transport
= param
->transport
;
1604 const uint16_t vids
[] = { param
->vid
, 0 };
1605 const uint16_t pids
[] = { param
->pid
, 0 };
1607 LOG_DEBUG("transport: %d vid: 0x%04x pid: 0x%04x", param
->transport
,
1608 param
->vid
, param
->pid
);
1611 On certain host USB configurations(e.g. MacBook Air)
1612 STLINKv2 dongle seems to have its FW in a funky state if,
1613 after plugging it in, you try to use openocd with it more
1614 then once (by launching and closing openocd). In cases like
1615 that initial attempt to read the FW info via
1616 stlink_usb_version will fail and the device has to be reset
1617 in order to become operational.
1620 if (jtag_libusb_open(vids
, pids
, &h
->fd
) != ERROR_OK
) {
1621 LOG_ERROR("open failed");
1625 jtag_libusb_set_configuration(h
->fd
, 0);
1627 if (jtag_libusb_claim_interface(h
->fd
, 0) != ERROR_OK
) {
1628 LOG_DEBUG("claim interface failed");
1632 /* RX EP is common for all versions */
1633 h
->rx_ep
= STLINK_RX_EP
;
1635 /* wrap version for first read */
1636 switch (param
->pid
) {
1638 h
->version
.stlink
= 1;
1639 h
->tx_ep
= STLINK_TX_EP
;
1640 h
->trace_ep
= STLINK_TRACE_EP
;
1642 case STLINK_V2_1_PID
:
1643 h
->version
.stlink
= 2;
1644 h
->tx_ep
= STLINK_V2_1_TX_EP
;
1645 h
->trace_ep
= STLINK_V2_1_TRACE_EP
;
1648 /* fall through - we assume V2 to be the default version*/
1650 h
->version
.stlink
= 2;
1651 h
->tx_ep
= STLINK_TX_EP
;
1652 h
->trace_ep
= STLINK_TRACE_EP
;
1656 /* get the device version */
1657 err
= stlink_usb_version(h
);
1659 if (err
== ERROR_OK
) {
1661 } else if (h
->version
.stlink
== 1 ||
1663 LOG_ERROR("read version failed");
1666 err
= jtag_libusb_release_interface(h
->fd
, 0);
1667 if (err
!= ERROR_OK
) {
1668 LOG_ERROR("release interface failed");
1672 err
= jtag_libusb_reset_device(h
->fd
);
1673 if (err
!= ERROR_OK
) {
1674 LOG_ERROR("reset device failed");
1678 jtag_libusb_close(h
->fd
);
1680 Give the device one second to settle down and
1683 usleep(1 * 1000 * 1000);
1688 /* compare usb vid/pid */
1689 if ((param
->vid
!= h
->vid
) || (param
->pid
!= h
->pid
))
1690 LOG_INFO("vid/pid are not identical: 0x%04X/0x%04X 0x%04X/0x%04X",
1691 param
->vid
, param
->pid
,
1694 /* check if mode is supported */
1697 switch (h
->transport
) {
1698 case HL_TRANSPORT_SWD
:
1699 case HL_TRANSPORT_JTAG
:
1700 if (h
->version
.jtag
== 0)
1703 case HL_TRANSPORT_SWIM
:
1704 if (h
->version
.swim
== 0)
1712 if (err
!= ERROR_OK
) {
1713 LOG_ERROR("mode (transport) not supported by device");
1717 api
= h
->version
.jtag_api_max
;
1719 LOG_INFO("using stlink api v%d", api
);
1721 /* set the used jtag api, this will default to the newest supported version */
1724 if (h
->jtag_api
>= 2 && param
->trace_source_hz
> 0) {
1727 prescale
= param
->trace_source_hz
> STLINK_TRACE_MAX_HZ
?
1728 (param
->trace_source_hz
/ STLINK_TRACE_MAX_HZ
) - 1 : 0;
1730 h
->trace
.output_f
= param
->trace_f
;
1731 h
->trace
.source_hz
= param
->trace_source_hz
;
1732 h
->trace
.prescale
= prescale
;
1735 /* initialize the debug hardware */
1736 err
= stlink_usb_init_mode(h
, param
->connect_under_reset
);
1738 if (err
!= ERROR_OK
) {
1739 LOG_ERROR("init mode failed");
1743 /* get cpuid, so we can determine the max page size
1744 * start with a safe default */
1745 h
->max_mem_packet
= (1 << 10);
1748 err
= stlink_usb_read_mem32(h
, CPUID
, 4, buffer
);
1749 if (err
== ERROR_OK
) {
1750 uint32_t cpuid
= le_to_h_u32(buffer
);
1751 int i
= (cpuid
>> 4) & 0xf;
1752 if (i
== 4 || i
== 3) {
1753 /* Cortex-M3/M4 has 4096 bytes autoincrement range */
1754 h
->max_mem_packet
= (1 << 12);
1758 LOG_DEBUG("Using TAR autoincrement: %" PRIu32
, h
->max_mem_packet
);
1765 stlink_usb_close(h
);
1771 struct hl_layout_api_s stlink_usb_layout_api
= {
1773 .open
= stlink_usb_open
,
1775 .close
= stlink_usb_close
,
1777 .idcode
= stlink_usb_idcode
,
1779 .state
= stlink_usb_state
,
1781 .reset
= stlink_usb_reset
,
1783 .assert_srst
= stlink_usb_assert_srst
,
1785 .run
= stlink_usb_run
,
1787 .halt
= stlink_usb_halt
,
1789 .step
= stlink_usb_step
,
1791 .read_regs
= stlink_usb_read_regs
,
1793 .read_reg
= stlink_usb_read_reg
,
1795 .write_reg
= stlink_usb_write_reg
,
1797 .read_mem
= stlink_usb_read_mem
,
1799 .write_mem
= stlink_usb_write_mem
,
1801 .write_debug_reg
= stlink_usb_write_debug_reg