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_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)
47 struct stlink_usb_version
{
57 struct stlink_usb_handle_s
{
59 struct jtag_libusb_device_handle
*fd
;
61 struct libusb_transfer
*trans
;
63 uint8_t txbuf
[STLINK_TX_SIZE
];
65 uint8_t rxbuf
[STLINK_RX_SIZE
];
67 enum stlink_transports transport
;
69 struct stlink_usb_version version
;
78 #define STLINK_OK 0x80
79 #define STLINK_FALSE 0x81
80 #define STLINK_CORE_RUNNING 0x80
81 #define STLINK_CORE_HALTED 0x81
82 #define STLINK_CORE_STAT_UNKNOWN -1
84 #define STLINK_GET_VERSION 0xF1
85 #define STLINK_DEBUG_COMMAND 0xF2
86 #define STLINK_DFU_COMMAND 0xF3
87 #define STLINK_SWIM_COMMAND 0xF4
88 #define STLINK_GET_CURRENT_MODE 0xF5
90 #define STLINK_DEV_DFU_MODE 0x00
91 #define STLINK_DEV_MASS_MODE 0x01
92 #define STLINK_DEV_DEBUG_MODE 0x02
93 #define STLINK_DEV_SWIM_MODE 0x03
94 #define STLINK_DEV_UNKNOWN_MODE -1
96 #define STLINK_DFU_EXIT 0x07
98 #define STLINK_SWIM_ENTER 0x00
99 #define STLINK_SWIM_EXIT 0x01
101 #define STLINK_DEBUG_ENTER_JTAG 0x00
102 #define STLINK_DEBUG_GETSTATUS 0x01
103 #define STLINK_DEBUG_FORCEDEBUG 0x02
104 #define STLINK_DEBUG_RESETSYS 0x03
105 #define STLINK_DEBUG_READALLREGS 0x04
106 #define STLINK_DEBUG_READREG 0x05
107 #define STLINK_DEBUG_WRITEREG 0x06
108 #define STLINK_DEBUG_READMEM_32BIT 0x07
109 #define STLINK_DEBUG_WRITEMEM_32BIT 0x08
110 #define STLINK_DEBUG_RUNCORE 0x09
111 #define STLINK_DEBUG_STEPCORE 0x0a
112 #define STLINK_DEBUG_SETFP 0x0b
113 #define STLINK_DEBUG_READMEM_8BIT 0x0c
114 #define STLINK_DEBUG_WRITEMEM_8BIT 0x0d
115 #define STLINK_DEBUG_CLEARFP 0x0e
116 #define STLINK_DEBUG_WRITEDEBUGREG 0x0f
118 #define STLINK_DEBUG_ENTER_JTAG 0x00
119 #define STLINK_DEBUG_ENTER_SWD 0xa3
121 #define STLINK_DEBUG_ENTER 0x20
122 #define STLINK_DEBUG_EXIT 0x21
123 #define STLINK_DEBUG_READCOREID 0x22
127 STLINK_MODE_UNKNOWN
= 0,
130 STLINK_MODE_DEBUG_JTAG
,
131 STLINK_MODE_DEBUG_SWD
,
132 STLINK_MODE_DEBUG_SWIM
136 static void stlink_usb_recv_v1_create_cmd(char *b
, int s
, uint32_t tag
, uint32_t rxsize
,
137 uint8_t flag
, uint8_t lun
, uint8_t length
)
143 /* fill the send buffer */
147 buf_set_u32(b
+i
, 0, 32, tag
);
149 buf_set_u32(b
+i
, 0, 32, rxsize
);
157 static int stlink_usb_recv_v1_mass_storage_cmd(void *handle
, const uint8_t *txbuf
, int txsize
, uint8_t *rxbuf
,
161 struct stlink_usb_handle_s
*h
;
163 assert(handle
!= NULL
);
165 h
= (struct stlink_usb_handle_s
*)handle
;
166 h
->sg_tag
= (h
->sg_tag
+ 1) & 1;
168 stlink_usb_recv_v1_create_cmd(sg_buffer
, 31, h
->sg_tag
, rxsize
, STLINK_TX_EP
, 0x00, txsize
);
170 memcpy(sg_buffer
+15, txbuf
, 10);
172 if (jtag_libusb_bulk_write(h
->fd
, STLINK_TX_EP
, (char *)sg_buffer
, 31,
174 printf("send failed\n");
181 #define REQUEST_SENSE 0x03
182 #define REQUEST_SENSE_LENGTH 18
185 static int stlink_usb_recv_v1_get_status(void *handle
, char *sg_buffer
, int len
)
187 struct stlink_usb_handle_s
*h
;
189 assert(handle
!= NULL
);
191 h
= (struct stlink_usb_handle_s
*)handle
;
194 memset(sg_buffer
, 0x00, len
);
196 if (jtag_libusb_bulk_read(h
->fd
, STLINK_RX_EP
, (char *)sg_buffer
,
202 t1
= buf_get_u32(sg_buffer
+0, 0, 32);
203 t2
= buf_get_u32(sg_buffer
+4, 0, 32);
206 if (t1
!= 0x53425355)
213 static int stlink_usb_recv_v1_get_sense(void *handle
)
215 struct stlink_usb_handle_s
*h
;
219 assert(handle
!= NULL
);
221 h
= (struct stlink_usb_handle_s
*)handle
;
222 h
->sg_tag
= (h
->sg_tag
+ 1) & 1;
224 cdb
[0] = REQUEST_SENSE
;
225 cdb
[4] = REQUEST_SENSE_LENGTH
;
227 stlink_usb_recv_v1_create_cmd(sg_buffer
, 31, h
->sg_tag
, REQUEST_SENSE_LENGTH
, STLINK_TX_EP
,
230 memcpy(sg_buffer
+15, cdb
, 16);
232 if (jtag_libusb_bulk_write(h
->fd
, STLINK_TX_EP
, (char *)sg_buffer
, 16,
236 if (jtag_libusb_bulk_read(h
->fd
, STLINK_RX_EP
, (char *)cdb
,
240 if (stlink_usb_recv_v1_get_status(handle
, sg_buffer
, 13) != ERROR_OK
)
242 /* check for sense */
243 if (sg_buffer
[12] != 0)
246 /* if (sense[0] != 0x70 && sense[0] != 0x71) */
252 static int stlink_usb_recv_v1(void *handle
, const uint8_t *txbuf
, int txsize
, uint8_t *rxbuf
,
257 struct stlink_usb_handle_s
*h
;
259 assert(handle
!= NULL
);
261 h
= (struct stlink_usb_handle_s
*)handle
;
263 err
= stlink_usb_recv_v1_mass_storage_cmd(handle
, txbuf
, txsize
, rxbuf
, rxsize
);
268 if (rxsize
&& rxbuf
) {
269 if (jtag_libusb_bulk_read(h
->fd
, STLINK_RX_EP
, (char *)rxbuf
,
270 rxsize
, 1000) != rxsize
) {
271 LOG_DEBUG("jtag_libusb_bulk_read");
276 if (stlink_usb_recv_v1_get_status(handle
, sg_buffer
, 13) != ERROR_OK
)
278 /* check for sense */
279 if (sg_buffer
[12] == 1) {
280 LOG_DEBUG("get sense");
281 err
= stlink_usb_recv_v1_get_sense(handle
);
287 static int stlink_usb_recv_v2(void *handle
, const uint8_t *txbuf
, int txsize
, uint8_t *rxbuf
,
290 struct stlink_usb_handle_s
*h
;
292 assert(handle
!= NULL
);
294 h
= (struct stlink_usb_handle_s
*)handle
;
296 if (jtag_libusb_bulk_write(h
->fd
, STLINK_TX_EP
, (char *)txbuf
, txsize
,
300 if (rxsize
&& rxbuf
) {
301 if (jtag_libusb_bulk_read(h
->fd
, STLINK_RX_EP
, (char *)rxbuf
,
302 rxsize
, 1000) != rxsize
) {
310 static int stlink_usb_recv(void *handle
, const uint8_t *txbuf
, int txsize
, uint8_t *rxbuf
,
313 struct stlink_usb_handle_s
*h
;
315 assert(handle
!= NULL
);
317 h
= (struct stlink_usb_handle_s
*)handle
;
319 if (h
->version
.stlink
== 1) {
320 return stlink_usb_recv_v1(handle
, txbuf
, txsize
, rxbuf
, rxsize
);
322 if (txsize
< STLINK_CMD_SIZE
)
323 txsize
= STLINK_CMD_SIZE
;
324 return stlink_usb_recv_v2(handle
, txbuf
, txsize
, rxbuf
, rxsize
);
329 static void stlink_usb_init_buffer(void *handle
)
331 struct stlink_usb_handle_s
*h
;
333 assert(handle
!= NULL
);
335 h
= (struct stlink_usb_handle_s
*)handle
;
337 memset(h
->txbuf
, 0, STLINK_CMD_SIZE
);
341 static int stlink_usb_version(void *handle
)
345 struct stlink_usb_handle_s
*h
;
347 assert(handle
!= NULL
);
349 h
= (struct stlink_usb_handle_s
*)handle
;
351 stlink_usb_init_buffer(handle
);
353 h
->txbuf
[0] = STLINK_GET_VERSION
;
355 res
= stlink_usb_recv(handle
, h
->txbuf
, STLINK_CMD_SIZE
, h
->rxbuf
, 6);
360 v
= (h
->rxbuf
[0] << 8) | h
->rxbuf
[1];
362 h
->version
.stlink
= (v
>> 12) & 0x0f;
363 h
->version
.jtag
= (v
>> 6) & 0x3f;
364 h
->version
.swim
= v
& 0x3f;
365 h
->vid
= buf_get_u32(h
->rxbuf
, 16, 16);
366 h
->pid
= buf_get_u32(h
->rxbuf
, 32, 16);
368 LOG_DEBUG("STLINK v%d JTAG v%d SWIM v%d VID %04X PID %04X",
379 static int stlink_usb_current_mode(void *handle
, uint8_t *mode
)
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
);
390 h
->txbuf
[0] = STLINK_GET_CURRENT_MODE
;
392 res
= stlink_usb_recv(handle
, h
->txbuf
, STLINK_CMD_SIZE
, h
->rxbuf
, 2);
403 static int stlink_usb_mode_enter(void *handle
, enum stlink_mode type
)
406 struct stlink_usb_handle_s
*h
;
408 assert(handle
!= NULL
);
410 h
= (struct stlink_usb_handle_s
*)handle
;
412 stlink_usb_init_buffer(handle
);
415 case STLINK_MODE_DEBUG_JTAG
:
416 h
->txbuf
[0] = STLINK_DEBUG_COMMAND
;
417 h
->txbuf
[1] = STLINK_DEBUG_ENTER
;
418 h
->txbuf
[2] = STLINK_DEBUG_ENTER_JTAG
;
420 case STLINK_MODE_DEBUG_SWD
:
421 h
->txbuf
[0] = STLINK_DEBUG_COMMAND
;
422 h
->txbuf
[1] = STLINK_DEBUG_ENTER
;
423 h
->txbuf
[2] = STLINK_DEBUG_ENTER_SWD
;
425 case STLINK_MODE_DEBUG_SWIM
:
426 h
->txbuf
[0] = STLINK_SWIM_COMMAND
;
427 h
->txbuf
[1] = STLINK_SWIM_ENTER
;
429 case STLINK_MODE_DFU
:
430 case STLINK_MODE_MASS
:
435 res
= stlink_usb_recv(handle
, h
->txbuf
, STLINK_CMD_SIZE
, 0, 0);
443 static int stlink_usb_mode_leave(void *handle
, enum stlink_mode type
)
446 struct stlink_usb_handle_s
*h
;
448 assert(handle
!= NULL
);
450 h
= (struct stlink_usb_handle_s
*)handle
;
452 stlink_usb_init_buffer(handle
);
455 case STLINK_MODE_DEBUG_JTAG
:
456 case STLINK_MODE_DEBUG_SWD
:
457 h
->txbuf
[0] = STLINK_DEBUG_COMMAND
;
458 h
->txbuf
[1] = STLINK_DEBUG_EXIT
;
460 case STLINK_MODE_DEBUG_SWIM
:
461 h
->txbuf
[0] = STLINK_SWIM_COMMAND
;
462 h
->txbuf
[1] = STLINK_SWIM_EXIT
;
464 case STLINK_MODE_DFU
:
465 h
->txbuf
[0] = STLINK_DFU_COMMAND
;
466 h
->txbuf
[1] = STLINK_DFU_EXIT
;
468 case STLINK_MODE_MASS
:
473 res
= stlink_usb_recv(handle
, h
->txbuf
, STLINK_CMD_SIZE
, 0, 0);
481 static int stlink_usb_init_mode(void *handle
)
485 enum stlink_mode emode
;
486 struct stlink_usb_handle_s
*h
;
488 assert(handle
!= NULL
);
490 h
= (struct stlink_usb_handle_s
*)handle
;
492 res
= stlink_usb_current_mode(handle
, &mode
);
497 LOG_DEBUG("MODE: %02X", mode
);
499 /* try to exit current mode */
501 case STLINK_DEV_DFU_MODE
:
502 emode
= STLINK_MODE_DFU
;
504 case STLINK_DEV_DEBUG_MODE
:
505 emode
= STLINK_MODE_DEBUG_SWD
;
507 case STLINK_DEV_SWIM_MODE
:
508 emode
= STLINK_MODE_DEBUG_SWIM
;
511 emode
= STLINK_MODE_UNKNOWN
;
515 if (emode
!= STLINK_MODE_UNKNOWN
) {
516 res
= stlink_usb_mode_leave(handle
, emode
);
522 res
= stlink_usb_current_mode(handle
, &mode
);
527 LOG_DEBUG("MODE: %02X", mode
);
529 /* set selected mode */
530 switch (h
->transport
) {
531 case STLINK_TRANSPORT_SWD
:
532 emode
= STLINK_MODE_DEBUG_SWD
;
534 case STLINK_TRANSPORT_JTAG
:
535 emode
= STLINK_MODE_DEBUG_JTAG
;
537 case STLINK_TRANSPORT_SWIM
:
538 emode
= STLINK_MODE_DEBUG_SWIM
;
541 emode
= STLINK_MODE_UNKNOWN
;
545 if (emode
== STLINK_MODE_UNKNOWN
) {
546 LOG_ERROR("selected mode (transport) not supported");
550 res
= stlink_usb_mode_enter(handle
, emode
);
555 res
= stlink_usb_current_mode(handle
, &mode
);
560 LOG_DEBUG("MODE: %02X", mode
);
566 static int stlink_usb_idcode(void *handle
, uint32_t *idcode
)
569 struct stlink_usb_handle_s
*h
;
571 assert(handle
!= NULL
);
573 h
= (struct stlink_usb_handle_s
*)handle
;
575 stlink_usb_init_buffer(handle
);
577 h
->txbuf
[0] = STLINK_DEBUG_COMMAND
;
578 h
->txbuf
[1] = STLINK_DEBUG_READCOREID
;
580 res
= stlink_usb_recv(handle
, h
->txbuf
, STLINK_CMD_SIZE
, h
->rxbuf
, 4);
585 *idcode
= le_to_h_u32(h
->rxbuf
);
587 LOG_DEBUG("IDCODE: %08X", *idcode
);
593 static enum target_state
stlink_usb_state(void *handle
)
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
);
604 h
->txbuf
[0] = STLINK_DEBUG_COMMAND
;
605 h
->txbuf
[1] = STLINK_DEBUG_GETSTATUS
;
607 res
= stlink_usb_recv(handle
, h
->txbuf
, STLINK_CMD_SIZE
, h
->rxbuf
, 2);
610 return TARGET_UNKNOWN
;
612 if (h
->rxbuf
[0] == STLINK_CORE_RUNNING
)
613 return TARGET_RUNNING
;
614 if (h
->rxbuf
[0] == STLINK_CORE_HALTED
)
615 return TARGET_HALTED
;
617 return TARGET_UNKNOWN
;
621 static int stlink_usb_reset(void *handle
)
624 struct stlink_usb_handle_s
*h
;
626 assert(handle
!= NULL
);
628 h
= (struct stlink_usb_handle_s
*)handle
;
630 stlink_usb_init_buffer(handle
);
632 h
->txbuf
[0] = STLINK_DEBUG_COMMAND
;
633 h
->txbuf
[1] = STLINK_DEBUG_RESETSYS
;
635 res
= stlink_usb_recv(handle
, h
->txbuf
, STLINK_CMD_SIZE
, h
->rxbuf
, 2);
640 LOG_DEBUG("RESET: %08X", h
->rxbuf
[0]);
646 static int stlink_usb_run(void *handle
)
649 struct stlink_usb_handle_s
*h
;
651 assert(handle
!= NULL
);
653 h
= (struct stlink_usb_handle_s
*)handle
;
655 stlink_usb_init_buffer(handle
);
657 h
->txbuf
[0] = STLINK_DEBUG_COMMAND
;
658 h
->txbuf
[1] = STLINK_DEBUG_RUNCORE
;
660 res
= stlink_usb_recv(handle
, h
->txbuf
, STLINK_CMD_SIZE
, h
->rxbuf
, 2);
669 static int stlink_usb_halt(void *handle
)
672 struct stlink_usb_handle_s
*h
;
674 assert(handle
!= NULL
);
676 h
= (struct stlink_usb_handle_s
*)handle
;
678 stlink_usb_init_buffer(handle
);
680 h
->txbuf
[0] = STLINK_DEBUG_COMMAND
;
681 h
->txbuf
[1] = STLINK_DEBUG_FORCEDEBUG
;
683 res
= stlink_usb_recv(handle
, h
->txbuf
, STLINK_CMD_SIZE
, h
->rxbuf
, 2);
692 static int stlink_usb_step(void *handle
)
695 struct stlink_usb_handle_s
*h
;
697 assert(handle
!= NULL
);
699 h
= (struct stlink_usb_handle_s
*)handle
;
701 stlink_usb_init_buffer(handle
);
703 h
->txbuf
[0] = STLINK_DEBUG_COMMAND
;
704 h
->txbuf
[1] = STLINK_DEBUG_STEPCORE
;
706 res
= stlink_usb_recv(handle
, h
->txbuf
, STLINK_CMD_SIZE
, h
->rxbuf
, 2);
715 static int stlink_usb_read_regs(void *handle
)
718 struct stlink_usb_handle_s
*h
;
720 assert(handle
!= NULL
);
722 h
= (struct stlink_usb_handle_s
*)handle
;
724 stlink_usb_init_buffer(handle
);
726 h
->txbuf
[0] = STLINK_DEBUG_COMMAND
;
727 h
->txbuf
[1] = STLINK_DEBUG_READALLREGS
;
729 res
= stlink_usb_recv(handle
, h
->txbuf
, STLINK_CMD_SIZE
, h
->rxbuf
, 84);
738 static int stlink_usb_read_reg(void *handle
, int num
, uint32_t *val
)
741 struct stlink_usb_handle_s
*h
;
743 assert(handle
!= NULL
);
745 h
= (struct stlink_usb_handle_s
*)handle
;
747 stlink_usb_init_buffer(handle
);
749 h
->txbuf
[0] = STLINK_DEBUG_COMMAND
;
750 h
->txbuf
[1] = STLINK_DEBUG_READREG
;
753 res
= stlink_usb_recv(handle
, h
->txbuf
, STLINK_CMD_SIZE
, h
->rxbuf
, 4);
758 *val
= le_to_h_u32(h
->rxbuf
);
764 static int stlink_usb_write_reg(void *handle
, int num
, uint32_t val
)
767 struct stlink_usb_handle_s
*h
;
769 assert(handle
!= NULL
);
771 h
= (struct stlink_usb_handle_s
*)handle
;
773 stlink_usb_init_buffer(handle
);
775 h
->txbuf
[0] = STLINK_DEBUG_COMMAND
;
776 h
->txbuf
[1] = STLINK_DEBUG_WRITEREG
;
778 h_u32_to_le(h
->txbuf
+ 3, val
);
780 res
= stlink_usb_recv(handle
, h
->txbuf
, STLINK_CMD_SIZE
, h
->rxbuf
, 2);
789 static int stlink_usb_read_mem8(void *handle
, uint32_t addr
, uint16_t len
,
793 uint16_t read_len
= len
;
794 struct stlink_usb_handle_s
*h
;
796 assert(handle
!= NULL
);
798 h
= (struct stlink_usb_handle_s
*)handle
;
800 stlink_usb_init_buffer(handle
);
802 h
->txbuf
[0] = STLINK_DEBUG_COMMAND
;
803 h
->txbuf
[1] = STLINK_DEBUG_READMEM_8BIT
;
804 h_u32_to_le(h
->txbuf
+ 2, addr
);
805 h_u16_to_le(h
->txbuf
+ 2 + 4, len
);
807 /* we need to fix read length for single bytes */
811 res
= stlink_usb_recv(handle
, h
->txbuf
, STLINK_CMD_SIZE
, h
->rxbuf
, read_len
);
816 memcpy(buffer
, h
->rxbuf
, len
);
822 static int stlink_usb_write_mem8(void *handle
, uint32_t addr
, uint16_t len
,
823 const uint8_t *buffer
)
826 struct stlink_usb_handle_s
*h
;
828 assert(handle
!= NULL
);
830 h
= (struct stlink_usb_handle_s
*)handle
;
832 stlink_usb_init_buffer(handle
);
834 h
->txbuf
[0] = STLINK_DEBUG_COMMAND
;
835 h
->txbuf
[1] = STLINK_DEBUG_WRITEMEM_8BIT
;
836 h_u32_to_le(h
->txbuf
+ 2, addr
);
837 h_u16_to_le(h
->txbuf
+ 2 + 4, len
);
839 res
= stlink_usb_recv(handle
, h
->txbuf
, STLINK_CMD_SIZE
, 0, 0);
844 res
= stlink_usb_recv(handle
, (uint8_t *) buffer
, len
, 0, 0);
853 static int stlink_usb_read_mem32(void *handle
, uint32_t addr
, uint16_t len
,
857 struct stlink_usb_handle_s
*h
;
859 assert(handle
!= NULL
);
861 h
= (struct stlink_usb_handle_s
*)handle
;
863 stlink_usb_init_buffer(handle
);
867 h
->txbuf
[0] = STLINK_DEBUG_COMMAND
;
868 h
->txbuf
[1] = STLINK_DEBUG_READMEM_32BIT
;
869 h_u32_to_le(h
->txbuf
+ 2, addr
);
870 h_u16_to_le(h
->txbuf
+ 2 + 4, len
);
872 res
= stlink_usb_recv(handle
, h
->txbuf
, STLINK_CMD_SIZE
, h
->rxbuf
, len
);
877 memcpy(buffer
, h
->rxbuf
, len
);
883 static int stlink_usb_write_mem32(void *handle
, uint32_t addr
, uint16_t len
,
884 const uint32_t *buffer
)
887 struct stlink_usb_handle_s
*h
;
889 assert(handle
!= NULL
);
891 h
= (struct stlink_usb_handle_s
*)handle
;
893 stlink_usb_init_buffer(handle
);
897 h
->txbuf
[0] = STLINK_DEBUG_COMMAND
;
898 h
->txbuf
[1] = STLINK_DEBUG_WRITEMEM_32BIT
;
899 h_u32_to_le(h
->txbuf
+ 2, addr
);
900 h_u16_to_le(h
->txbuf
+ 2 + 4, len
);
902 res
= stlink_usb_recv(handle
, h
->txbuf
, STLINK_CMD_SIZE
, 0, 0);
907 res
= stlink_usb_recv(handle
, (uint8_t *) buffer
, len
, 0, 0);
916 static int stlink_usb_open(struct stlink_interface_param_s
*param
, void **fd
)
919 struct stlink_usb_handle_s
*h
;
921 LOG_DEBUG("stlink_usb_open");
923 h
= malloc(sizeof(struct stlink_usb_handle_s
));
926 LOG_DEBUG("malloc failed");
930 h
->transport
= param
->transport
;
932 const uint16_t vids
[] = { param
->vid
, 0 };
933 const uint16_t pids
[] = { param
->pid
, 0 };
935 LOG_DEBUG("transport: %d vid: %04x pid: %04x", param
->transport
,
936 param
->vid
, param
->pid
);
938 if (jtag_libusb_open(vids
, pids
, &h
->fd
) != ERROR_OK
) {
939 LOG_ERROR("open failed");
943 jtag_libusb_set_configuration(h
->fd
, 0);
945 if (jtag_libusb_claim_interface(h
->fd
, 0) != ERROR_OK
) {
946 LOG_DEBUG("claim interface failed");
950 /* wrap version for first read */
951 switch (param
->pid
) {
953 h
->version
.stlink
= 1;
956 h
->version
.stlink
= 2;
960 /* get the device version */
961 err
= stlink_usb_version(h
);
963 if (err
!= ERROR_OK
) {
964 LOG_ERROR("read version failed");
965 jtag_libusb_close(h
->fd
);
970 /* compare usb vid/pid */
971 if ((param
->vid
!= h
->vid
) || (param
->pid
!= h
->pid
))
972 LOG_INFO("vid/pid are not identical: %04X/%04X %04X/%04X",
973 param
->vid
, param
->pid
,
976 /* check if mode is supported */
979 switch (h
->transport
) {
980 case STLINK_TRANSPORT_SWD
:
981 case STLINK_TRANSPORT_JTAG
:
982 if (h
->version
.jtag
== 0)
985 case STLINK_TRANSPORT_SWIM
:
986 if (h
->version
.swim
== 0)
994 if (err
!= ERROR_OK
) {
995 LOG_ERROR("mode (transport) not supported by device");
996 jtag_libusb_close(h
->fd
);
1001 err
= stlink_usb_init_mode(h
);
1003 if (err
!= ERROR_OK
) {
1004 LOG_ERROR("init mode failed");
1005 jtag_libusb_close(h
->fd
);
1016 static int stlink_usb_close(void *fd
)
1022 struct stlink_layout_api_s stlink_usb_layout_api
= {
1024 .open
= stlink_usb_open
,
1026 .close
= stlink_usb_close
,
1028 .idcode
= stlink_usb_idcode
,
1030 .state
= stlink_usb_state
,
1032 .reset
= stlink_usb_reset
,
1034 .run
= stlink_usb_run
,
1036 .halt
= stlink_usb_halt
,
1038 .step
= stlink_usb_step
,
1040 .read_regs
= stlink_usb_read_regs
,
1042 .read_reg
= stlink_usb_read_reg
,
1044 .write_reg
= stlink_usb_write_reg
,
1046 .read_mem8
= stlink_usb_read_mem8
,
1048 .write_mem8
= stlink_usb_write_mem8
,
1050 .read_mem32
= stlink_usb_read_mem32
,
1052 .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)