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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
24 ***************************************************************************/
30 /* project specific includes */
31 #include <helper/binarybuffer.h>
32 #include <jtag/interface.h>
33 #include <jtag/stlink/stlink_layout.h>
34 #include <jtag/stlink/stlink_transport.h>
35 #include <jtag/stlink/stlink_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_NULL_EP 0
46 #define STLINK_RX_EP (1|ENDPOINT_IN)
47 #define STLINK_TX_EP (2|ENDPOINT_OUT)
48 #define STLINK_SG_SIZE (31)
49 #define STLINK_DATA_SIZE (4*128)
50 #define STLINK_CMD_SIZE_V2 (16)
51 #define STLINK_CMD_SIZE_V1 (10)
53 enum stlink_jtag_api_version
{
54 STLINK_JTAG_API_V1
= 1,
59 struct stlink_usb_version
{
66 /** highest supported jtag api version */
67 enum stlink_jtag_api_version jtag_api_max
;
71 struct stlink_usb_handle_s
{
73 struct jtag_libusb_device_handle
*fd
;
75 struct libusb_transfer
*trans
;
77 uint8_t cmdbuf
[STLINK_SG_SIZE
];
83 uint8_t databuf
[STLINK_DATA_SIZE
];
85 enum stlink_transports transport
;
87 struct stlink_usb_version version
;
92 /** this is the currently used jtag api */
93 enum stlink_jtag_api_version jtag_api
;
96 #define STLINK_DEBUG_ERR_OK 0x80
97 #define STLINK_DEBUG_ERR_FAULT 0x81
98 #define STLINK_SWD_AP_WAIT 0x10
99 #define STLINK_SWD_DP_WAIT 0x14
101 #define STLINK_CORE_RUNNING 0x80
102 #define STLINK_CORE_HALTED 0x81
103 #define STLINK_CORE_STAT_UNKNOWN -1
105 #define STLINK_GET_VERSION 0xF1
106 #define STLINK_DEBUG_COMMAND 0xF2
107 #define STLINK_DFU_COMMAND 0xF3
108 #define STLINK_SWIM_COMMAND 0xF4
109 #define STLINK_GET_CURRENT_MODE 0xF5
111 #define STLINK_DEV_DFU_MODE 0x00
112 #define STLINK_DEV_MASS_MODE 0x01
113 #define STLINK_DEV_DEBUG_MODE 0x02
114 #define STLINK_DEV_SWIM_MODE 0x03
115 #define STLINK_DEV_BOOTLOADER_MODE 0x04
116 #define STLINK_DEV_UNKNOWN_MODE -1
118 #define STLINK_DFU_EXIT 0x07
120 #define STLINK_SWIM_ENTER 0x00
121 #define STLINK_SWIM_EXIT 0x01
123 #define STLINK_DEBUG_ENTER_JTAG 0x00
124 #define STLINK_DEBUG_GETSTATUS 0x01
125 #define STLINK_DEBUG_FORCEDEBUG 0x02
126 #define STLINK_DEBUG_APIV1_RESETSYS 0x03
127 #define STLINK_DEBUG_APIV1_READALLREGS 0x04
128 #define STLINK_DEBUG_APIV1_READREG 0x05
129 #define STLINK_DEBUG_APIV1_WRITEREG 0x06
130 #define STLINK_DEBUG_READMEM_32BIT 0x07
131 #define STLINK_DEBUG_WRITEMEM_32BIT 0x08
132 #define STLINK_DEBUG_RUNCORE 0x09
133 #define STLINK_DEBUG_STEPCORE 0x0a
134 #define STLINK_DEBUG_APIV1_SETFP 0x0b
135 #define STLINK_DEBUG_READMEM_8BIT 0x0c
136 #define STLINK_DEBUG_WRITEMEM_8BIT 0x0d
137 #define STLINK_DEBUG_APIV1_CLEARFP 0x0e
138 #define STLINK_DEBUG_APIV1_WRITEDEBUGREG 0x0f
139 #define STLINK_DEBUG_APIV1_SETWATCHPOINT 0x10
141 #define STLINK_DEBUG_ENTER_JTAG 0x00
142 #define STLINK_DEBUG_ENTER_SWD 0xa3
144 #define STLINK_DEBUG_APIV1_ENTER 0x20
145 #define STLINK_DEBUG_EXIT 0x21
146 #define STLINK_DEBUG_READCOREID 0x22
148 #define STLINK_DEBUG_APIV2_ENTER 0x30
149 #define STLINK_DEBUG_APIV2_READ_IDCODES 0x31
150 #define STLINK_DEBUG_APIV2_RESETSYS 0x32
151 #define STLINK_DEBUG_APIV2_READREG 0x33
152 #define STLINK_DEBUG_APIV2_WRITEREG 0x34
153 #define STLINK_DEBUG_APIV2_WRITEDEBUGREG 0x35
154 #define STLINK_DEBUG_APIV2_READDEBUGREG 0x36
156 #define STLINK_DEBUG_APIV2_READALLREGS 0x3A
157 #define STLINK_DEBUG_APIV2_GETLASTRWSTATUS 0x3B
158 #define STLINK_DEBUG_APIV2_DRIVE_NRST 0x3C
160 #define STLINK_DEBUG_APIV2_DRIVE_NRST_LOW 0x00
161 #define STLINK_DEBUG_APIV2_DRIVE_NRST_HIGH 0x01
162 #define STLINK_DEBUG_APIV2_DRIVE_NRST_PULSE 0x02
166 STLINK_MODE_UNKNOWN
= 0,
169 STLINK_MODE_DEBUG_JTAG
,
170 STLINK_MODE_DEBUG_SWD
,
171 STLINK_MODE_DEBUG_SWIM
174 #define REQUEST_SENSE 0x03
175 #define REQUEST_SENSE_LENGTH 18
177 static void stlink_usb_init_buffer(void *handle
, uint8_t direction
, uint32_t size
);
180 static int stlink_usb_xfer_v1_get_status(void *handle
)
182 struct stlink_usb_handle_s
*h
;
184 assert(handle
!= NULL
);
186 h
= (struct stlink_usb_handle_s
*)handle
;
189 memset(h
->cmdbuf
, 0, STLINK_SG_SIZE
);
191 if (jtag_libusb_bulk_read(h
->fd
, STLINK_RX_EP
, (char *)h
->cmdbuf
,
197 t1
= buf_get_u32(h
->cmdbuf
, 0, 32);
200 if (t1
!= 0x53425355)
208 if (h
->cmdbuf
[12] != 0)
215 static int stlink_usb_xfer_rw(void *handle
, int cmdsize
, const uint8_t *buf
, int size
)
217 struct stlink_usb_handle_s
*h
;
219 assert(handle
!= NULL
);
221 h
= (struct stlink_usb_handle_s
*)handle
;
223 if (jtag_libusb_bulk_write(h
->fd
, STLINK_TX_EP
, (char *)h
->cmdbuf
, cmdsize
,
228 if (h
->direction
== STLINK_TX_EP
&& size
) {
229 if (jtag_libusb_bulk_write(h
->fd
, STLINK_TX_EP
, (char *)buf
,
230 size
, 1000) != size
) {
231 LOG_DEBUG("bulk write failed");
234 } else if (h
->direction
== STLINK_RX_EP
&& size
) {
235 if (jtag_libusb_bulk_read(h
->fd
, STLINK_RX_EP
, (char *)buf
,
236 size
, 1000) != size
) {
237 LOG_DEBUG("bulk read failed");
246 static int stlink_usb_xfer_v1_get_sense(void *handle
)
249 struct stlink_usb_handle_s
*h
;
251 assert(handle
!= NULL
);
253 h
= (struct stlink_usb_handle_s
*)handle
;
255 stlink_usb_init_buffer(handle
, STLINK_RX_EP
, 16);
257 h
->cmdbuf
[h
->cmdidx
++] = REQUEST_SENSE
;
258 h
->cmdbuf
[h
->cmdidx
++] = 0;
259 h
->cmdbuf
[h
->cmdidx
++] = 0;
260 h
->cmdbuf
[h
->cmdidx
++] = 0;
261 h
->cmdbuf
[h
->cmdidx
++] = REQUEST_SENSE_LENGTH
;
263 res
= stlink_usb_xfer_rw(handle
, REQUEST_SENSE_LENGTH
, h
->databuf
, 16);
268 if (stlink_usb_xfer_v1_get_status(handle
) != ERROR_OK
)
275 static int stlink_usb_xfer(void *handle
, const uint8_t *buf
, int size
)
277 int err
, cmdsize
= STLINK_CMD_SIZE_V2
;
278 struct stlink_usb_handle_s
*h
;
280 assert(handle
!= NULL
);
282 h
= (struct stlink_usb_handle_s
*)handle
;
284 if (h
->version
.stlink
== 1)
285 cmdsize
= STLINK_SG_SIZE
;
287 err
= stlink_usb_xfer_rw(handle
, cmdsize
, buf
, size
);
292 if (h
->version
.stlink
== 1) {
293 if (stlink_usb_xfer_v1_get_status(handle
) != ERROR_OK
) {
294 /* check csw status */
295 if (h
->cmdbuf
[12] == 1) {
296 LOG_DEBUG("get sense");
297 if (stlink_usb_xfer_v1_get_sense(handle
) != ERROR_OK
)
308 static void stlink_usb_xfer_v1_create_cmd(void *handle
, uint8_t direction
, uint32_t size
)
310 struct stlink_usb_handle_s
*h
;
312 h
= (struct stlink_usb_handle_s
*)handle
;
314 /* fill the send buffer */
315 strcpy((char *)h
->cmdbuf
, "USBC");
317 /* csw tag not used */
319 buf_set_u32(h
->cmdbuf
+h
->cmdidx
, 0, 32, size
);
321 h
->cmdbuf
[h
->cmdidx
++] = (direction
== STLINK_RX_EP
? ENDPOINT_IN
: ENDPOINT_OUT
);
322 h
->cmdbuf
[h
->cmdidx
++] = 0; /* lun */
323 h
->cmdbuf
[h
->cmdidx
++] = STLINK_CMD_SIZE_V1
;
327 static void stlink_usb_init_buffer(void *handle
, uint8_t direction
, uint32_t size
)
329 struct stlink_usb_handle_s
*h
;
331 h
= (struct stlink_usb_handle_s
*)handle
;
333 h
->direction
= direction
;
337 memset(h
->cmdbuf
, 0, STLINK_SG_SIZE
);
338 memset(h
->databuf
, 0, STLINK_DATA_SIZE
);
340 if (h
->version
.stlink
== 1)
341 stlink_usb_xfer_v1_create_cmd(handle
, direction
, size
);
344 static const char * const stlink_usb_error_msg
[] = {
349 static int stlink_usb_error_check(void *handle
)
352 const char *err_msg
= 0;
353 struct stlink_usb_handle_s
*h
;
355 assert(handle
!= NULL
);
357 h
= (struct stlink_usb_handle_s
*)handle
;
359 /* TODO: no error checking yet on api V1 */
360 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
361 h
->databuf
[0] = STLINK_DEBUG_ERR_OK
;
363 switch (h
->databuf
[0]) {
364 case STLINK_DEBUG_ERR_OK
:
367 case STLINK_DEBUG_ERR_FAULT
:
369 err_msg
= stlink_usb_error_msg
[0];
375 LOG_DEBUG("status error: %d ('%s')", h
->databuf
[0], err_msg
);
381 static int stlink_usb_version(void *handle
)
385 struct stlink_usb_handle_s
*h
;
387 assert(handle
!= NULL
);
389 h
= (struct stlink_usb_handle_s
*)handle
;
391 stlink_usb_init_buffer(handle
, STLINK_RX_EP
, 6);
393 h
->cmdbuf
[h
->cmdidx
++] = STLINK_GET_VERSION
;
395 res
= stlink_usb_xfer(handle
, h
->databuf
, 6);
400 v
= (h
->databuf
[0] << 8) | h
->databuf
[1];
402 h
->version
.stlink
= (v
>> 12) & 0x0f;
403 h
->version
.jtag
= (v
>> 6) & 0x3f;
404 h
->version
.swim
= v
& 0x3f;
405 h
->vid
= buf_get_u32(h
->databuf
, 16, 16);
406 h
->pid
= buf_get_u32(h
->databuf
, 32, 16);
408 /* set the supported jtag api version
409 * API V2 is supported since JTAG V11
411 if (h
->version
.jtag
>= 11)
412 h
->version
.jtag_api_max
= STLINK_JTAG_API_V2
;
414 h
->version
.jtag_api_max
= STLINK_JTAG_API_V1
;
416 LOG_DEBUG("STLINK v%d JTAG v%d API v%d SWIM v%d VID 0x%04X PID 0x%04X",
419 (h
->version
.jtag_api_max
== STLINK_JTAG_API_V1
) ? 1 : 2,
428 static int stlink_usb_current_mode(void *handle
, uint8_t *mode
)
431 struct stlink_usb_handle_s
*h
;
433 assert(handle
!= NULL
);
435 h
= (struct stlink_usb_handle_s
*)handle
;
437 stlink_usb_init_buffer(handle
, STLINK_RX_EP
, 2);
439 h
->cmdbuf
[h
->cmdidx
++] = STLINK_GET_CURRENT_MODE
;
441 res
= stlink_usb_xfer(handle
, h
->databuf
, 2);
446 *mode
= h
->databuf
[0];
452 static int stlink_usb_mode_enter(void *handle
, enum stlink_mode type
)
456 struct stlink_usb_handle_s
*h
;
458 assert(handle
!= NULL
);
460 h
= (struct stlink_usb_handle_s
*)handle
;
462 /* on api V2 we are able the read the latest command
464 * TODO: we need the test on api V1 too
466 if (h
->jtag_api
== STLINK_JTAG_API_V2
)
469 stlink_usb_init_buffer(handle
, STLINK_RX_EP
, rx_size
);
472 case STLINK_MODE_DEBUG_JTAG
:
473 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
474 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
475 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_ENTER
;
477 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_ENTER
;
478 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_ENTER_JTAG
;
480 case STLINK_MODE_DEBUG_SWD
:
481 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
482 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
483 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_ENTER
;
485 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_ENTER
;
486 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_ENTER_SWD
;
488 case STLINK_MODE_DEBUG_SWIM
:
489 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
490 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_ENTER
;
492 case STLINK_MODE_DFU
:
493 case STLINK_MODE_MASS
:
498 res
= stlink_usb_xfer(handle
, h
->databuf
, rx_size
);
503 res
= stlink_usb_error_check(h
);
509 static int stlink_usb_mode_leave(void *handle
, enum stlink_mode type
)
512 struct stlink_usb_handle_s
*h
;
514 assert(handle
!= NULL
);
516 h
= (struct stlink_usb_handle_s
*)handle
;
518 stlink_usb_init_buffer(handle
, STLINK_NULL_EP
, 0);
521 case STLINK_MODE_DEBUG_JTAG
:
522 case STLINK_MODE_DEBUG_SWD
:
523 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
524 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_EXIT
;
526 case STLINK_MODE_DEBUG_SWIM
:
527 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
528 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_EXIT
;
530 case STLINK_MODE_DFU
:
531 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DFU_COMMAND
;
532 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DFU_EXIT
;
534 case STLINK_MODE_MASS
:
539 res
= stlink_usb_xfer(handle
, 0, 0);
548 static int stlink_usb_init_mode(void *handle
)
552 enum stlink_mode emode
;
553 struct stlink_usb_handle_s
*h
;
555 assert(handle
!= NULL
);
557 h
= (struct stlink_usb_handle_s
*)handle
;
559 res
= stlink_usb_current_mode(handle
, &mode
);
564 LOG_DEBUG("MODE: 0x%02X", mode
);
566 /* try to exit current mode */
568 case STLINK_DEV_DFU_MODE
:
569 emode
= STLINK_MODE_DFU
;
571 case STLINK_DEV_DEBUG_MODE
:
572 emode
= STLINK_MODE_DEBUG_SWD
;
574 case STLINK_DEV_SWIM_MODE
:
575 emode
= STLINK_MODE_DEBUG_SWIM
;
577 case STLINK_DEV_BOOTLOADER_MODE
:
578 case STLINK_DEV_MASS_MODE
:
580 emode
= STLINK_MODE_UNKNOWN
;
584 if (emode
!= STLINK_MODE_UNKNOWN
) {
585 res
= stlink_usb_mode_leave(handle
, emode
);
591 res
= stlink_usb_current_mode(handle
, &mode
);
596 LOG_DEBUG("MODE: 0x%02X", mode
);
598 /* set selected mode */
599 switch (h
->transport
) {
600 case STLINK_TRANSPORT_SWD
:
601 emode
= STLINK_MODE_DEBUG_SWD
;
603 case STLINK_TRANSPORT_JTAG
:
604 emode
= STLINK_MODE_DEBUG_JTAG
;
606 case STLINK_TRANSPORT_SWIM
:
607 emode
= STLINK_MODE_DEBUG_SWIM
;
610 emode
= STLINK_MODE_UNKNOWN
;
614 if (emode
== STLINK_MODE_UNKNOWN
) {
615 LOG_ERROR("selected mode (transport) not supported");
619 res
= stlink_usb_mode_enter(handle
, emode
);
624 res
= stlink_usb_current_mode(handle
, &mode
);
629 LOG_DEBUG("MODE: 0x%02X", mode
);
635 static int stlink_usb_idcode(void *handle
, uint32_t *idcode
)
638 struct stlink_usb_handle_s
*h
;
640 assert(handle
!= NULL
);
642 h
= (struct stlink_usb_handle_s
*)handle
;
644 stlink_usb_init_buffer(handle
, STLINK_RX_EP
, 4);
646 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
647 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_READCOREID
;
649 res
= stlink_usb_xfer(handle
, h
->databuf
, 4);
654 *idcode
= le_to_h_u32(h
->databuf
);
656 LOG_DEBUG("IDCODE: 0x%08X", *idcode
);
661 static int stlink_usb_v2_read_debug_reg(void *handle
, uint32_t addr
, uint32_t *val
)
663 struct stlink_usb_handle_s
*h
;
666 assert(handle
!= NULL
);
668 h
= (struct stlink_usb_handle_s
*)handle
;
670 stlink_usb_init_buffer(handle
, STLINK_RX_EP
, 8);
672 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
673 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_READDEBUGREG
;
674 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
677 res
= stlink_usb_xfer(handle
, h
->databuf
, 8);
682 *val
= le_to_h_u32(h
->databuf
+ 4);
684 return h
->databuf
[0] == STLINK_DEBUG_ERR_OK
? ERROR_OK
: ERROR_FAIL
;
687 static int stlink_usb_write_debug_reg(void *handle
, uint32_t addr
, uint32_t val
)
690 struct stlink_usb_handle_s
*h
;
692 assert(handle
!= NULL
);
694 h
= (struct stlink_usb_handle_s
*)handle
;
696 stlink_usb_init_buffer(handle
, STLINK_RX_EP
, 2);
698 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
699 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
700 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_WRITEDEBUGREG
;
702 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_WRITEDEBUGREG
;
703 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
705 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, val
);
708 res
= stlink_usb_xfer(handle
, h
->databuf
, 2);
713 return h
->databuf
[0] == STLINK_DEBUG_ERR_OK
? ERROR_OK
: ERROR_FAIL
;
716 static enum target_state
stlink_usb_v2_get_status(void *handle
)
721 result
= stlink_usb_v2_read_debug_reg(handle
, DCB_DHCSR
, &status
);
722 if (result
!= ERROR_OK
)
723 return TARGET_UNKNOWN
;
726 return TARGET_HALTED
;
727 else if (status
& S_RESET_ST
)
730 return TARGET_RUNNING
;
734 static enum target_state
stlink_usb_state(void *handle
)
737 struct stlink_usb_handle_s
*h
;
739 assert(handle
!= NULL
);
741 h
= (struct stlink_usb_handle_s
*)handle
;
743 if (h
->jtag_api
== STLINK_JTAG_API_V2
)
744 return stlink_usb_v2_get_status(handle
);
746 stlink_usb_init_buffer(handle
, STLINK_RX_EP
, 2);
748 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
749 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_GETSTATUS
;
751 res
= stlink_usb_xfer(handle
, h
->databuf
, 2);
754 return TARGET_UNKNOWN
;
756 if (h
->databuf
[0] == STLINK_CORE_RUNNING
)
757 return TARGET_RUNNING
;
758 if (h
->databuf
[0] == STLINK_CORE_HALTED
)
759 return TARGET_HALTED
;
761 return TARGET_UNKNOWN
;
765 static int stlink_usb_reset(void *handle
)
768 struct stlink_usb_handle_s
*h
;
770 assert(handle
!= NULL
);
772 h
= (struct stlink_usb_handle_s
*)handle
;
774 stlink_usb_init_buffer(handle
, STLINK_RX_EP
, 2);
776 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
778 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
779 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_RESETSYS
;
781 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_RESETSYS
;
783 res
= stlink_usb_xfer(handle
, h
->databuf
, 2);
788 LOG_DEBUG("RESET: 0x%08X", h
->databuf
[0]);
790 /* the following is not a error under swd (using hardware srst), so return success */
791 if (h
->databuf
[0] == STLINK_SWD_AP_WAIT
|| h
->databuf
[0] == STLINK_SWD_DP_WAIT
)
794 return h
->databuf
[0] == STLINK_DEBUG_ERR_OK
? ERROR_OK
: ERROR_FAIL
;
797 static int stlink_usb_assert_srst(void *handle
, int srst
)
800 struct stlink_usb_handle_s
*h
;
802 assert(handle
!= NULL
);
804 h
= (struct stlink_usb_handle_s
*)handle
;
806 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
807 return ERROR_COMMAND_NOTFOUND
;
809 stlink_usb_init_buffer(handle
, STLINK_RX_EP
, 2);
811 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
812 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_DRIVE_NRST
;
813 h
->cmdbuf
[h
->cmdidx
++] = srst
;
815 res
= stlink_usb_xfer(handle
, h
->databuf
, 2);
820 return h
->databuf
[0] == STLINK_DEBUG_ERR_OK
? ERROR_OK
: ERROR_FAIL
;
824 static int stlink_usb_run(void *handle
)
827 struct stlink_usb_handle_s
*h
;
829 assert(handle
!= NULL
);
831 h
= (struct stlink_usb_handle_s
*)handle
;
833 if (h
->jtag_api
== STLINK_JTAG_API_V2
)
834 return stlink_usb_write_debug_reg(handle
, DCB_DHCSR
, DBGKEY
|C_DEBUGEN
);
836 stlink_usb_init_buffer(handle
, STLINK_RX_EP
, 2);
838 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
839 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_RUNCORE
;
841 res
= stlink_usb_xfer(handle
, h
->databuf
, 2);
846 return h
->databuf
[0] == STLINK_DEBUG_ERR_OK
? ERROR_OK
: ERROR_FAIL
;
850 static int stlink_usb_halt(void *handle
)
853 struct stlink_usb_handle_s
*h
;
855 assert(handle
!= NULL
);
857 h
= (struct stlink_usb_handle_s
*)handle
;
859 if (h
->jtag_api
== STLINK_JTAG_API_V2
)
860 return stlink_usb_write_debug_reg(handle
, DCB_DHCSR
, DBGKEY
|C_HALT
|C_DEBUGEN
);
862 stlink_usb_init_buffer(handle
, STLINK_RX_EP
, 2);
864 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
865 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_FORCEDEBUG
;
867 res
= stlink_usb_xfer(handle
, h
->databuf
, 2);
872 return h
->databuf
[0] == STLINK_DEBUG_ERR_OK
? ERROR_OK
: ERROR_FAIL
;
876 static int stlink_usb_step(void *handle
)
879 struct stlink_usb_handle_s
*h
;
881 assert(handle
!= NULL
);
883 h
= (struct stlink_usb_handle_s
*)handle
;
885 if (h
->jtag_api
== STLINK_JTAG_API_V2
) {
886 /* TODO: this emulates the v1 api, it should really use a similar auto mask isr
887 * that the cortex-m3 currently does. */
888 stlink_usb_write_debug_reg(handle
, DCB_DHCSR
, DBGKEY
|C_HALT
|C_MASKINTS
|C_DEBUGEN
);
889 stlink_usb_write_debug_reg(handle
, DCB_DHCSR
, DBGKEY
|C_STEP
|C_MASKINTS
|C_DEBUGEN
);
890 return stlink_usb_write_debug_reg(handle
, DCB_DHCSR
, DBGKEY
|C_HALT
|C_DEBUGEN
);
893 stlink_usb_init_buffer(handle
, STLINK_RX_EP
, 2);
895 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
896 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_STEPCORE
;
898 res
= stlink_usb_xfer(handle
, h
->databuf
, 2);
903 return h
->databuf
[0] == STLINK_DEBUG_ERR_OK
? ERROR_OK
: ERROR_FAIL
;
907 static int stlink_usb_read_regs(void *handle
)
910 struct stlink_usb_handle_s
*h
;
912 assert(handle
!= NULL
);
914 h
= (struct stlink_usb_handle_s
*)handle
;
916 stlink_usb_init_buffer(handle
, STLINK_RX_EP
, 84);
918 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
919 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
920 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_READALLREGS
;
922 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_READALLREGS
;
924 res
= stlink_usb_xfer(handle
, h
->databuf
, 84);
933 static int stlink_usb_read_reg(void *handle
, int num
, uint32_t *val
)
936 struct stlink_usb_handle_s
*h
;
938 assert(handle
!= NULL
);
940 h
= (struct stlink_usb_handle_s
*)handle
;
942 stlink_usb_init_buffer(handle
, STLINK_RX_EP
, h
->jtag_api
== STLINK_JTAG_API_V1
? 4 : 8);
944 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
945 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
946 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_READREG
;
948 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_READREG
;
949 h
->cmdbuf
[h
->cmdidx
++] = num
;
951 res
= stlink_usb_xfer(handle
, h
->databuf
, h
->jtag_api
== STLINK_JTAG_API_V1
? 4 : 8);
956 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
957 *val
= le_to_h_u32(h
->databuf
);
959 *val
= le_to_h_u32(h
->databuf
+ 4);
960 return h
->databuf
[0] == STLINK_DEBUG_ERR_OK
? ERROR_OK
: ERROR_FAIL
;
967 static int stlink_usb_write_reg(void *handle
, int num
, uint32_t val
)
970 struct stlink_usb_handle_s
*h
;
972 assert(handle
!= NULL
);
974 h
= (struct stlink_usb_handle_s
*)handle
;
976 stlink_usb_init_buffer(handle
, STLINK_RX_EP
, 2);
978 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
979 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
980 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_WRITEREG
;
982 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_WRITEREG
;
983 h
->cmdbuf
[h
->cmdidx
++] = num
;
984 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, val
);
987 res
= stlink_usb_xfer(handle
, h
->databuf
, 2);
992 return h
->databuf
[0] == STLINK_DEBUG_ERR_OK
? ERROR_OK
: ERROR_FAIL
;
995 static int stlink_usb_get_rw_status(void *handle
)
998 struct stlink_usb_handle_s
*h
;
1000 assert(handle
!= NULL
);
1002 h
= (struct stlink_usb_handle_s
*)handle
;
1004 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
1007 stlink_usb_init_buffer(handle
, STLINK_RX_EP
, 2);
1009 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1010 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_GETLASTRWSTATUS
;
1012 res
= stlink_usb_xfer(handle
, h
->databuf
, 2);
1014 if (res
!= ERROR_OK
)
1017 return h
->databuf
[0] == STLINK_DEBUG_ERR_OK
? ERROR_OK
: res
;
1021 static int stlink_usb_read_mem8(void *handle
, uint32_t addr
, uint16_t len
,
1025 uint16_t read_len
= len
;
1026 struct stlink_usb_handle_s
*h
;
1028 assert(handle
!= NULL
);
1030 h
= (struct stlink_usb_handle_s
*)handle
;
1032 stlink_usb_init_buffer(handle
, STLINK_RX_EP
, read_len
);
1034 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1035 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_READMEM_8BIT
;
1036 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
1038 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
1041 /* we need to fix read length for single bytes */
1045 res
= stlink_usb_xfer(handle
, h
->databuf
, read_len
);
1047 if (res
!= ERROR_OK
)
1050 memcpy(buffer
, h
->databuf
, len
);
1052 return stlink_usb_get_rw_status(handle
);
1056 static int stlink_usb_write_mem8(void *handle
, uint32_t addr
, uint16_t len
,
1057 const uint8_t *buffer
)
1060 struct stlink_usb_handle_s
*h
;
1062 assert(handle
!= NULL
);
1064 h
= (struct stlink_usb_handle_s
*)handle
;
1066 stlink_usb_init_buffer(handle
, STLINK_TX_EP
, len
);
1068 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1069 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_WRITEMEM_8BIT
;
1070 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
1072 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
1075 res
= stlink_usb_xfer(handle
, buffer
, len
);
1077 if (res
!= ERROR_OK
)
1080 return stlink_usb_get_rw_status(handle
);
1084 static int stlink_usb_read_mem32(void *handle
, uint32_t addr
, uint16_t len
,
1088 struct stlink_usb_handle_s
*h
;
1090 assert(handle
!= NULL
);
1092 h
= (struct stlink_usb_handle_s
*)handle
;
1096 stlink_usb_init_buffer(handle
, STLINK_RX_EP
, len
);
1098 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1099 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_READMEM_32BIT
;
1100 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
1102 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
1105 res
= stlink_usb_xfer(handle
, h
->databuf
, len
);
1107 if (res
!= ERROR_OK
)
1110 memcpy(buffer
, h
->databuf
, len
);
1112 return stlink_usb_get_rw_status(handle
);
1116 static int stlink_usb_write_mem32(void *handle
, uint32_t addr
, uint16_t len
,
1117 const uint8_t *buffer
)
1120 struct stlink_usb_handle_s
*h
;
1122 assert(handle
!= NULL
);
1124 h
= (struct stlink_usb_handle_s
*)handle
;
1128 stlink_usb_init_buffer(handle
, STLINK_TX_EP
, len
);
1130 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1131 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_WRITEMEM_32BIT
;
1132 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
1134 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
1137 res
= stlink_usb_xfer(handle
, buffer
, len
);
1139 if (res
!= ERROR_OK
)
1142 return stlink_usb_get_rw_status(handle
);
1146 static int stlink_usb_open(struct stlink_interface_param_s
*param
, void **fd
)
1149 struct stlink_usb_handle_s
*h
;
1150 enum stlink_jtag_api_version api
;
1152 LOG_DEBUG("stlink_usb_open");
1154 h
= malloc(sizeof(struct stlink_usb_handle_s
));
1157 LOG_DEBUG("malloc failed");
1161 h
->transport
= param
->transport
;
1163 const uint16_t vids
[] = { param
->vid
, 0 };
1164 const uint16_t pids
[] = { param
->pid
, 0 };
1166 LOG_DEBUG("transport: %d vid: 0x%04x pid: 0x%04x", param
->transport
,
1167 param
->vid
, param
->pid
);
1169 if (jtag_libusb_open(vids
, pids
, &h
->fd
) != ERROR_OK
) {
1170 LOG_ERROR("open failed");
1174 jtag_libusb_set_configuration(h
->fd
, 0);
1176 if (jtag_libusb_claim_interface(h
->fd
, 0) != ERROR_OK
) {
1177 LOG_DEBUG("claim interface failed");
1181 /* wrap version for first read */
1182 switch (param
->pid
) {
1184 h
->version
.stlink
= 1;
1187 h
->version
.stlink
= 2;
1191 /* get the device version */
1192 err
= stlink_usb_version(h
);
1194 if (err
!= ERROR_OK
) {
1195 LOG_ERROR("read version failed");
1196 jtag_libusb_close(h
->fd
);
1201 /* compare usb vid/pid */
1202 if ((param
->vid
!= h
->vid
) || (param
->pid
!= h
->pid
))
1203 LOG_INFO("vid/pid are not identical: 0x%04X/0x%04X 0x%04X/0x%04X",
1204 param
->vid
, param
->pid
,
1207 /* check if mode is supported */
1210 switch (h
->transport
) {
1211 case STLINK_TRANSPORT_SWD
:
1212 case STLINK_TRANSPORT_JTAG
:
1213 if (h
->version
.jtag
== 0)
1216 case STLINK_TRANSPORT_SWIM
:
1217 if (h
->version
.swim
== 0)
1225 if (err
!= ERROR_OK
) {
1226 LOG_ERROR("mode (transport) not supported by device");
1227 jtag_libusb_close(h
->fd
);
1232 api
= h
->version
.jtag_api_max
;
1234 /* check that user has not requested certain api version
1235 * and if they have check it is supported */
1236 if ((param
->api
!= 0) && (param
->api
<= h
->version
.jtag_api_max
)) {
1238 LOG_INFO("using stlink api v%d", api
);
1241 /* set the used jtag api, this will default to the newest supported version */
1244 /* initialize the debug hardware */
1245 err
= stlink_usb_init_mode(h
);
1247 if (err
!= ERROR_OK
) {
1248 LOG_ERROR("init mode failed");
1249 jtag_libusb_close(h
->fd
);
1260 static int stlink_usb_close(void *fd
)
1266 struct stlink_layout_api_s stlink_usb_layout_api
= {
1268 .open
= stlink_usb_open
,
1270 .close
= stlink_usb_close
,
1272 .idcode
= stlink_usb_idcode
,
1274 .state
= stlink_usb_state
,
1276 .reset
= stlink_usb_reset
,
1278 .assert_srst
= stlink_usb_assert_srst
,
1280 .run
= stlink_usb_run
,
1282 .halt
= stlink_usb_halt
,
1284 .step
= stlink_usb_step
,
1286 .read_regs
= stlink_usb_read_regs
,
1288 .read_reg
= stlink_usb_read_reg
,
1290 .write_reg
= stlink_usb_write_reg
,
1292 .read_mem8
= stlink_usb_read_mem8
,
1294 .write_mem8
= stlink_usb_write_mem8
,
1296 .read_mem32
= stlink_usb_read_mem32
,
1298 .write_mem32
= stlink_usb_write_mem32
,
1300 .write_debug_reg
= stlink_usb_write_debug_reg
Linking to existing account procedure
If you already have an account and want to add another login method
you
MUST first sign in with your existing account and
then change URL to read
https://review.openocd.org/login/?link
to get to this page again but this time it'll work for linking. Thank you.
SSH host keys fingerprints
1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=.. |
|+o.. . |
|*.o . . |
|+B . . . |
|Bo. = o S |
|Oo.+ + = |
|oB=.* = . o |
| =+=.+ + E |
|. .=o . o |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)