1 /***************************************************************************
2 * Copyright (C) 2011-2012 by Mathias Kuester *
3 * Mathias Kuester <kesmtp@freenet.de> *
5 * This code is based on https://github.com/texane/stlink *
7 * This program is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; either version 2 of the License, or *
10 * (at your option) any later version. *
12 * This program is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
17 * You should have received a copy of the GNU General Public License *
18 * along with this program; if not, write to the *
19 * Free Software Foundation, Inc., *
20 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
21 ***************************************************************************/
27 /* project specific includes */
28 #include <helper/binarybuffer.h>
29 #include <jtag/interface.h>
30 #include <jtag/stlink/stlink_layout.h>
31 #include <jtag/stlink/stlink_transport.h>
32 #include <jtag/stlink/stlink_interface.h>
33 #include <target/target.h>
35 #include "libusb_common.h"
37 #define ENDPOINT_IN 0x80
38 #define ENDPOINT_OUT 0x00
40 #define STLINK_RX_EP (1|ENDPOINT_IN)
41 #define STLINK_TX_EP (2|ENDPOINT_OUT)
42 #define STLINK_CMD_SIZE (16)
43 #define STLINK_TX_SIZE (4*128)
44 #define STLINK_RX_SIZE (4*128)
46 enum stlink_jtag_api_version
{
47 STLINK_JTAG_API_V1
= 0,
52 struct stlink_usb_version
{
59 /** highest supported jtag api version */
60 enum stlink_jtag_api_version jtag_api_max
;
64 struct stlink_usb_handle_s
{
66 struct jtag_libusb_device_handle
*fd
;
68 struct libusb_transfer
*trans
;
70 uint8_t txbuf
[STLINK_TX_SIZE
];
72 uint8_t rxbuf
[STLINK_RX_SIZE
];
74 enum stlink_transports transport
;
76 struct stlink_usb_version version
;
83 /** this is the currently used jtag api */
84 enum stlink_jtag_api_version jtag_api
;
87 #define STLINK_DEBUG_ERR_OK 0x80
88 #define STLINK_DEBUG_ERR_FAULT 0x81
89 #define STLINK_CORE_RUNNING 0x80
90 #define STLINK_CORE_HALTED 0x81
91 #define STLINK_CORE_STAT_UNKNOWN -1
93 #define STLINK_GET_VERSION 0xF1
94 #define STLINK_DEBUG_COMMAND 0xF2
95 #define STLINK_DFU_COMMAND 0xF3
96 #define STLINK_SWIM_COMMAND 0xF4
97 #define STLINK_GET_CURRENT_MODE 0xF5
99 #define STLINK_DEV_DFU_MODE 0x00
100 #define STLINK_DEV_MASS_MODE 0x01
101 #define STLINK_DEV_DEBUG_MODE 0x02
102 #define STLINK_DEV_SWIM_MODE 0x03
103 #define STLINK_DEV_BOOTLOADER_MODE 0x04
104 #define STLINK_DEV_UNKNOWN_MODE -1
106 #define STLINK_DFU_EXIT 0x07
108 #define STLINK_SWIM_ENTER 0x00
109 #define STLINK_SWIM_EXIT 0x01
111 #define STLINK_DEBUG_ENTER_JTAG 0x00
112 #define STLINK_DEBUG_GETSTATUS 0x01
113 #define STLINK_DEBUG_FORCEDEBUG 0x02
114 #define STLINK_DEBUG_APIV1_RESETSYS 0x03
115 #define STLINK_DEBUG_APIV1_READALLREGS 0x04
116 #define STLINK_DEBUG_APIV1_READREG 0x05
117 #define STLINK_DEBUG_APIV1_WRITEREG 0x06
118 #define STLINK_DEBUG_READMEM_32BIT 0x07
119 #define STLINK_DEBUG_WRITEMEM_32BIT 0x08
120 #define STLINK_DEBUG_RUNCORE 0x09
121 #define STLINK_DEBUG_STEPCORE 0x0a
122 #define STLINK_DEBUG_APIV1_SETFP 0x0b
123 #define STLINK_DEBUG_READMEM_8BIT 0x0c
124 #define STLINK_DEBUG_WRITEMEM_8BIT 0x0d
125 #define STLINK_DEBUG_APIV1_CLEARFP 0x0e
126 #define STLINK_DEBUG_APIV1_WRITEDEBUGREG 0x0f
127 #define STLINK_DEBUG_APIV1_SETWATCHPOINT 0x10
129 #define STLINK_DEBUG_ENTER_JTAG 0x00
130 #define STLINK_DEBUG_ENTER_SWD 0xa3
132 #define STLINK_DEBUG_APIV1_ENTER 0x20
133 #define STLINK_DEBUG_EXIT 0x21
134 #define STLINK_DEBUG_READCOREID 0x22
136 #define STLINK_DEBUG_APIV2_ENTER 0x30
137 #define STLINK_DEBUG_APIV2_READ_IDCODES 0x31
138 #define STLINK_DEBUG_APIV2_RESETSYS 0x32
139 #define STLINK_DEBUG_APIV2_READREG 0x33
140 #define STLINK_DEBUG_APIV2_WRITEREG 0x34
142 #define STLINK_DEBUG_APIV2_READALLREGS 0x3A
144 #define STLINK_DEBUG_APIV2_DRIVE_NRST 0x3C
146 #define STLINK_DEBUG_APIV2_DRIVE_NRST_LOW 0x00
147 #define STLINK_DEBUG_APIV2_DRIVE_NRST_HIGH 0x01
148 #define STLINK_DEBUG_APIV2_DRIVE_NRST_PULSE 0x02
152 STLINK_MODE_UNKNOWN
= 0,
155 STLINK_MODE_DEBUG_JTAG
,
156 STLINK_MODE_DEBUG_SWD
,
157 STLINK_MODE_DEBUG_SWIM
161 static int stlink_usb_xfer_v1_send_cmd(void *handle
, const uint8_t *cmd
, int cmdsize
, int ep
, int size
)
163 uint8_t sg_buffer
[31];
164 struct stlink_usb_handle_s
*h
;
166 assert(handle
!= NULL
);
167 assert(cmdsize
<= 16);
169 h
= (struct stlink_usb_handle_s
*)handle
;
170 h
->sg_tag
= (h
->sg_tag
+ 1) & 1; /* seriously? */
172 memset(sg_buffer
, 0, sizeof(sg_buffer
));
174 h_u32_to_le(sg_buffer
, 0x43425355); /* USBC */
175 h_u32_to_le(&sg_buffer
[4], h
->sg_tag
);
176 h_u32_to_le(&sg_buffer
[8], size
);
178 sg_buffer
[12] = (ep
== STLINK_RX_EP
? ENDPOINT_IN
: ENDPOINT_OUT
);
179 /* sg_buffer[13] = 0; */
180 sg_buffer
[14] = (uint8_t)cmdsize
;
182 memcpy(&sg_buffer
[15], cmd
, cmdsize
);
184 if (jtag_libusb_bulk_write(h
->fd
, STLINK_TX_EP
, (char *)sg_buffer
, sizeof(sg_buffer
),
185 1000) != sizeof(sg_buffer
)) {
186 LOG_DEBUG("send failed\n");
194 static int stlink_usb_xfer_v1_get_status(void *handle
, uint8_t *sg_buffer
, int len
)
196 struct stlink_usb_handle_s
*h
;
198 assert(handle
!= NULL
);
200 h
= (struct stlink_usb_handle_s
*)handle
;
203 memset(sg_buffer
, 0, len
);
205 if (jtag_libusb_bulk_read(h
->fd
, STLINK_RX_EP
, (char *)sg_buffer
,
209 uint32_t t1
= le_to_h_u32(sg_buffer
+0);
210 /* uint32_t t2 = le_to_h_u32(sg_buffer+4); */
213 if (t1
!= 0x53425355)
220 static int stlink_usb_xfer_rw(void *handle
, int ep
, uint8_t *buf
, int size
)
222 struct stlink_usb_handle_s
*h
;
224 h
= (struct stlink_usb_handle_s
*)handle
;
229 if (ep
== STLINK_RX_EP
) {
230 if (jtag_libusb_bulk_read(h
->fd
, STLINK_RX_EP
, (char *)buf
,
231 size
, 1000) != size
) {
235 if (jtag_libusb_bulk_write(h
->fd
, STLINK_TX_EP
, (char *)buf
,
236 size
, 1000) != size
) {
245 * http://en.wikipedia.org/wiki/SCSI_Request_Sense_Command
247 static int stlink_usb_xfer_v1_get_sense(void *handle
)
251 uint8_t cdb
[STLINK_CMD_SIZE
];
255 assert(handle
!= NULL
);
257 memset(cdb
, 0, sizeof(cdb
));
260 cdb
[4] = sizeof(sense
);
262 err
= stlink_usb_xfer_v1_send_cmd(handle
, cdb
, sizeof(cdb
), STLINK_RX_EP
, sizeof(sense
));
267 err
= stlink_usb_xfer_rw(handle
, STLINK_RX_EP
, sense
, sizeof(sense
));
272 err
= stlink_usb_xfer_v1_get_status(handle
, status
, sizeof(status
));
277 /* check for sense */
281 /* if (sense[0] != 0x70 && sense[0] != 0x71) */
287 static int stlink_usb_xfer_v1_check_status(void *handle
)
290 uint8_t sg_buffer
[13];
292 err
= stlink_usb_xfer_v1_get_status(handle
, sg_buffer
, sizeof(sg_buffer
));
297 /* check for sense */
298 if (sg_buffer
[12] == 1) {
299 LOG_DEBUG("get sense");
301 err
= stlink_usb_xfer_v1_get_sense(handle
);
308 static int stlink_usb_xfer_v1(void *handle
, const uint8_t *cmd
, int cmdsize
, int ep
,
309 uint8_t *buf
, int size
)
313 assert(handle
!= NULL
);
315 err
= stlink_usb_xfer_v1_send_cmd(handle
, cmd
, cmdsize
, ep
, size
);
320 err
= stlink_usb_xfer_rw(handle
, ep
, buf
, size
);
325 return stlink_usb_xfer_v1_check_status(handle
);
329 static int stlink_usb_xfer_v2(void *handle
, const uint8_t *cmd
, int cmdsize
, int ep
,
330 uint8_t *buf
, int size
)
332 struct stlink_usb_handle_s
*h
;
334 assert(handle
!= NULL
);
336 h
= (struct stlink_usb_handle_s
*)handle
;
338 if (jtag_libusb_bulk_write(h
->fd
, STLINK_TX_EP
, (char *)cmd
, cmdsize
,
343 return stlink_usb_xfer_rw(handle
, ep
, buf
, size
);
347 static int stlink_usb_xfer(void *handle
, const uint8_t *cmd
, int cmdsize
, int ep
,
348 uint8_t *buf
, int size
)
350 struct stlink_usb_handle_s
*h
;
352 assert(handle
!= NULL
);
353 assert(cmdsize
== STLINK_CMD_SIZE
);
355 h
= (struct stlink_usb_handle_s
*)handle
;
357 if (h
->version
.stlink
== 1) {
358 return stlink_usb_xfer_v1(handle
, cmd
, cmdsize
, ep
, buf
, size
);
360 return stlink_usb_xfer_v2(handle
, cmd
, cmdsize
, ep
, buf
, size
);
365 static int stlink_usb_recv(void *handle
, const uint8_t *cmd
, int cmdsize
, uint8_t *rxbuf
,
368 return stlink_usb_xfer(handle
, cmd
, cmdsize
, STLINK_RX_EP
, rxbuf
, rxsize
);
372 static void stlink_usb_init_buffer(void *handle
)
374 struct stlink_usb_handle_s
*h
;
376 assert(handle
!= NULL
);
378 h
= (struct stlink_usb_handle_s
*)handle
;
380 memset(h
->txbuf
, 0, STLINK_TX_SIZE
);
381 memset(h
->rxbuf
, 0, STLINK_RX_SIZE
);
384 static const char * const stlink_usb_error_msg
[] = {
389 static int stlink_usb_error_check(void *handle
)
392 const char *err_msg
= 0;
393 struct stlink_usb_handle_s
*h
;
395 assert(handle
!= NULL
);
397 h
= (struct stlink_usb_handle_s
*)handle
;
399 /* TODO: no error checking yet on api V1 */
400 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
401 h
->rxbuf
[0] = STLINK_DEBUG_ERR_OK
;
403 switch (h
->rxbuf
[0]) {
404 case STLINK_DEBUG_ERR_OK
:
407 case STLINK_DEBUG_ERR_FAULT
:
409 err_msg
= stlink_usb_error_msg
[0];
415 LOG_DEBUG("status error: %d ('%s')", h
->rxbuf
[0], err_msg
);
421 static int stlink_usb_version(void *handle
)
425 struct stlink_usb_handle_s
*h
;
427 assert(handle
!= NULL
);
429 h
= (struct stlink_usb_handle_s
*)handle
;
431 stlink_usb_init_buffer(handle
);
433 h
->txbuf
[0] = STLINK_GET_VERSION
;
435 res
= stlink_usb_recv(handle
, h
->txbuf
, STLINK_CMD_SIZE
, h
->rxbuf
, 6);
440 v
= (h
->rxbuf
[0] << 8) | h
->rxbuf
[1];
442 h
->version
.stlink
= (v
>> 12) & 0x0f;
443 h
->version
.jtag
= (v
>> 6) & 0x3f;
444 h
->version
.swim
= v
& 0x3f;
445 h
->vid
= buf_get_u32(h
->rxbuf
, 16, 16);
446 h
->pid
= buf_get_u32(h
->rxbuf
, 32, 16);
448 /* set the supported jtag api version
449 * V1 doesn't support API V2 at all
450 * V2 support API V2 since JTAG V13
452 if ((h
->version
.stlink
== 2) && (h
->version
.jtag
> 12))
453 h
->version
.jtag_api_max
= STLINK_JTAG_API_V2
;
455 h
->version
.jtag_api_max
= STLINK_JTAG_API_V1
;
457 LOG_DEBUG("STLINK v%d JTAG v%d API v%d SWIM v%d VID %04X PID %04X",
460 (h
->version
.jtag_api_max
== STLINK_JTAG_API_V1
) ? 1 : 2,
469 static int stlink_usb_current_mode(void *handle
, uint8_t *mode
)
472 struct stlink_usb_handle_s
*h
;
474 assert(handle
!= NULL
);
476 h
= (struct stlink_usb_handle_s
*)handle
;
478 stlink_usb_init_buffer(handle
);
480 h
->txbuf
[0] = STLINK_GET_CURRENT_MODE
;
482 res
= stlink_usb_recv(handle
, h
->txbuf
, STLINK_CMD_SIZE
, h
->rxbuf
, 2);
493 static int stlink_usb_mode_enter(void *handle
, enum stlink_mode type
)
497 struct stlink_usb_handle_s
*h
;
499 assert(handle
!= NULL
);
501 h
= (struct stlink_usb_handle_s
*)handle
;
503 stlink_usb_init_buffer(handle
);
506 case STLINK_MODE_DEBUG_JTAG
:
507 h
->txbuf
[0] = STLINK_DEBUG_COMMAND
;
508 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
509 h
->txbuf
[1] = STLINK_DEBUG_APIV1_ENTER
;
511 h
->txbuf
[1] = STLINK_DEBUG_APIV2_ENTER
;
512 h
->txbuf
[2] = STLINK_DEBUG_ENTER_JTAG
;
514 case STLINK_MODE_DEBUG_SWD
:
515 h
->txbuf
[0] = STLINK_DEBUG_COMMAND
;
516 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
517 h
->txbuf
[1] = STLINK_DEBUG_APIV1_ENTER
;
519 h
->txbuf
[1] = STLINK_DEBUG_APIV2_ENTER
;
520 h
->txbuf
[2] = STLINK_DEBUG_ENTER_SWD
;
522 case STLINK_MODE_DEBUG_SWIM
:
523 h
->txbuf
[0] = STLINK_SWIM_COMMAND
;
524 h
->txbuf
[1] = STLINK_SWIM_ENTER
;
526 case STLINK_MODE_DFU
:
527 case STLINK_MODE_MASS
:
532 /* on api V2 we are able the read the latest command
534 * TODO: we need the test on api V1 too
536 if (h
->jtag_api
== STLINK_JTAG_API_V2
)
539 res
= stlink_usb_recv(handle
, h
->txbuf
, STLINK_CMD_SIZE
, h
->rxbuf
, rx_size
);
544 res
= stlink_usb_error_check(h
);
550 static int stlink_usb_mode_leave(void *handle
, enum stlink_mode type
)
553 struct stlink_usb_handle_s
*h
;
555 assert(handle
!= NULL
);
557 h
= (struct stlink_usb_handle_s
*)handle
;
559 stlink_usb_init_buffer(handle
);
562 case STLINK_MODE_DEBUG_JTAG
:
563 case STLINK_MODE_DEBUG_SWD
:
564 h
->txbuf
[0] = STLINK_DEBUG_COMMAND
;
565 h
->txbuf
[1] = STLINK_DEBUG_EXIT
;
567 case STLINK_MODE_DEBUG_SWIM
:
568 h
->txbuf
[0] = STLINK_SWIM_COMMAND
;
569 h
->txbuf
[1] = STLINK_SWIM_EXIT
;
571 case STLINK_MODE_DFU
:
572 h
->txbuf
[0] = STLINK_DFU_COMMAND
;
573 h
->txbuf
[1] = STLINK_DFU_EXIT
;
575 case STLINK_MODE_MASS
:
580 res
= stlink_usb_recv(handle
, h
->txbuf
, STLINK_CMD_SIZE
, 0, 0);
589 static int stlink_usb_init_mode(void *handle
)
593 enum stlink_mode emode
;
594 struct stlink_usb_handle_s
*h
;
596 assert(handle
!= NULL
);
598 h
= (struct stlink_usb_handle_s
*)handle
;
600 res
= stlink_usb_current_mode(handle
, &mode
);
605 LOG_DEBUG("MODE: %02X", mode
);
607 /* try to exit current mode */
609 case STLINK_DEV_DFU_MODE
:
610 emode
= STLINK_MODE_DFU
;
612 case STLINK_DEV_DEBUG_MODE
:
613 emode
= STLINK_MODE_DEBUG_SWD
;
615 case STLINK_DEV_SWIM_MODE
:
616 emode
= STLINK_MODE_DEBUG_SWIM
;
618 case STLINK_DEV_BOOTLOADER_MODE
:
619 case STLINK_DEV_MASS_MODE
:
621 emode
= STLINK_MODE_UNKNOWN
;
625 if (emode
!= STLINK_MODE_UNKNOWN
) {
626 res
= stlink_usb_mode_leave(handle
, emode
);
632 res
= stlink_usb_current_mode(handle
, &mode
);
637 LOG_DEBUG("MODE: %02X", mode
);
639 /* set selected mode */
640 switch (h
->transport
) {
641 case STLINK_TRANSPORT_SWD
:
642 emode
= STLINK_MODE_DEBUG_SWD
;
644 case STLINK_TRANSPORT_JTAG
:
645 emode
= STLINK_MODE_DEBUG_JTAG
;
647 case STLINK_TRANSPORT_SWIM
:
648 emode
= STLINK_MODE_DEBUG_SWIM
;
651 emode
= STLINK_MODE_UNKNOWN
;
655 if (emode
== STLINK_MODE_UNKNOWN
) {
656 LOG_ERROR("selected mode (transport) not supported");
660 res
= stlink_usb_mode_enter(handle
, emode
);
665 res
= stlink_usb_current_mode(handle
, &mode
);
670 LOG_DEBUG("MODE: %02X", mode
);
676 static int stlink_usb_idcode(void *handle
, uint32_t *idcode
)
679 struct stlink_usb_handle_s
*h
;
681 assert(handle
!= NULL
);
683 h
= (struct stlink_usb_handle_s
*)handle
;
685 stlink_usb_init_buffer(handle
);
687 h
->txbuf
[0] = STLINK_DEBUG_COMMAND
;
688 h
->txbuf
[1] = STLINK_DEBUG_READCOREID
;
690 res
= stlink_usb_recv(handle
, h
->txbuf
, STLINK_CMD_SIZE
, h
->rxbuf
, 4);
695 *idcode
= le_to_h_u32(h
->rxbuf
);
697 LOG_DEBUG("IDCODE: %08X", *idcode
);
703 static enum target_state
stlink_usb_state(void *handle
)
706 struct stlink_usb_handle_s
*h
;
708 assert(handle
!= NULL
);
710 h
= (struct stlink_usb_handle_s
*)handle
;
712 if (h
->jtag_api
== STLINK_JTAG_API_V2
)
713 return TARGET_UNKNOWN
;
715 stlink_usb_init_buffer(handle
);
717 h
->txbuf
[0] = STLINK_DEBUG_COMMAND
;
718 h
->txbuf
[1] = STLINK_DEBUG_GETSTATUS
;
720 res
= stlink_usb_recv(handle
, h
->txbuf
, STLINK_CMD_SIZE
, h
->rxbuf
, 2);
723 return TARGET_UNKNOWN
;
725 if (h
->rxbuf
[0] == STLINK_CORE_RUNNING
)
726 return TARGET_RUNNING
;
727 if (h
->rxbuf
[0] == STLINK_CORE_HALTED
)
728 return TARGET_HALTED
;
730 return TARGET_UNKNOWN
;
734 static int stlink_usb_reset(void *handle
)
737 struct stlink_usb_handle_s
*h
;
739 assert(handle
!= NULL
);
741 h
= (struct stlink_usb_handle_s
*)handle
;
743 stlink_usb_init_buffer(handle
);
745 h
->txbuf
[0] = STLINK_DEBUG_COMMAND
;
747 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
748 h
->txbuf
[1] = STLINK_DEBUG_APIV1_RESETSYS
;
750 h
->txbuf
[1] = STLINK_DEBUG_APIV2_RESETSYS
;
752 res
= stlink_usb_recv(handle
, h
->txbuf
, STLINK_CMD_SIZE
, h
->rxbuf
, 2);
757 LOG_DEBUG("RESET: %08X", h
->rxbuf
[0]);
763 static int stlink_usb_run(void *handle
)
766 struct stlink_usb_handle_s
*h
;
768 assert(handle
!= NULL
);
770 h
= (struct stlink_usb_handle_s
*)handle
;
772 if (h
->jtag_api
== STLINK_JTAG_API_V2
)
775 stlink_usb_init_buffer(handle
);
777 h
->txbuf
[0] = STLINK_DEBUG_COMMAND
;
778 h
->txbuf
[1] = STLINK_DEBUG_RUNCORE
;
780 res
= stlink_usb_recv(handle
, h
->txbuf
, STLINK_CMD_SIZE
, h
->rxbuf
, 2);
789 static int stlink_usb_halt(void *handle
)
792 struct stlink_usb_handle_s
*h
;
794 assert(handle
!= NULL
);
796 h
= (struct stlink_usb_handle_s
*)handle
;
798 if (h
->jtag_api
== STLINK_JTAG_API_V2
)
801 stlink_usb_init_buffer(handle
);
803 h
->txbuf
[0] = STLINK_DEBUG_COMMAND
;
804 h
->txbuf
[1] = STLINK_DEBUG_FORCEDEBUG
;
806 res
= stlink_usb_recv(handle
, h
->txbuf
, STLINK_CMD_SIZE
, h
->rxbuf
, 2);
815 static int stlink_usb_step(void *handle
)
818 struct stlink_usb_handle_s
*h
;
820 assert(handle
!= NULL
);
822 h
= (struct stlink_usb_handle_s
*)handle
;
824 if (h
->jtag_api
== STLINK_JTAG_API_V2
)
827 stlink_usb_init_buffer(handle
);
829 h
->txbuf
[0] = STLINK_DEBUG_COMMAND
;
830 h
->txbuf
[1] = STLINK_DEBUG_STEPCORE
;
832 res
= stlink_usb_recv(handle
, h
->txbuf
, STLINK_CMD_SIZE
, h
->rxbuf
, 2);
841 static int stlink_usb_read_regs(void *handle
)
844 struct stlink_usb_handle_s
*h
;
846 assert(handle
!= NULL
);
848 h
= (struct stlink_usb_handle_s
*)handle
;
850 stlink_usb_init_buffer(handle
);
852 h
->txbuf
[0] = STLINK_DEBUG_COMMAND
;
853 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
854 h
->txbuf
[1] = STLINK_DEBUG_APIV1_READALLREGS
;
856 h
->txbuf
[1] = STLINK_DEBUG_APIV2_READALLREGS
;
858 res
= stlink_usb_recv(handle
, h
->txbuf
, STLINK_CMD_SIZE
, h
->rxbuf
, 84);
867 static int stlink_usb_read_reg(void *handle
, int num
, uint32_t *val
)
870 struct stlink_usb_handle_s
*h
;
872 assert(handle
!= NULL
);
874 h
= (struct stlink_usb_handle_s
*)handle
;
876 stlink_usb_init_buffer(handle
);
878 h
->txbuf
[0] = STLINK_DEBUG_COMMAND
;
879 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
880 h
->txbuf
[1] = STLINK_DEBUG_APIV1_READREG
;
882 h
->txbuf
[1] = STLINK_DEBUG_APIV2_READREG
;
885 res
= stlink_usb_recv(handle
, h
->txbuf
, STLINK_CMD_SIZE
, h
->rxbuf
, 4);
890 *val
= le_to_h_u32(h
->rxbuf
);
896 static int stlink_usb_write_reg(void *handle
, int num
, uint32_t val
)
899 struct stlink_usb_handle_s
*h
;
901 assert(handle
!= NULL
);
903 h
= (struct stlink_usb_handle_s
*)handle
;
905 stlink_usb_init_buffer(handle
);
907 h
->txbuf
[0] = STLINK_DEBUG_COMMAND
;
908 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
909 h
->txbuf
[1] = STLINK_DEBUG_APIV1_WRITEREG
;
911 h
->txbuf
[1] = STLINK_DEBUG_APIV2_WRITEREG
;
913 h_u32_to_le(h
->txbuf
+ 3, val
);
915 res
= stlink_usb_recv(handle
, h
->txbuf
, STLINK_CMD_SIZE
, h
->rxbuf
, 2);
924 static int stlink_usb_read_mem8(void *handle
, uint32_t addr
, uint16_t len
,
928 uint16_t read_len
= len
;
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
);
937 h
->txbuf
[0] = STLINK_DEBUG_COMMAND
;
938 h
->txbuf
[1] = STLINK_DEBUG_READMEM_8BIT
;
939 h_u32_to_le(h
->txbuf
+ 2, addr
);
940 h_u16_to_le(h
->txbuf
+ 2 + 4, len
);
942 /* we need to fix read length for single bytes */
946 res
= stlink_usb_recv(handle
, h
->txbuf
, STLINK_CMD_SIZE
, h
->rxbuf
, read_len
);
951 memcpy(buffer
, h
->rxbuf
, len
);
957 static int stlink_usb_write_mem8(void *handle
, uint32_t addr
, uint16_t len
,
958 const uint8_t *buffer
)
961 struct stlink_usb_handle_s
*h
;
963 assert(handle
!= NULL
);
965 h
= (struct stlink_usb_handle_s
*)handle
;
967 stlink_usb_init_buffer(handle
);
969 h
->txbuf
[0] = STLINK_DEBUG_COMMAND
;
970 h
->txbuf
[1] = STLINK_DEBUG_WRITEMEM_8BIT
;
971 h_u32_to_le(h
->txbuf
+ 2, addr
);
972 h_u16_to_le(h
->txbuf
+ 2 + 4, len
);
974 res
= stlink_usb_recv(handle
, h
->txbuf
, STLINK_CMD_SIZE
, 0, 0);
979 res
= stlink_usb_recv(handle
, (uint8_t *) buffer
, len
, 0, 0);
988 static int stlink_usb_read_mem32(void *handle
, uint32_t addr
, uint16_t len
,
992 struct stlink_usb_handle_s
*h
;
994 assert(handle
!= NULL
);
996 h
= (struct stlink_usb_handle_s
*)handle
;
998 stlink_usb_init_buffer(handle
);
1002 h
->txbuf
[0] = STLINK_DEBUG_COMMAND
;
1003 h
->txbuf
[1] = STLINK_DEBUG_READMEM_32BIT
;
1004 h_u32_to_le(h
->txbuf
+ 2, addr
);
1005 h_u16_to_le(h
->txbuf
+ 2 + 4, len
);
1007 res
= stlink_usb_recv(handle
, h
->txbuf
, STLINK_CMD_SIZE
, h
->rxbuf
, len
);
1009 if (res
!= ERROR_OK
)
1012 memcpy(buffer
, h
->rxbuf
, len
);
1018 static int stlink_usb_write_mem32(void *handle
, uint32_t addr
, uint16_t len
,
1019 const uint32_t *buffer
)
1022 struct stlink_usb_handle_s
*h
;
1024 assert(handle
!= NULL
);
1026 h
= (struct stlink_usb_handle_s
*)handle
;
1028 stlink_usb_init_buffer(handle
);
1032 h
->txbuf
[0] = STLINK_DEBUG_COMMAND
;
1033 h
->txbuf
[1] = STLINK_DEBUG_WRITEMEM_32BIT
;
1034 h_u32_to_le(h
->txbuf
+ 2, addr
);
1035 h_u16_to_le(h
->txbuf
+ 2 + 4, len
);
1037 res
= stlink_usb_recv(handle
, h
->txbuf
, STLINK_CMD_SIZE
, 0, 0);
1039 if (res
!= ERROR_OK
)
1042 res
= stlink_usb_recv(handle
, (uint8_t *) buffer
, len
, 0, 0);
1044 if (res
!= ERROR_OK
)
1051 static int stlink_usb_open(struct stlink_interface_param_s
*param
, void **fd
)
1054 struct stlink_usb_handle_s
*h
;
1056 LOG_DEBUG("stlink_usb_open");
1058 h
= malloc(sizeof(struct stlink_usb_handle_s
));
1061 LOG_DEBUG("malloc failed");
1065 h
->transport
= param
->transport
;
1067 const uint16_t vids
[] = { param
->vid
, 0 };
1068 const uint16_t pids
[] = { param
->pid
, 0 };
1070 LOG_DEBUG("transport: %d vid: %04x pid: %04x", param
->transport
,
1071 param
->vid
, param
->pid
);
1073 if (jtag_libusb_open(vids
, pids
, &h
->fd
) != ERROR_OK
) {
1074 LOG_ERROR("open failed");
1078 jtag_libusb_set_configuration(h
->fd
, 0);
1080 if (jtag_libusb_claim_interface(h
->fd
, 0) != ERROR_OK
) {
1081 LOG_DEBUG("claim interface failed");
1085 /* wrap version for first read */
1086 switch (param
->pid
) {
1088 h
->version
.stlink
= 1;
1091 h
->version
.stlink
= 2;
1095 /* get the device version */
1096 err
= stlink_usb_version(h
);
1098 if (err
!= ERROR_OK
) {
1099 LOG_ERROR("read version failed");
1100 jtag_libusb_close(h
->fd
);
1105 /* compare usb vid/pid */
1106 if ((param
->vid
!= h
->vid
) || (param
->pid
!= h
->pid
))
1107 LOG_INFO("vid/pid are not identical: %04X/%04X %04X/%04X",
1108 param
->vid
, param
->pid
,
1111 /* check if mode is supported */
1114 switch (h
->transport
) {
1115 case STLINK_TRANSPORT_SWD
:
1116 case STLINK_TRANSPORT_JTAG
:
1117 if (h
->version
.jtag
== 0)
1120 case STLINK_TRANSPORT_SWIM
:
1121 if (h
->version
.swim
== 0)
1129 if (err
!= ERROR_OK
) {
1130 LOG_ERROR("mode (transport) not supported by device");
1131 jtag_libusb_close(h
->fd
);
1136 /* set the used jtag api */
1137 h
->jtag_api
= STLINK_JTAG_API_V1
;
1139 /* initialize the debug hardware */
1140 err
= stlink_usb_init_mode(h
);
1142 if (err
!= ERROR_OK
) {
1143 LOG_ERROR("init mode failed");
1144 jtag_libusb_close(h
->fd
);
1155 static int stlink_usb_close(void *fd
)
1161 struct stlink_layout_api_s stlink_usb_layout_api
= {
1163 .open
= stlink_usb_open
,
1165 .close
= stlink_usb_close
,
1167 .idcode
= stlink_usb_idcode
,
1169 .state
= stlink_usb_state
,
1171 .reset
= stlink_usb_reset
,
1173 .run
= stlink_usb_run
,
1175 .halt
= stlink_usb_halt
,
1177 .step
= stlink_usb_step
,
1179 .read_regs
= stlink_usb_read_regs
,
1181 .read_reg
= stlink_usb_read_reg
,
1183 .write_reg
= stlink_usb_write_reg
,
1185 .read_mem8
= stlink_usb_read_mem8
,
1187 .write_mem8
= stlink_usb_write_mem8
,
1189 .read_mem32
= stlink_usb_read_mem32
,
1191 .write_mem32
= stlink_usb_write_mem32
,
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)