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_CORE_RUNNING 0x80
99 #define STLINK_CORE_HALTED 0x81
100 #define STLINK_CORE_STAT_UNKNOWN -1
102 #define STLINK_GET_VERSION 0xF1
103 #define STLINK_DEBUG_COMMAND 0xF2
104 #define STLINK_DFU_COMMAND 0xF3
105 #define STLINK_SWIM_COMMAND 0xF4
106 #define STLINK_GET_CURRENT_MODE 0xF5
108 #define STLINK_DEV_DFU_MODE 0x00
109 #define STLINK_DEV_MASS_MODE 0x01
110 #define STLINK_DEV_DEBUG_MODE 0x02
111 #define STLINK_DEV_SWIM_MODE 0x03
112 #define STLINK_DEV_BOOTLOADER_MODE 0x04
113 #define STLINK_DEV_UNKNOWN_MODE -1
115 #define STLINK_DFU_EXIT 0x07
117 #define STLINK_SWIM_ENTER 0x00
118 #define STLINK_SWIM_EXIT 0x01
120 #define STLINK_DEBUG_ENTER_JTAG 0x00
121 #define STLINK_DEBUG_GETSTATUS 0x01
122 #define STLINK_DEBUG_FORCEDEBUG 0x02
123 #define STLINK_DEBUG_APIV1_RESETSYS 0x03
124 #define STLINK_DEBUG_APIV1_READALLREGS 0x04
125 #define STLINK_DEBUG_APIV1_READREG 0x05
126 #define STLINK_DEBUG_APIV1_WRITEREG 0x06
127 #define STLINK_DEBUG_READMEM_32BIT 0x07
128 #define STLINK_DEBUG_WRITEMEM_32BIT 0x08
129 #define STLINK_DEBUG_RUNCORE 0x09
130 #define STLINK_DEBUG_STEPCORE 0x0a
131 #define STLINK_DEBUG_APIV1_SETFP 0x0b
132 #define STLINK_DEBUG_READMEM_8BIT 0x0c
133 #define STLINK_DEBUG_WRITEMEM_8BIT 0x0d
134 #define STLINK_DEBUG_APIV1_CLEARFP 0x0e
135 #define STLINK_DEBUG_APIV1_WRITEDEBUGREG 0x0f
136 #define STLINK_DEBUG_APIV1_SETWATCHPOINT 0x10
138 #define STLINK_DEBUG_ENTER_JTAG 0x00
139 #define STLINK_DEBUG_ENTER_SWD 0xa3
141 #define STLINK_DEBUG_APIV1_ENTER 0x20
142 #define STLINK_DEBUG_EXIT 0x21
143 #define STLINK_DEBUG_READCOREID 0x22
145 #define STLINK_DEBUG_APIV2_ENTER 0x30
146 #define STLINK_DEBUG_APIV2_READ_IDCODES 0x31
147 #define STLINK_DEBUG_APIV2_RESETSYS 0x32
148 #define STLINK_DEBUG_APIV2_READREG 0x33
149 #define STLINK_DEBUG_APIV2_WRITEREG 0x34
150 #define STLINK_DEBUG_APIV2_WRITEDEBUGREG 0x35
151 #define STLINK_DEBUG_APIV2_READDEBUGREG 0x36
153 #define STLINK_DEBUG_APIV2_READALLREGS 0x3A
154 #define STLINK_DEBUG_APIV2_GETLASTRWSTATUS 0x3B
155 #define STLINK_DEBUG_APIV2_DRIVE_NRST 0x3C
157 #define STLINK_DEBUG_APIV2_DRIVE_NRST_LOW 0x00
158 #define STLINK_DEBUG_APIV2_DRIVE_NRST_HIGH 0x01
159 #define STLINK_DEBUG_APIV2_DRIVE_NRST_PULSE 0x02
163 STLINK_MODE_UNKNOWN
= 0,
166 STLINK_MODE_DEBUG_JTAG
,
167 STLINK_MODE_DEBUG_SWD
,
168 STLINK_MODE_DEBUG_SWIM
171 #define REQUEST_SENSE 0x03
172 #define REQUEST_SENSE_LENGTH 18
174 static void stlink_usb_init_buffer(void *handle
, uint8_t direction
, uint32_t size
);
177 static int stlink_usb_xfer_v1_get_status(void *handle
)
179 struct stlink_usb_handle_s
*h
;
181 assert(handle
!= NULL
);
183 h
= (struct stlink_usb_handle_s
*)handle
;
186 memset(h
->cmdbuf
, 0, STLINK_SG_SIZE
);
188 if (jtag_libusb_bulk_read(h
->fd
, STLINK_RX_EP
, (char *)h
->cmdbuf
,
194 t1
= buf_get_u32(h
->cmdbuf
, 0, 32);
197 if (t1
!= 0x53425355)
205 if (h
->cmdbuf
[12] != 0)
212 static int stlink_usb_xfer_rw(void *handle
, int cmdsize
, const uint8_t *buf
, int size
)
214 struct stlink_usb_handle_s
*h
;
216 assert(handle
!= NULL
);
218 h
= (struct stlink_usb_handle_s
*)handle
;
220 if (jtag_libusb_bulk_write(h
->fd
, STLINK_TX_EP
, (char *)h
->cmdbuf
, cmdsize
,
225 if (h
->direction
== STLINK_TX_EP
&& size
) {
226 if (jtag_libusb_bulk_write(h
->fd
, STLINK_TX_EP
, (char *)buf
,
227 size
, 1000) != size
) {
228 LOG_DEBUG("bulk write failed");
231 } else if (h
->direction
== STLINK_RX_EP
&& size
) {
232 if (jtag_libusb_bulk_read(h
->fd
, STLINK_RX_EP
, (char *)buf
,
233 size
, 1000) != size
) {
234 LOG_DEBUG("bulk read failed");
243 static int stlink_usb_xfer_v1_get_sense(void *handle
)
246 struct stlink_usb_handle_s
*h
;
248 assert(handle
!= NULL
);
250 h
= (struct stlink_usb_handle_s
*)handle
;
252 stlink_usb_init_buffer(handle
, STLINK_RX_EP
, 16);
254 h
->cmdbuf
[h
->cmdidx
++] = REQUEST_SENSE
;
255 h
->cmdbuf
[h
->cmdidx
++] = 0;
256 h
->cmdbuf
[h
->cmdidx
++] = 0;
257 h
->cmdbuf
[h
->cmdidx
++] = 0;
258 h
->cmdbuf
[h
->cmdidx
++] = REQUEST_SENSE_LENGTH
;
260 res
= stlink_usb_xfer_rw(handle
, REQUEST_SENSE_LENGTH
, h
->databuf
, 16);
265 if (stlink_usb_xfer_v1_get_status(handle
) != ERROR_OK
)
272 static int stlink_usb_xfer(void *handle
, const uint8_t *buf
, int size
)
274 int err
, cmdsize
= STLINK_CMD_SIZE_V2
;
275 struct stlink_usb_handle_s
*h
;
277 assert(handle
!= NULL
);
279 h
= (struct stlink_usb_handle_s
*)handle
;
281 if (h
->version
.stlink
== 1)
282 cmdsize
= STLINK_SG_SIZE
;
284 err
= stlink_usb_xfer_rw(handle
, cmdsize
, buf
, size
);
289 if (h
->version
.stlink
== 1) {
290 if (stlink_usb_xfer_v1_get_status(handle
) != ERROR_OK
) {
291 /* check csw status */
292 if (h
->cmdbuf
[12] == 1) {
293 LOG_DEBUG("get sense");
294 if (stlink_usb_xfer_v1_get_sense(handle
) != ERROR_OK
)
305 static void stlink_usb_xfer_v1_create_cmd(void *handle
, uint8_t direction
, uint32_t size
)
307 struct stlink_usb_handle_s
*h
;
309 h
= (struct stlink_usb_handle_s
*)handle
;
311 /* fill the send buffer */
312 strcpy((char *)h
->cmdbuf
, "USBC");
314 /* csw tag not used */
316 buf_set_u32(h
->cmdbuf
+h
->cmdidx
, 0, 32, size
);
318 h
->cmdbuf
[h
->cmdidx
++] = (direction
== STLINK_RX_EP
? ENDPOINT_IN
: ENDPOINT_OUT
);
319 h
->cmdbuf
[h
->cmdidx
++] = 0; /* lun */
320 h
->cmdbuf
[h
->cmdidx
++] = STLINK_CMD_SIZE_V1
;
324 static void stlink_usb_init_buffer(void *handle
, uint8_t direction
, uint32_t size
)
326 struct stlink_usb_handle_s
*h
;
328 h
= (struct stlink_usb_handle_s
*)handle
;
330 h
->direction
= direction
;
334 memset(h
->cmdbuf
, 0, STLINK_SG_SIZE
);
335 memset(h
->databuf
, 0, STLINK_DATA_SIZE
);
337 if (h
->version
.stlink
== 1)
338 stlink_usb_xfer_v1_create_cmd(handle
, direction
, size
);
341 static const char * const stlink_usb_error_msg
[] = {
346 static int stlink_usb_error_check(void *handle
)
349 const char *err_msg
= 0;
350 struct stlink_usb_handle_s
*h
;
352 assert(handle
!= NULL
);
354 h
= (struct stlink_usb_handle_s
*)handle
;
356 /* TODO: no error checking yet on api V1 */
357 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
358 h
->databuf
[0] = STLINK_DEBUG_ERR_OK
;
360 switch (h
->databuf
[0]) {
361 case STLINK_DEBUG_ERR_OK
:
364 case STLINK_DEBUG_ERR_FAULT
:
366 err_msg
= stlink_usb_error_msg
[0];
372 LOG_DEBUG("status error: %d ('%s')", h
->databuf
[0], err_msg
);
378 static int stlink_usb_version(void *handle
)
382 struct stlink_usb_handle_s
*h
;
384 assert(handle
!= NULL
);
386 h
= (struct stlink_usb_handle_s
*)handle
;
388 stlink_usb_init_buffer(handle
, STLINK_RX_EP
, 6);
390 h
->cmdbuf
[h
->cmdidx
++] = STLINK_GET_VERSION
;
392 res
= stlink_usb_xfer(handle
, h
->databuf
, 6);
397 v
= (h
->databuf
[0] << 8) | h
->databuf
[1];
399 h
->version
.stlink
= (v
>> 12) & 0x0f;
400 h
->version
.jtag
= (v
>> 6) & 0x3f;
401 h
->version
.swim
= v
& 0x3f;
402 h
->vid
= buf_get_u32(h
->databuf
, 16, 16);
403 h
->pid
= buf_get_u32(h
->databuf
, 32, 16);
405 /* set the supported jtag api version
406 * API V2 is supported since JTAG V11
408 if (h
->version
.jtag
>= 11)
409 h
->version
.jtag_api_max
= STLINK_JTAG_API_V2
;
411 h
->version
.jtag_api_max
= STLINK_JTAG_API_V1
;
413 LOG_DEBUG("STLINK v%d JTAG v%d API v%d SWIM v%d VID 0x%04X PID 0x%04X",
416 (h
->version
.jtag_api_max
== STLINK_JTAG_API_V1
) ? 1 : 2,
425 static int stlink_usb_current_mode(void *handle
, uint8_t *mode
)
428 struct stlink_usb_handle_s
*h
;
430 assert(handle
!= NULL
);
432 h
= (struct stlink_usb_handle_s
*)handle
;
434 stlink_usb_init_buffer(handle
, STLINK_RX_EP
, 2);
436 h
->cmdbuf
[h
->cmdidx
++] = STLINK_GET_CURRENT_MODE
;
438 res
= stlink_usb_xfer(handle
, h
->databuf
, 2);
443 *mode
= h
->databuf
[0];
449 static int stlink_usb_mode_enter(void *handle
, enum stlink_mode type
)
453 struct stlink_usb_handle_s
*h
;
455 assert(handle
!= NULL
);
457 h
= (struct stlink_usb_handle_s
*)handle
;
459 /* on api V2 we are able the read the latest command
461 * TODO: we need the test on api V1 too
463 if (h
->jtag_api
== STLINK_JTAG_API_V2
)
466 stlink_usb_init_buffer(handle
, STLINK_RX_EP
, rx_size
);
469 case STLINK_MODE_DEBUG_JTAG
:
470 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
471 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
472 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_ENTER
;
474 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_ENTER
;
475 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_ENTER_JTAG
;
477 case STLINK_MODE_DEBUG_SWD
:
478 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
479 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
480 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_ENTER
;
482 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_ENTER
;
483 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_ENTER_SWD
;
485 case STLINK_MODE_DEBUG_SWIM
:
486 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
487 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_ENTER
;
489 case STLINK_MODE_DFU
:
490 case STLINK_MODE_MASS
:
495 res
= stlink_usb_xfer(handle
, h
->databuf
, rx_size
);
500 res
= stlink_usb_error_check(h
);
506 static int stlink_usb_mode_leave(void *handle
, enum stlink_mode type
)
509 struct stlink_usb_handle_s
*h
;
511 assert(handle
!= NULL
);
513 h
= (struct stlink_usb_handle_s
*)handle
;
515 stlink_usb_init_buffer(handle
, STLINK_NULL_EP
, 0);
518 case STLINK_MODE_DEBUG_JTAG
:
519 case STLINK_MODE_DEBUG_SWD
:
520 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
521 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_EXIT
;
523 case STLINK_MODE_DEBUG_SWIM
:
524 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
525 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_EXIT
;
527 case STLINK_MODE_DFU
:
528 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DFU_COMMAND
;
529 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DFU_EXIT
;
531 case STLINK_MODE_MASS
:
536 res
= stlink_usb_xfer(handle
, 0, 0);
545 static int stlink_usb_init_mode(void *handle
)
549 enum stlink_mode emode
;
550 struct stlink_usb_handle_s
*h
;
552 assert(handle
!= NULL
);
554 h
= (struct stlink_usb_handle_s
*)handle
;
556 res
= stlink_usb_current_mode(handle
, &mode
);
561 LOG_DEBUG("MODE: 0x%02X", mode
);
563 /* try to exit current mode */
565 case STLINK_DEV_DFU_MODE
:
566 emode
= STLINK_MODE_DFU
;
568 case STLINK_DEV_DEBUG_MODE
:
569 emode
= STLINK_MODE_DEBUG_SWD
;
571 case STLINK_DEV_SWIM_MODE
:
572 emode
= STLINK_MODE_DEBUG_SWIM
;
574 case STLINK_DEV_BOOTLOADER_MODE
:
575 case STLINK_DEV_MASS_MODE
:
577 emode
= STLINK_MODE_UNKNOWN
;
581 if (emode
!= STLINK_MODE_UNKNOWN
) {
582 res
= stlink_usb_mode_leave(handle
, emode
);
588 res
= stlink_usb_current_mode(handle
, &mode
);
593 LOG_DEBUG("MODE: 0x%02X", mode
);
595 /* set selected mode */
596 switch (h
->transport
) {
597 case STLINK_TRANSPORT_SWD
:
598 emode
= STLINK_MODE_DEBUG_SWD
;
600 case STLINK_TRANSPORT_JTAG
:
601 emode
= STLINK_MODE_DEBUG_JTAG
;
603 case STLINK_TRANSPORT_SWIM
:
604 emode
= STLINK_MODE_DEBUG_SWIM
;
607 emode
= STLINK_MODE_UNKNOWN
;
611 if (emode
== STLINK_MODE_UNKNOWN
) {
612 LOG_ERROR("selected mode (transport) not supported");
616 res
= stlink_usb_mode_enter(handle
, emode
);
621 res
= stlink_usb_current_mode(handle
, &mode
);
626 LOG_DEBUG("MODE: 0x%02X", mode
);
632 static int stlink_usb_idcode(void *handle
, uint32_t *idcode
)
635 struct stlink_usb_handle_s
*h
;
637 assert(handle
!= NULL
);
639 h
= (struct stlink_usb_handle_s
*)handle
;
641 stlink_usb_init_buffer(handle
, STLINK_RX_EP
, 4);
643 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
644 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_READCOREID
;
646 res
= stlink_usb_xfer(handle
, h
->databuf
, 4);
651 *idcode
= le_to_h_u32(h
->databuf
);
653 LOG_DEBUG("IDCODE: 0x%08X", *idcode
);
658 static int stlink_usb_v2_read_debug_reg(void *handle
, uint32_t addr
, uint32_t *val
)
660 struct stlink_usb_handle_s
*h
;
663 assert(handle
!= NULL
);
665 h
= (struct stlink_usb_handle_s
*)handle
;
667 stlink_usb_init_buffer(handle
, STLINK_RX_EP
, 8);
669 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
670 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_READDEBUGREG
;
671 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
674 res
= stlink_usb_xfer(handle
, h
->databuf
, 8);
679 *val
= le_to_h_u32(h
->databuf
+ 4);
681 return h
->databuf
[0] == STLINK_DEBUG_ERR_OK
? ERROR_OK
: ERROR_FAIL
;
684 static int stlink_usb_write_debug_reg(void *handle
, uint32_t addr
, uint32_t val
)
687 struct stlink_usb_handle_s
*h
;
689 assert(handle
!= NULL
);
691 h
= (struct stlink_usb_handle_s
*)handle
;
693 stlink_usb_init_buffer(handle
, STLINK_RX_EP
, 2);
695 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
696 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
697 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_WRITEDEBUGREG
;
699 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_WRITEDEBUGREG
;
700 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
702 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, val
);
705 res
= stlink_usb_xfer(handle
, h
->databuf
, 2);
710 return h
->databuf
[0] == STLINK_DEBUG_ERR_OK
? ERROR_OK
: ERROR_FAIL
;
713 static enum target_state
stlink_usb_v2_get_status(void *handle
)
718 result
= stlink_usb_v2_read_debug_reg(handle
, DCB_DHCSR
, &status
);
719 if (result
!= ERROR_OK
)
720 return TARGET_UNKNOWN
;
723 return TARGET_HALTED
;
724 else if (status
& S_RESET_ST
)
727 return TARGET_RUNNING
;
731 static enum target_state
stlink_usb_state(void *handle
)
734 struct stlink_usb_handle_s
*h
;
736 assert(handle
!= NULL
);
738 h
= (struct stlink_usb_handle_s
*)handle
;
740 if (h
->jtag_api
== STLINK_JTAG_API_V2
)
741 return stlink_usb_v2_get_status(handle
);
743 stlink_usb_init_buffer(handle
, STLINK_RX_EP
, 2);
745 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
746 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_GETSTATUS
;
748 res
= stlink_usb_xfer(handle
, h
->databuf
, 2);
751 return TARGET_UNKNOWN
;
753 if (h
->databuf
[0] == STLINK_CORE_RUNNING
)
754 return TARGET_RUNNING
;
755 if (h
->databuf
[0] == STLINK_CORE_HALTED
)
756 return TARGET_HALTED
;
758 return TARGET_UNKNOWN
;
762 static int stlink_usb_reset(void *handle
)
765 struct stlink_usb_handle_s
*h
;
767 assert(handle
!= NULL
);
769 h
= (struct stlink_usb_handle_s
*)handle
;
771 stlink_usb_init_buffer(handle
, STLINK_RX_EP
, 2);
773 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
775 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
776 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_RESETSYS
;
778 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_RESETSYS
;
780 res
= stlink_usb_xfer(handle
, h
->databuf
, 2);
785 LOG_DEBUG("RESET: 0x%08X", h
->databuf
[0]);
787 return h
->databuf
[0] == STLINK_DEBUG_ERR_OK
? ERROR_OK
: ERROR_FAIL
;
790 static int stlink_usb_assert_srst(void *handle
, int srst
)
793 struct stlink_usb_handle_s
*h
;
795 assert(handle
!= NULL
);
797 h
= (struct stlink_usb_handle_s
*)handle
;
799 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
800 return ERROR_COMMAND_NOTFOUND
;
802 stlink_usb_init_buffer(handle
, STLINK_RX_EP
, 2);
804 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
805 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_DRIVE_NRST
;
806 h
->cmdbuf
[h
->cmdidx
++] = srst
;
808 res
= stlink_usb_xfer(handle
, h
->databuf
, 2);
813 return h
->databuf
[0] == STLINK_DEBUG_ERR_OK
? ERROR_OK
: ERROR_FAIL
;
817 static int stlink_usb_run(void *handle
)
820 struct stlink_usb_handle_s
*h
;
822 assert(handle
!= NULL
);
824 h
= (struct stlink_usb_handle_s
*)handle
;
826 if (h
->jtag_api
== STLINK_JTAG_API_V2
)
827 return stlink_usb_write_debug_reg(handle
, DCB_DHCSR
, DBGKEY
|C_DEBUGEN
);
829 stlink_usb_init_buffer(handle
, STLINK_RX_EP
, 2);
831 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
832 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_RUNCORE
;
834 res
= stlink_usb_xfer(handle
, h
->databuf
, 2);
839 return h
->databuf
[0] == STLINK_DEBUG_ERR_OK
? ERROR_OK
: ERROR_FAIL
;
843 static int stlink_usb_halt(void *handle
)
846 struct stlink_usb_handle_s
*h
;
848 assert(handle
!= NULL
);
850 h
= (struct stlink_usb_handle_s
*)handle
;
852 if (h
->jtag_api
== STLINK_JTAG_API_V2
)
853 return stlink_usb_write_debug_reg(handle
, DCB_DHCSR
, DBGKEY
|C_HALT
|C_DEBUGEN
);
855 stlink_usb_init_buffer(handle
, STLINK_RX_EP
, 2);
857 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
858 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_FORCEDEBUG
;
860 res
= stlink_usb_xfer(handle
, h
->databuf
, 2);
865 return h
->databuf
[0] == STLINK_DEBUG_ERR_OK
? ERROR_OK
: ERROR_FAIL
;
869 static int stlink_usb_step(void *handle
)
872 struct stlink_usb_handle_s
*h
;
874 assert(handle
!= NULL
);
876 h
= (struct stlink_usb_handle_s
*)handle
;
878 if (h
->jtag_api
== STLINK_JTAG_API_V2
) {
879 /* TODO: this emulates the v1 api, it should really use a similar auto mask isr
880 * that the cortex-m3 currently does. */
881 stlink_usb_write_debug_reg(handle
, DCB_DHCSR
, DBGKEY
|C_HALT
|C_MASKINTS
|C_DEBUGEN
);
882 stlink_usb_write_debug_reg(handle
, DCB_DHCSR
, DBGKEY
|C_STEP
|C_MASKINTS
|C_DEBUGEN
);
883 return stlink_usb_write_debug_reg(handle
, DCB_DHCSR
, DBGKEY
|C_HALT
|C_DEBUGEN
);
886 stlink_usb_init_buffer(handle
, STLINK_RX_EP
, 2);
888 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
889 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_STEPCORE
;
891 res
= stlink_usb_xfer(handle
, h
->databuf
, 2);
896 return h
->databuf
[0] == STLINK_DEBUG_ERR_OK
? ERROR_OK
: ERROR_FAIL
;
900 static int stlink_usb_read_regs(void *handle
)
903 struct stlink_usb_handle_s
*h
;
905 assert(handle
!= NULL
);
907 h
= (struct stlink_usb_handle_s
*)handle
;
909 stlink_usb_init_buffer(handle
, STLINK_RX_EP
, 84);
911 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
912 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
913 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_READALLREGS
;
915 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_READALLREGS
;
917 res
= stlink_usb_xfer(handle
, h
->databuf
, 84);
926 static int stlink_usb_read_reg(void *handle
, int num
, uint32_t *val
)
929 struct stlink_usb_handle_s
*h
;
931 assert(handle
!= NULL
);
933 h
= (struct stlink_usb_handle_s
*)handle
;
935 stlink_usb_init_buffer(handle
, STLINK_RX_EP
, h
->jtag_api
== STLINK_JTAG_API_V1
? 4 : 8);
937 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
938 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
939 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_READREG
;
941 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_READREG
;
942 h
->cmdbuf
[h
->cmdidx
++] = num
;
944 res
= stlink_usb_xfer(handle
, h
->databuf
, h
->jtag_api
== STLINK_JTAG_API_V1
? 4 : 8);
949 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
950 *val
= le_to_h_u32(h
->databuf
);
952 *val
= le_to_h_u32(h
->databuf
+ 4);
953 return h
->databuf
[0] == STLINK_DEBUG_ERR_OK
? ERROR_OK
: ERROR_FAIL
;
960 static int stlink_usb_write_reg(void *handle
, int num
, uint32_t val
)
963 struct stlink_usb_handle_s
*h
;
965 assert(handle
!= NULL
);
967 h
= (struct stlink_usb_handle_s
*)handle
;
969 stlink_usb_init_buffer(handle
, STLINK_RX_EP
, 2);
971 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
972 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
973 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_WRITEREG
;
975 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_WRITEREG
;
976 h
->cmdbuf
[h
->cmdidx
++] = num
;
977 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, val
);
980 res
= stlink_usb_xfer(handle
, h
->databuf
, 2);
985 return h
->databuf
[0] == STLINK_DEBUG_ERR_OK
? ERROR_OK
: ERROR_FAIL
;
988 static int stlink_usb_get_rw_status(void *handle
)
991 struct stlink_usb_handle_s
*h
;
993 assert(handle
!= NULL
);
995 h
= (struct stlink_usb_handle_s
*)handle
;
997 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
1000 stlink_usb_init_buffer(handle
, STLINK_RX_EP
, 2);
1002 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1003 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_GETLASTRWSTATUS
;
1005 res
= stlink_usb_xfer(handle
, h
->databuf
, 2);
1007 if (res
!= ERROR_OK
)
1010 return h
->databuf
[0] == STLINK_DEBUG_ERR_OK
? ERROR_OK
: res
;
1014 static int stlink_usb_read_mem8(void *handle
, uint32_t addr
, uint16_t len
,
1018 uint16_t read_len
= len
;
1019 struct stlink_usb_handle_s
*h
;
1021 assert(handle
!= NULL
);
1023 h
= (struct stlink_usb_handle_s
*)handle
;
1025 stlink_usb_init_buffer(handle
, STLINK_RX_EP
, read_len
);
1027 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1028 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_READMEM_8BIT
;
1029 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
1031 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
1034 /* we need to fix read length for single bytes */
1038 res
= stlink_usb_xfer(handle
, h
->databuf
, read_len
);
1040 if (res
!= ERROR_OK
)
1043 memcpy(buffer
, h
->databuf
, len
);
1045 return stlink_usb_get_rw_status(handle
);
1049 static int stlink_usb_write_mem8(void *handle
, uint32_t addr
, uint16_t len
,
1050 const uint8_t *buffer
)
1053 struct stlink_usb_handle_s
*h
;
1055 assert(handle
!= NULL
);
1057 h
= (struct stlink_usb_handle_s
*)handle
;
1059 stlink_usb_init_buffer(handle
, STLINK_TX_EP
, len
);
1061 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1062 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_WRITEMEM_8BIT
;
1063 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
1065 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
1068 res
= stlink_usb_xfer(handle
, buffer
, len
);
1070 if (res
!= ERROR_OK
)
1073 return stlink_usb_get_rw_status(handle
);
1077 static int stlink_usb_read_mem32(void *handle
, uint32_t addr
, uint16_t len
,
1081 struct stlink_usb_handle_s
*h
;
1083 assert(handle
!= NULL
);
1085 h
= (struct stlink_usb_handle_s
*)handle
;
1089 stlink_usb_init_buffer(handle
, STLINK_RX_EP
, len
);
1091 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1092 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_READMEM_32BIT
;
1093 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
1095 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
1098 res
= stlink_usb_xfer(handle
, h
->databuf
, len
);
1100 if (res
!= ERROR_OK
)
1103 memcpy(buffer
, h
->databuf
, len
);
1105 return stlink_usb_get_rw_status(handle
);
1109 static int stlink_usb_write_mem32(void *handle
, uint32_t addr
, uint16_t len
,
1110 const uint8_t *buffer
)
1113 struct stlink_usb_handle_s
*h
;
1115 assert(handle
!= NULL
);
1117 h
= (struct stlink_usb_handle_s
*)handle
;
1121 stlink_usb_init_buffer(handle
, STLINK_TX_EP
, len
);
1123 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1124 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_WRITEMEM_32BIT
;
1125 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
1127 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
1130 res
= stlink_usb_xfer(handle
, buffer
, len
);
1132 if (res
!= ERROR_OK
)
1135 return stlink_usb_get_rw_status(handle
);
1139 static int stlink_usb_open(struct stlink_interface_param_s
*param
, void **fd
)
1142 struct stlink_usb_handle_s
*h
;
1143 enum stlink_jtag_api_version api
;
1145 LOG_DEBUG("stlink_usb_open");
1147 h
= malloc(sizeof(struct stlink_usb_handle_s
));
1150 LOG_DEBUG("malloc failed");
1154 h
->transport
= param
->transport
;
1156 const uint16_t vids
[] = { param
->vid
, 0 };
1157 const uint16_t pids
[] = { param
->pid
, 0 };
1159 LOG_DEBUG("transport: %d vid: 0x%04x pid: 0x%04x", param
->transport
,
1160 param
->vid
, param
->pid
);
1162 if (jtag_libusb_open(vids
, pids
, &h
->fd
) != ERROR_OK
) {
1163 LOG_ERROR("open failed");
1167 jtag_libusb_set_configuration(h
->fd
, 0);
1169 if (jtag_libusb_claim_interface(h
->fd
, 0) != ERROR_OK
) {
1170 LOG_DEBUG("claim interface failed");
1174 /* wrap version for first read */
1175 switch (param
->pid
) {
1177 h
->version
.stlink
= 1;
1180 h
->version
.stlink
= 2;
1184 /* get the device version */
1185 err
= stlink_usb_version(h
);
1187 if (err
!= ERROR_OK
) {
1188 LOG_ERROR("read version failed");
1189 jtag_libusb_close(h
->fd
);
1194 /* compare usb vid/pid */
1195 if ((param
->vid
!= h
->vid
) || (param
->pid
!= h
->pid
))
1196 LOG_INFO("vid/pid are not identical: 0x%04X/0x%04X 0x%04X/0x%04X",
1197 param
->vid
, param
->pid
,
1200 /* check if mode is supported */
1203 switch (h
->transport
) {
1204 case STLINK_TRANSPORT_SWD
:
1205 case STLINK_TRANSPORT_JTAG
:
1206 if (h
->version
.jtag
== 0)
1209 case STLINK_TRANSPORT_SWIM
:
1210 if (h
->version
.swim
== 0)
1218 if (err
!= ERROR_OK
) {
1219 LOG_ERROR("mode (transport) not supported by device");
1220 jtag_libusb_close(h
->fd
);
1225 api
= h
->version
.jtag_api_max
;
1227 /* check that user has not requested certain api version
1228 * and if they have check it is supported */
1229 if ((param
->api
!= 0) && (param
->api
<= h
->version
.jtag_api_max
)) {
1231 LOG_INFO("using stlink api v%d", api
);
1234 /* set the used jtag api, this will default to the newest supported version */
1237 /* initialize the debug hardware */
1238 err
= stlink_usb_init_mode(h
);
1240 if (err
!= ERROR_OK
) {
1241 LOG_ERROR("init mode failed");
1242 jtag_libusb_close(h
->fd
);
1253 static int stlink_usb_close(void *fd
)
1259 struct stlink_layout_api_s stlink_usb_layout_api
= {
1261 .open
= stlink_usb_open
,
1263 .close
= stlink_usb_close
,
1265 .idcode
= stlink_usb_idcode
,
1267 .state
= stlink_usb_state
,
1269 .reset
= stlink_usb_reset
,
1271 .assert_srst
= stlink_usb_assert_srst
,
1273 .run
= stlink_usb_run
,
1275 .halt
= stlink_usb_halt
,
1277 .step
= stlink_usb_step
,
1279 .read_regs
= stlink_usb_read_regs
,
1281 .read_reg
= stlink_usb_read_reg
,
1283 .write_reg
= stlink_usb_write_reg
,
1285 .read_mem8
= stlink_usb_read_mem8
,
1287 .write_mem8
= stlink_usb_write_mem8
,
1289 .read_mem32
= stlink_usb_read_mem32
,
1291 .write_mem32
= stlink_usb_write_mem32
,
1293 .write_debug_reg
= stlink_usb_write_debug_reg