1 /***************************************************************************
2 * Copyright (C) 2011 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_interface.h>
32 #include <target/target.h>
34 #include "libusb_common.h"
36 #define ENDPOINT_IN 0x80
37 #define ENDPOINT_OUT 0x00
39 #define STLINK_RX_EP (1|ENDPOINT_IN)
40 #define STLINK_TX_EP (2|ENDPOINT_OUT)
41 #define STLINK_CMD_SIZE (16)
42 #define STLINK_TX_SIZE (4*128)
43 #define STLINK_RX_SIZE (4*128)
46 struct stlink_usb_handle_s
{
48 struct jtag_libusb_device_handle
*fd
;
50 struct libusb_transfer
*trans
;
52 uint8_t txbuf
[STLINK_TX_SIZE
];
54 uint8_t rxbuf
[STLINK_RX_SIZE
];
57 #define STLINK_OK 0x80
58 #define STLINK_FALSE 0x81
59 #define STLINK_CORE_RUNNING 0x80
60 #define STLINK_CORE_HALTED 0x81
61 #define STLINK_CORE_STAT_UNKNOWN -1
63 #define STLINK_GET_VERSION 0xf1
64 #define STLINK_GET_CURRENT_MODE 0xf5
66 #define STLINK_DEBUG_COMMAND 0xF2
67 #define STLINK_DFU_COMMAND 0xF3
68 #define STLINK_DFU_EXIT 0x07
70 #define STLINK_DEV_DFU_MODE 0x00
71 #define STLINK_DEV_MASS_MODE 0x01
72 #define STLINK_DEV_DEBUG_MODE 0x02
73 #define STLINK_DEV_UNKNOWN_MODE -1
75 #define STLINK_DEBUG_ENTER 0x20
76 #define STLINK_DEBUG_EXIT 0x21
77 #define STLINK_DEBUG_READCOREID 0x22
79 #define STLINK_DEBUG_GETSTATUS 0x01
80 #define STLINK_DEBUG_FORCEDEBUG 0x02
81 #define STLINK_DEBUG_RESETSYS 0x03
82 #define STLINK_DEBUG_READALLREGS 0x04
83 #define STLINK_DEBUG_READREG 0x05
84 #define STLINK_DEBUG_WRITEREG 0x06
85 #define STLINK_DEBUG_READMEM_32BIT 0x07
86 #define STLINK_DEBUG_WRITEMEM_32BIT 0x08
87 #define STLINK_DEBUG_RUNCORE 0x09
88 #define STLINK_DEBUG_STEPCORE 0x0a
89 #define STLINK_DEBUG_SETFP 0x0b
90 #define STLINK_DEBUG_READMEM_8BIT 0x0c
91 #define STLINK_DEBUG_WRITEMEM_8BIT 0x0d
92 #define STLINK_DEBUG_CLEARFP 0x0e
93 #define STLINK_DEBUG_WRITEDEBUGREG 0x0f
94 #define STLINK_DEBUG_ENTER_SWD 0xa3
95 #define STLINK_DEBUG_ENTER_JTAG 0x00
97 #define STLINK_SWD_ENTER 0x30
98 #define STLINK_SWD_READCOREID 0x32
101 int stlink_usb_recv(void *handle
, const uint8_t *txbuf
, int txsize
, uint8_t *rxbuf
,
104 struct stlink_usb_handle_s
*h
;
106 assert(handle
!= NULL
);
108 h
= (struct stlink_usb_handle_s
*)handle
;
110 if (jtag_libusb_bulk_write(h
->fd
, STLINK_TX_EP
, (char *)txbuf
, txsize
,
114 if (rxsize
&& rxbuf
) {
115 if (jtag_libusb_bulk_read(h
->fd
, STLINK_RX_EP
, (char *)rxbuf
,
116 rxsize
, 1000) != rxsize
) {
124 void stlink_usb_init_buffer(void *handle
)
126 struct stlink_usb_handle_s
*h
;
128 assert(handle
!= NULL
);
130 h
= (struct stlink_usb_handle_s
*)handle
;
132 memset(h
->txbuf
, 0, STLINK_CMD_SIZE
);
136 int stlink_usb_version(void *handle
)
140 struct stlink_usb_handle_s
*h
;
142 assert(handle
!= NULL
);
144 h
= (struct stlink_usb_handle_s
*)handle
;
146 stlink_usb_init_buffer(handle
);
148 h
->txbuf
[0] = STLINK_GET_VERSION
;
150 res
= stlink_usb_recv(handle
, h
->txbuf
, STLINK_CMD_SIZE
, h
->rxbuf
, 6);
155 v
= (h
->rxbuf
[0] << 8) | h
->rxbuf
[1];
157 LOG_DEBUG("STLINK v%d", (v
>> 12) & 0x0f);
158 LOG_DEBUG("JTAG v%d", (v
>> 6) & 0x3f);
159 LOG_DEBUG("SWIM v%d", v
& 0x3f);
160 LOG_DEBUG("VID %04X", buf_get_u32(h
->rxbuf
, 16, 16));
161 LOG_DEBUG("PID %04X", buf_get_u32(h
->rxbuf
, 32, 16));
167 int stlink_usb_current_mode(void *handle
, uint8_t *mode
)
170 struct stlink_usb_handle_s
*h
;
172 assert(handle
!= NULL
);
174 h
= (struct stlink_usb_handle_s
*)handle
;
176 stlink_usb_init_buffer(handle
);
178 h
->txbuf
[0] = STLINK_GET_CURRENT_MODE
;
180 res
= stlink_usb_recv(handle
, h
->txbuf
, STLINK_CMD_SIZE
, h
->rxbuf
, 2);
191 int stlink_usb_dfu_mode_leave(void *handle
)
194 struct stlink_usb_handle_s
*h
;
196 assert(handle
!= NULL
);
198 h
= (struct stlink_usb_handle_s
*)handle
;
200 stlink_usb_init_buffer(handle
);
202 h
->txbuf
[0] = STLINK_DFU_COMMAND
;
203 h
->txbuf
[1] = STLINK_DFU_EXIT
;
205 res
= stlink_usb_recv(handle
, h
->txbuf
, STLINK_CMD_SIZE
, 0, 0);
214 int stlink_usb_swd_mode_enter(void *handle
)
217 struct stlink_usb_handle_s
*h
;
219 assert(handle
!= NULL
);
221 h
= (struct stlink_usb_handle_s
*)handle
;
223 stlink_usb_init_buffer(handle
);
225 h
->txbuf
[0] = STLINK_DEBUG_COMMAND
;
226 h
->txbuf
[1] = STLINK_DEBUG_ENTER
;
227 h
->txbuf
[2] = STLINK_DEBUG_ENTER_SWD
;
229 res
= stlink_usb_recv(handle
, h
->txbuf
, STLINK_CMD_SIZE
, 0, 0);
237 int stlink_usb_debug_mode_leave(void *handle
)
240 struct stlink_usb_handle_s
*h
;
242 assert(handle
!= NULL
);
244 h
= (struct stlink_usb_handle_s
*)handle
;
246 stlink_usb_init_buffer(handle
);
248 h
->txbuf
[0] = STLINK_DEBUG_COMMAND
;
249 h
->txbuf
[1] = STLINK_DEBUG_EXIT
;
251 res
= stlink_usb_recv(handle
, h
->txbuf
, STLINK_CMD_SIZE
, 0, 0);
259 int stlink_usb_init_mode(void *handle
)
264 assert(handle
!= NULL
);
266 res
= stlink_usb_current_mode(handle
, &mode
);
271 LOG_DEBUG("MODE: %02X", mode
);
273 if (mode
== STLINK_DEV_DFU_MODE
) {
274 res
= stlink_usb_dfu_mode_leave(handle
);
280 res
= stlink_usb_current_mode(handle
, &mode
);
285 LOG_DEBUG("MODE: %02X", mode
);
287 if (mode
!= STLINK_DEV_DEBUG_MODE
) {
288 res
= stlink_usb_swd_mode_enter(handle
);
294 res
= stlink_usb_current_mode(handle
, &mode
);
299 LOG_DEBUG("MODE: %02X", mode
);
305 int stlink_usb_idcode(void *handle
, uint32_t *idcode
)
308 struct stlink_usb_handle_s
*h
;
310 assert(handle
!= NULL
);
312 h
= (struct stlink_usb_handle_s
*)handle
;
314 stlink_usb_init_buffer(handle
);
316 h
->txbuf
[0] = STLINK_DEBUG_COMMAND
;
317 h
->txbuf
[1] = STLINK_DEBUG_READCOREID
;
319 res
= stlink_usb_recv(handle
, h
->txbuf
, STLINK_CMD_SIZE
, h
->rxbuf
, 4);
324 *idcode
= le_to_h_u32(h
->rxbuf
);
326 LOG_DEBUG("IDCODE: %08X", *idcode
);
332 enum target_state
stlink_usb_state(void *handle
)
335 struct stlink_usb_handle_s
*h
;
337 assert(handle
!= NULL
);
339 h
= (struct stlink_usb_handle_s
*)handle
;
341 stlink_usb_init_buffer(handle
);
343 h
->txbuf
[0] = STLINK_DEBUG_COMMAND
;
344 h
->txbuf
[1] = STLINK_DEBUG_GETSTATUS
;
346 res
= stlink_usb_recv(handle
, h
->txbuf
, STLINK_CMD_SIZE
, h
->rxbuf
, 2);
349 return TARGET_UNKNOWN
;
351 if (h
->rxbuf
[0] == STLINK_CORE_RUNNING
)
352 return TARGET_RUNNING
;
353 if (h
->rxbuf
[0] == STLINK_CORE_HALTED
)
354 return TARGET_HALTED
;
356 return TARGET_UNKNOWN
;
360 int stlink_usb_reset(void *handle
)
363 struct stlink_usb_handle_s
*h
;
365 assert(handle
!= NULL
);
367 h
= (struct stlink_usb_handle_s
*)handle
;
369 stlink_usb_init_buffer(handle
);
371 h
->txbuf
[0] = STLINK_DEBUG_COMMAND
;
372 h
->txbuf
[1] = STLINK_DEBUG_RESETSYS
;
374 res
= stlink_usb_recv(handle
, h
->txbuf
, STLINK_CMD_SIZE
, h
->rxbuf
, 2);
379 LOG_DEBUG("RESET: %08X", h
->rxbuf
[0]);
385 int stlink_usb_run(void *handle
)
388 struct stlink_usb_handle_s
*h
;
390 assert(handle
!= NULL
);
392 h
= (struct stlink_usb_handle_s
*)handle
;
394 stlink_usb_init_buffer(handle
);
396 h
->txbuf
[0] = STLINK_DEBUG_COMMAND
;
397 h
->txbuf
[1] = STLINK_DEBUG_RUNCORE
;
399 res
= stlink_usb_recv(handle
, h
->txbuf
, STLINK_CMD_SIZE
, h
->rxbuf
, 2);
408 int stlink_usb_halt(void *handle
)
411 struct stlink_usb_handle_s
*h
;
413 assert(handle
!= NULL
);
415 h
= (struct stlink_usb_handle_s
*)handle
;
417 stlink_usb_init_buffer(handle
);
419 h
->txbuf
[0] = STLINK_DEBUG_COMMAND
;
420 h
->txbuf
[1] = STLINK_DEBUG_FORCEDEBUG
;
422 res
= stlink_usb_recv(handle
, h
->txbuf
, STLINK_CMD_SIZE
, h
->rxbuf
, 2);
431 int stlink_usb_step(void *handle
)
434 struct stlink_usb_handle_s
*h
;
436 assert(handle
!= NULL
);
438 h
= (struct stlink_usb_handle_s
*)handle
;
440 stlink_usb_init_buffer(handle
);
442 h
->txbuf
[0] = STLINK_DEBUG_COMMAND
;
443 h
->txbuf
[1] = STLINK_DEBUG_STEPCORE
;
445 res
= stlink_usb_recv(handle
, h
->txbuf
, STLINK_CMD_SIZE
, h
->rxbuf
, 2);
454 int stlink_usb_read_regs(void *handle
)
457 struct stlink_usb_handle_s
*h
;
459 assert(handle
!= NULL
);
461 h
= (struct stlink_usb_handle_s
*)handle
;
463 stlink_usb_init_buffer(handle
);
465 h
->txbuf
[0] = STLINK_DEBUG_COMMAND
;
466 h
->txbuf
[1] = STLINK_DEBUG_READALLREGS
;
468 res
= stlink_usb_recv(handle
, h
->txbuf
, STLINK_CMD_SIZE
, h
->rxbuf
, 84);
477 int stlink_usb_read_reg(void *handle
, int num
, uint32_t *val
)
480 struct stlink_usb_handle_s
*h
;
482 assert(handle
!= NULL
);
484 h
= (struct stlink_usb_handle_s
*)handle
;
486 stlink_usb_init_buffer(handle
);
488 h
->txbuf
[0] = STLINK_DEBUG_COMMAND
;
489 h
->txbuf
[1] = STLINK_DEBUG_READREG
;
492 res
= stlink_usb_recv(handle
, h
->txbuf
, STLINK_CMD_SIZE
, h
->rxbuf
, 4);
497 *val
= le_to_h_u32(h
->rxbuf
);
503 int stlink_usb_write_reg(void *handle
, int num
, uint32_t val
)
506 struct stlink_usb_handle_s
*h
;
508 assert(handle
!= NULL
);
510 h
= (struct stlink_usb_handle_s
*)handle
;
512 stlink_usb_init_buffer(handle
);
514 h
->txbuf
[0] = STLINK_DEBUG_COMMAND
;
515 h
->txbuf
[1] = STLINK_DEBUG_WRITEREG
;
517 h_u32_to_le(h
->txbuf
+ 3, val
);
519 res
= stlink_usb_recv(handle
, h
->txbuf
, STLINK_CMD_SIZE
, h
->rxbuf
, 2);
528 int stlink_usb_read_mem8(void *handle
, uint32_t addr
, uint16_t len
,
532 uint16_t read_len
= len
;
533 struct stlink_usb_handle_s
*h
;
535 assert(handle
!= NULL
);
537 h
= (struct stlink_usb_handle_s
*)handle
;
539 stlink_usb_init_buffer(handle
);
541 h
->txbuf
[0] = STLINK_DEBUG_COMMAND
;
542 h
->txbuf
[1] = STLINK_DEBUG_READMEM_8BIT
;
543 h_u32_to_le(h
->txbuf
+ 2, addr
);
544 h_u16_to_le(h
->txbuf
+ 2 + 4, len
);
546 /* we need to fix read length for single bytes */
550 res
= stlink_usb_recv(handle
, h
->txbuf
, STLINK_CMD_SIZE
, h
->rxbuf
, read_len
);
555 memcpy(buffer
, h
->rxbuf
, len
);
561 int stlink_usb_write_mem8(void *handle
, uint32_t addr
, uint16_t len
,
562 const uint8_t *buffer
)
565 struct stlink_usb_handle_s
*h
;
567 assert(handle
!= NULL
);
569 h
= (struct stlink_usb_handle_s
*)handle
;
571 stlink_usb_init_buffer(handle
);
573 h
->txbuf
[0] = STLINK_DEBUG_COMMAND
;
574 h
->txbuf
[1] = STLINK_DEBUG_WRITEMEM_8BIT
;
575 h_u32_to_le(h
->txbuf
+ 2, addr
);
576 h_u16_to_le(h
->txbuf
+ 2 + 4, len
);
578 res
= stlink_usb_recv(handle
, h
->txbuf
, STLINK_CMD_SIZE
, 0, 0);
583 res
= stlink_usb_recv(handle
, (uint8_t *) buffer
, len
, 0, 0);
592 int stlink_usb_read_mem32(void *handle
, uint32_t addr
, uint16_t len
,
596 struct stlink_usb_handle_s
*h
;
598 assert(handle
!= NULL
);
600 h
= (struct stlink_usb_handle_s
*)handle
;
602 stlink_usb_init_buffer(handle
);
606 h
->txbuf
[0] = STLINK_DEBUG_COMMAND
;
607 h
->txbuf
[1] = STLINK_DEBUG_READMEM_32BIT
;
608 h_u32_to_le(h
->txbuf
+ 2, addr
);
609 h_u16_to_le(h
->txbuf
+ 2 + 4, len
);
611 res
= stlink_usb_recv(handle
, h
->txbuf
, STLINK_CMD_SIZE
, h
->rxbuf
, len
);
616 memcpy(buffer
, h
->rxbuf
, len
);
622 int stlink_usb_write_mem32(void *handle
, uint32_t addr
, uint16_t len
,
623 const uint32_t *buffer
)
626 struct stlink_usb_handle_s
*h
;
628 assert(handle
!= NULL
);
630 h
= (struct stlink_usb_handle_s
*)handle
;
632 stlink_usb_init_buffer(handle
);
636 h
->txbuf
[0] = STLINK_DEBUG_COMMAND
;
637 h
->txbuf
[1] = STLINK_DEBUG_WRITEMEM_32BIT
;
638 h_u32_to_le(h
->txbuf
+ 2, addr
);
639 h_u16_to_le(h
->txbuf
+ 2 + 4, len
);
641 res
= stlink_usb_recv(handle
, h
->txbuf
, STLINK_CMD_SIZE
, 0, 0);
646 res
= stlink_usb_recv(handle
, (uint8_t *) buffer
, len
, 0, 0);
655 int stlink_usb_open(struct stlink_interface_param_s
*param
, void **fd
)
657 struct stlink_usb_handle_s
*h
;
659 LOG_DEBUG("stlink_usb_open");
661 h
= malloc(sizeof(struct stlink_usb_handle_s
));
664 LOG_DEBUG("stlink_open_usb: malloc failed");
668 const uint16_t vids
[] = { param
->vid
, 0 };
669 const uint16_t pids
[] = { param
->pid
, 0 };
671 LOG_DEBUG("stlink_open_usb: vid: %04x pid: %04x", param
->vid
,
674 if (jtag_libusb_open(vids
, pids
, &h
->fd
) != ERROR_OK
) {
675 LOG_ERROR("stlink_open_usb: open failed");
679 jtag_libusb_set_configuration(h
->fd
, 0);
681 if (jtag_libusb_claim_interface(h
->fd
, 0) != ERROR_OK
) {
682 LOG_DEBUG("stlink_open_usb: claim failed");
686 stlink_usb_init_mode(h
);
688 stlink_usb_version(h
);
696 struct stlink_layout_api_s stlink_layout_api
= {
698 .open
= stlink_usb_open
,
700 .idcode
= stlink_usb_idcode
,
702 .state
= stlink_usb_state
,
704 .reset
= stlink_usb_reset
,
706 .run
= stlink_usb_run
,
708 .halt
= stlink_usb_halt
,
710 .step
= stlink_usb_step
,
712 .read_regs
= stlink_usb_read_regs
,
714 .read_reg
= stlink_usb_read_reg
,
716 .write_reg
= stlink_usb_write_reg
,
718 .read_mem8
= stlink_usb_read_mem8
,
720 .write_mem8
= stlink_usb_write_mem8
,
722 .read_mem32
= stlink_usb_read_mem32
,
724 .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)