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 int stlink_usb_send(void *handle
, const uint8_t *cmd
, int cmdsize
, uint8_t *txbuf
,
375 return stlink_usb_xfer(handle
, cmd
, cmdsize
, STLINK_TX_EP
, txbuf
, txsize
);
379 static void stlink_usb_init_buffer(void *handle
)
381 struct stlink_usb_handle_s
*h
;
383 assert(handle
!= NULL
);
385 h
= (struct stlink_usb_handle_s
*)handle
;
387 memset(h
->txbuf
, 0, STLINK_TX_SIZE
);
388 memset(h
->rxbuf
, 0, STLINK_RX_SIZE
);
391 static const char * const stlink_usb_error_msg
[] = {
396 static int stlink_usb_error_check(void *handle
)
399 const char *err_msg
= 0;
400 struct stlink_usb_handle_s
*h
;
402 assert(handle
!= NULL
);
404 h
= (struct stlink_usb_handle_s
*)handle
;
406 /* TODO: no error checking yet on api V1 */
407 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
408 h
->rxbuf
[0] = STLINK_DEBUG_ERR_OK
;
410 switch (h
->rxbuf
[0]) {
411 case STLINK_DEBUG_ERR_OK
:
414 case STLINK_DEBUG_ERR_FAULT
:
416 err_msg
= stlink_usb_error_msg
[0];
422 LOG_DEBUG("status error: %d ('%s')", h
->rxbuf
[0], err_msg
);
428 static int stlink_usb_version(void *handle
)
432 struct stlink_usb_handle_s
*h
;
434 assert(handle
!= NULL
);
436 h
= (struct stlink_usb_handle_s
*)handle
;
438 stlink_usb_init_buffer(handle
);
440 h
->txbuf
[0] = STLINK_GET_VERSION
;
442 res
= stlink_usb_recv(handle
, h
->txbuf
, STLINK_CMD_SIZE
, h
->rxbuf
, 6);
447 v
= (h
->rxbuf
[0] << 8) | h
->rxbuf
[1];
449 h
->version
.stlink
= (v
>> 12) & 0x0f;
450 h
->version
.jtag
= (v
>> 6) & 0x3f;
451 h
->version
.swim
= v
& 0x3f;
452 h
->vid
= buf_get_u32(h
->rxbuf
, 16, 16);
453 h
->pid
= buf_get_u32(h
->rxbuf
, 32, 16);
455 /* set the supported jtag api version
456 * V1 doesn't support API V2 at all
457 * V2 support API V2 since JTAG V13
459 if ((h
->version
.stlink
== 2) && (h
->version
.jtag
> 12))
460 h
->version
.jtag_api_max
= STLINK_JTAG_API_V2
;
462 h
->version
.jtag_api_max
= STLINK_JTAG_API_V1
;
464 LOG_DEBUG("STLINK v%d JTAG v%d API v%d SWIM v%d VID %04X PID %04X",
467 (h
->version
.jtag_api_max
== STLINK_JTAG_API_V1
) ? 1 : 2,
476 static int stlink_usb_current_mode(void *handle
, uint8_t *mode
)
479 struct stlink_usb_handle_s
*h
;
481 assert(handle
!= NULL
);
483 h
= (struct stlink_usb_handle_s
*)handle
;
485 stlink_usb_init_buffer(handle
);
487 h
->txbuf
[0] = STLINK_GET_CURRENT_MODE
;
489 res
= stlink_usb_recv(handle
, h
->txbuf
, STLINK_CMD_SIZE
, h
->rxbuf
, 2);
500 static int stlink_usb_mode_enter(void *handle
, enum stlink_mode type
)
504 struct stlink_usb_handle_s
*h
;
506 assert(handle
!= NULL
);
508 h
= (struct stlink_usb_handle_s
*)handle
;
510 stlink_usb_init_buffer(handle
);
513 case STLINK_MODE_DEBUG_JTAG
:
514 h
->txbuf
[0] = STLINK_DEBUG_COMMAND
;
515 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
516 h
->txbuf
[1] = STLINK_DEBUG_APIV1_ENTER
;
518 h
->txbuf
[1] = STLINK_DEBUG_APIV2_ENTER
;
519 h
->txbuf
[2] = STLINK_DEBUG_ENTER_JTAG
;
521 case STLINK_MODE_DEBUG_SWD
:
522 h
->txbuf
[0] = STLINK_DEBUG_COMMAND
;
523 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
524 h
->txbuf
[1] = STLINK_DEBUG_APIV1_ENTER
;
526 h
->txbuf
[1] = STLINK_DEBUG_APIV2_ENTER
;
527 h
->txbuf
[2] = STLINK_DEBUG_ENTER_SWD
;
529 case STLINK_MODE_DEBUG_SWIM
:
530 h
->txbuf
[0] = STLINK_SWIM_COMMAND
;
531 h
->txbuf
[1] = STLINK_SWIM_ENTER
;
533 case STLINK_MODE_DFU
:
534 case STLINK_MODE_MASS
:
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 res
= stlink_usb_recv(handle
, h
->txbuf
, STLINK_CMD_SIZE
, h
->rxbuf
, rx_size
);
551 res
= stlink_usb_error_check(h
);
557 static int stlink_usb_mode_leave(void *handle
, enum stlink_mode type
)
560 struct stlink_usb_handle_s
*h
;
562 assert(handle
!= NULL
);
564 h
= (struct stlink_usb_handle_s
*)handle
;
566 stlink_usb_init_buffer(handle
);
569 case STLINK_MODE_DEBUG_JTAG
:
570 case STLINK_MODE_DEBUG_SWD
:
571 h
->txbuf
[0] = STLINK_DEBUG_COMMAND
;
572 h
->txbuf
[1] = STLINK_DEBUG_EXIT
;
574 case STLINK_MODE_DEBUG_SWIM
:
575 h
->txbuf
[0] = STLINK_SWIM_COMMAND
;
576 h
->txbuf
[1] = STLINK_SWIM_EXIT
;
578 case STLINK_MODE_DFU
:
579 h
->txbuf
[0] = STLINK_DFU_COMMAND
;
580 h
->txbuf
[1] = STLINK_DFU_EXIT
;
582 case STLINK_MODE_MASS
:
587 res
= stlink_usb_recv(handle
, h
->txbuf
, STLINK_CMD_SIZE
, 0, 0);
596 static int stlink_usb_init_mode(void *handle
)
600 enum stlink_mode emode
;
601 struct stlink_usb_handle_s
*h
;
603 assert(handle
!= NULL
);
605 h
= (struct stlink_usb_handle_s
*)handle
;
607 res
= stlink_usb_current_mode(handle
, &mode
);
612 LOG_DEBUG("MODE: %02X", mode
);
614 /* try to exit current mode */
616 case STLINK_DEV_DFU_MODE
:
617 emode
= STLINK_MODE_DFU
;
619 case STLINK_DEV_DEBUG_MODE
:
620 emode
= STLINK_MODE_DEBUG_SWD
;
622 case STLINK_DEV_SWIM_MODE
:
623 emode
= STLINK_MODE_DEBUG_SWIM
;
625 case STLINK_DEV_BOOTLOADER_MODE
:
626 case STLINK_DEV_MASS_MODE
:
628 emode
= STLINK_MODE_UNKNOWN
;
632 if (emode
!= STLINK_MODE_UNKNOWN
) {
633 res
= stlink_usb_mode_leave(handle
, emode
);
639 res
= stlink_usb_current_mode(handle
, &mode
);
644 LOG_DEBUG("MODE: %02X", mode
);
646 /* set selected mode */
647 switch (h
->transport
) {
648 case STLINK_TRANSPORT_SWD
:
649 emode
= STLINK_MODE_DEBUG_SWD
;
651 case STLINK_TRANSPORT_JTAG
:
652 emode
= STLINK_MODE_DEBUG_JTAG
;
654 case STLINK_TRANSPORT_SWIM
:
655 emode
= STLINK_MODE_DEBUG_SWIM
;
658 emode
= STLINK_MODE_UNKNOWN
;
662 if (emode
== STLINK_MODE_UNKNOWN
) {
663 LOG_ERROR("selected mode (transport) not supported");
667 res
= stlink_usb_mode_enter(handle
, emode
);
672 res
= stlink_usb_current_mode(handle
, &mode
);
677 LOG_DEBUG("MODE: %02X", mode
);
683 static int stlink_usb_idcode(void *handle
, uint32_t *idcode
)
686 struct stlink_usb_handle_s
*h
;
688 assert(handle
!= NULL
);
690 h
= (struct stlink_usb_handle_s
*)handle
;
692 stlink_usb_init_buffer(handle
);
694 h
->txbuf
[0] = STLINK_DEBUG_COMMAND
;
695 h
->txbuf
[1] = STLINK_DEBUG_READCOREID
;
697 res
= stlink_usb_recv(handle
, h
->txbuf
, STLINK_CMD_SIZE
, h
->rxbuf
, 4);
702 *idcode
= le_to_h_u32(h
->rxbuf
);
704 LOG_DEBUG("IDCODE: %08X", *idcode
);
710 static enum target_state
stlink_usb_state(void *handle
)
713 struct stlink_usb_handle_s
*h
;
715 assert(handle
!= NULL
);
717 h
= (struct stlink_usb_handle_s
*)handle
;
719 if (h
->jtag_api
== STLINK_JTAG_API_V2
)
720 return TARGET_UNKNOWN
;
722 stlink_usb_init_buffer(handle
);
724 h
->txbuf
[0] = STLINK_DEBUG_COMMAND
;
725 h
->txbuf
[1] = STLINK_DEBUG_GETSTATUS
;
727 res
= stlink_usb_recv(handle
, h
->txbuf
, STLINK_CMD_SIZE
, h
->rxbuf
, 2);
730 return TARGET_UNKNOWN
;
732 if (h
->rxbuf
[0] == STLINK_CORE_RUNNING
)
733 return TARGET_RUNNING
;
734 if (h
->rxbuf
[0] == STLINK_CORE_HALTED
)
735 return TARGET_HALTED
;
737 return TARGET_UNKNOWN
;
741 static int stlink_usb_reset(void *handle
)
744 struct stlink_usb_handle_s
*h
;
746 assert(handle
!= NULL
);
748 h
= (struct stlink_usb_handle_s
*)handle
;
750 stlink_usb_init_buffer(handle
);
752 h
->txbuf
[0] = STLINK_DEBUG_COMMAND
;
754 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
755 h
->txbuf
[1] = STLINK_DEBUG_APIV1_RESETSYS
;
757 h
->txbuf
[1] = STLINK_DEBUG_APIV2_RESETSYS
;
759 res
= stlink_usb_recv(handle
, h
->txbuf
, STLINK_CMD_SIZE
, h
->rxbuf
, 2);
764 LOG_DEBUG("RESET: %08X", h
->rxbuf
[0]);
770 static int stlink_usb_run(void *handle
)
773 struct stlink_usb_handle_s
*h
;
775 assert(handle
!= NULL
);
777 h
= (struct stlink_usb_handle_s
*)handle
;
779 if (h
->jtag_api
== STLINK_JTAG_API_V2
)
782 stlink_usb_init_buffer(handle
);
784 h
->txbuf
[0] = STLINK_DEBUG_COMMAND
;
785 h
->txbuf
[1] = STLINK_DEBUG_RUNCORE
;
787 res
= stlink_usb_recv(handle
, h
->txbuf
, STLINK_CMD_SIZE
, h
->rxbuf
, 2);
796 static int stlink_usb_halt(void *handle
)
799 struct stlink_usb_handle_s
*h
;
801 assert(handle
!= NULL
);
803 h
= (struct stlink_usb_handle_s
*)handle
;
805 if (h
->jtag_api
== STLINK_JTAG_API_V2
)
808 stlink_usb_init_buffer(handle
);
810 h
->txbuf
[0] = STLINK_DEBUG_COMMAND
;
811 h
->txbuf
[1] = STLINK_DEBUG_FORCEDEBUG
;
813 res
= stlink_usb_recv(handle
, h
->txbuf
, STLINK_CMD_SIZE
, h
->rxbuf
, 2);
822 static int stlink_usb_step(void *handle
)
825 struct stlink_usb_handle_s
*h
;
827 assert(handle
!= NULL
);
829 h
= (struct stlink_usb_handle_s
*)handle
;
831 if (h
->jtag_api
== STLINK_JTAG_API_V2
)
834 stlink_usb_init_buffer(handle
);
836 h
->txbuf
[0] = STLINK_DEBUG_COMMAND
;
837 h
->txbuf
[1] = STLINK_DEBUG_STEPCORE
;
839 res
= stlink_usb_recv(handle
, h
->txbuf
, STLINK_CMD_SIZE
, h
->rxbuf
, 2);
848 static int stlink_usb_read_regs(void *handle
)
851 struct stlink_usb_handle_s
*h
;
853 assert(handle
!= NULL
);
855 h
= (struct stlink_usb_handle_s
*)handle
;
857 stlink_usb_init_buffer(handle
);
859 h
->txbuf
[0] = STLINK_DEBUG_COMMAND
;
860 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
861 h
->txbuf
[1] = STLINK_DEBUG_APIV1_READALLREGS
;
863 h
->txbuf
[1] = STLINK_DEBUG_APIV2_READALLREGS
;
865 res
= stlink_usb_recv(handle
, h
->txbuf
, STLINK_CMD_SIZE
, h
->rxbuf
, 84);
874 static int stlink_usb_read_reg(void *handle
, int num
, uint32_t *val
)
877 struct stlink_usb_handle_s
*h
;
879 assert(handle
!= NULL
);
881 h
= (struct stlink_usb_handle_s
*)handle
;
883 stlink_usb_init_buffer(handle
);
885 h
->txbuf
[0] = STLINK_DEBUG_COMMAND
;
886 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
887 h
->txbuf
[1] = STLINK_DEBUG_APIV1_READREG
;
889 h
->txbuf
[1] = STLINK_DEBUG_APIV2_READREG
;
892 res
= stlink_usb_recv(handle
, h
->txbuf
, STLINK_CMD_SIZE
, h
->rxbuf
, 4);
897 *val
= le_to_h_u32(h
->rxbuf
);
903 static int stlink_usb_write_reg(void *handle
, int num
, uint32_t val
)
906 struct stlink_usb_handle_s
*h
;
908 assert(handle
!= NULL
);
910 h
= (struct stlink_usb_handle_s
*)handle
;
912 stlink_usb_init_buffer(handle
);
914 h
->txbuf
[0] = STLINK_DEBUG_COMMAND
;
915 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
916 h
->txbuf
[1] = STLINK_DEBUG_APIV1_WRITEREG
;
918 h
->txbuf
[1] = STLINK_DEBUG_APIV2_WRITEREG
;
920 h_u32_to_le(h
->txbuf
+ 3, val
);
922 res
= stlink_usb_recv(handle
, h
->txbuf
, STLINK_CMD_SIZE
, h
->rxbuf
, 2);
931 static int stlink_usb_read_mem8(void *handle
, uint32_t addr
, uint16_t len
,
935 uint16_t read_len
= len
;
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
);
944 h
->txbuf
[0] = STLINK_DEBUG_COMMAND
;
945 h
->txbuf
[1] = STLINK_DEBUG_READMEM_8BIT
;
946 h_u32_to_le(h
->txbuf
+ 2, addr
);
947 h_u16_to_le(h
->txbuf
+ 2 + 4, len
);
949 /* we need to fix read length for single bytes */
953 res
= stlink_usb_recv(handle
, h
->txbuf
, STLINK_CMD_SIZE
, h
->rxbuf
, read_len
);
958 memcpy(buffer
, h
->rxbuf
, len
);
964 static int stlink_usb_write_mem8(void *handle
, uint32_t addr
, uint16_t len
,
965 const uint8_t *buffer
)
968 struct stlink_usb_handle_s
*h
;
970 assert(handle
!= NULL
);
972 h
= (struct stlink_usb_handle_s
*)handle
;
974 stlink_usb_init_buffer(handle
);
976 h
->txbuf
[0] = STLINK_DEBUG_COMMAND
;
977 h
->txbuf
[1] = STLINK_DEBUG_WRITEMEM_8BIT
;
978 h_u32_to_le(h
->txbuf
+ 2, addr
);
979 h_u16_to_le(h
->txbuf
+ 2 + 4, len
);
981 res
= stlink_usb_send(handle
, h
->txbuf
, STLINK_CMD_SIZE
, (uint8_t *) buffer
, len
);
990 static int stlink_usb_read_mem32(void *handle
, uint32_t addr
, uint16_t len
,
994 struct stlink_usb_handle_s
*h
;
996 assert(handle
!= NULL
);
998 h
= (struct stlink_usb_handle_s
*)handle
;
1000 stlink_usb_init_buffer(handle
);
1004 h
->txbuf
[0] = STLINK_DEBUG_COMMAND
;
1005 h
->txbuf
[1] = STLINK_DEBUG_READMEM_32BIT
;
1006 h_u32_to_le(h
->txbuf
+ 2, addr
);
1007 h_u16_to_le(h
->txbuf
+ 2 + 4, len
);
1009 res
= stlink_usb_recv(handle
, h
->txbuf
, STLINK_CMD_SIZE
, h
->rxbuf
, len
);
1011 if (res
!= ERROR_OK
)
1014 memcpy(buffer
, h
->rxbuf
, len
);
1020 static int stlink_usb_write_mem32(void *handle
, uint32_t addr
, uint16_t len
,
1021 const uint32_t *buffer
)
1024 struct stlink_usb_handle_s
*h
;
1026 assert(handle
!= NULL
);
1028 h
= (struct stlink_usb_handle_s
*)handle
;
1030 stlink_usb_init_buffer(handle
);
1034 h
->txbuf
[0] = STLINK_DEBUG_COMMAND
;
1035 h
->txbuf
[1] = STLINK_DEBUG_WRITEMEM_32BIT
;
1036 h_u32_to_le(h
->txbuf
+ 2, addr
);
1037 h_u16_to_le(h
->txbuf
+ 2 + 4, len
);
1039 res
= stlink_usb_send(handle
, h
->txbuf
, STLINK_CMD_SIZE
, (uint8_t *) buffer
, len
);
1041 if (res
!= ERROR_OK
)
1048 static int stlink_usb_open(struct stlink_interface_param_s
*param
, void **fd
)
1051 struct stlink_usb_handle_s
*h
;
1053 LOG_DEBUG("stlink_usb_open");
1055 h
= malloc(sizeof(struct stlink_usb_handle_s
));
1058 LOG_DEBUG("malloc failed");
1062 h
->transport
= param
->transport
;
1064 const uint16_t vids
[] = { param
->vid
, 0 };
1065 const uint16_t pids
[] = { param
->pid
, 0 };
1067 LOG_DEBUG("transport: %d vid: %04x pid: %04x", param
->transport
,
1068 param
->vid
, param
->pid
);
1070 if (jtag_libusb_open(vids
, pids
, &h
->fd
) != ERROR_OK
) {
1071 LOG_ERROR("open failed");
1075 jtag_libusb_set_configuration(h
->fd
, 0);
1077 if (jtag_libusb_claim_interface(h
->fd
, 0) != ERROR_OK
) {
1078 LOG_DEBUG("claim interface failed");
1082 /* wrap version for first read */
1083 switch (param
->pid
) {
1085 h
->version
.stlink
= 1;
1088 h
->version
.stlink
= 2;
1092 /* get the device version */
1093 err
= stlink_usb_version(h
);
1095 if (err
!= ERROR_OK
) {
1096 LOG_ERROR("read version failed");
1097 jtag_libusb_close(h
->fd
);
1102 /* compare usb vid/pid */
1103 if ((param
->vid
!= h
->vid
) || (param
->pid
!= h
->pid
))
1104 LOG_INFO("vid/pid are not identical: %04X/%04X %04X/%04X",
1105 param
->vid
, param
->pid
,
1108 /* check if mode is supported */
1111 switch (h
->transport
) {
1112 case STLINK_TRANSPORT_SWD
:
1113 case STLINK_TRANSPORT_JTAG
:
1114 if (h
->version
.jtag
== 0)
1117 case STLINK_TRANSPORT_SWIM
:
1118 if (h
->version
.swim
== 0)
1126 if (err
!= ERROR_OK
) {
1127 LOG_ERROR("mode (transport) not supported by device");
1128 jtag_libusb_close(h
->fd
);
1133 /* set the used jtag api */
1134 h
->jtag_api
= STLINK_JTAG_API_V1
;
1136 /* initialize the debug hardware */
1137 err
= stlink_usb_init_mode(h
);
1139 if (err
!= ERROR_OK
) {
1140 LOG_ERROR("init mode failed");
1141 jtag_libusb_close(h
->fd
);
1152 static int stlink_usb_close(void *fd
)
1158 struct stlink_layout_api_s stlink_usb_layout_api
= {
1160 .open
= stlink_usb_open
,
1162 .close
= stlink_usb_close
,
1164 .idcode
= stlink_usb_idcode
,
1166 .state
= stlink_usb_state
,
1168 .reset
= stlink_usb_reset
,
1170 .run
= stlink_usb_run
,
1172 .halt
= stlink_usb_halt
,
1174 .step
= stlink_usb_step
,
1176 .read_regs
= stlink_usb_read_regs
,
1178 .read_reg
= stlink_usb_read_reg
,
1180 .write_reg
= stlink_usb_write_reg
,
1182 .read_mem8
= stlink_usb_read_mem8
,
1184 .write_mem8
= stlink_usb_write_mem8
,
1186 .read_mem32
= stlink_usb_read_mem32
,
1188 .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)