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_OK 0x80
88 #define STLINK_FALSE 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
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
145 STLINK_MODE_UNKNOWN
= 0,
148 STLINK_MODE_DEBUG_JTAG
,
149 STLINK_MODE_DEBUG_SWD
,
150 STLINK_MODE_DEBUG_SWIM
154 static void stlink_usb_recv_v1_create_cmd(char *b
, int s
, uint32_t tag
, uint32_t rxsize
,
155 uint8_t flag
, uint8_t lun
, uint8_t length
)
161 /* fill the send buffer */
165 buf_set_u32(b
+i
, 0, 32, tag
);
167 buf_set_u32(b
+i
, 0, 32, rxsize
);
175 static int stlink_usb_recv_v1_mass_storage_cmd(void *handle
, const uint8_t *txbuf
, int txsize
, uint8_t *rxbuf
,
179 struct stlink_usb_handle_s
*h
;
181 assert(handle
!= NULL
);
183 h
= (struct stlink_usb_handle_s
*)handle
;
184 h
->sg_tag
= (h
->sg_tag
+ 1) & 1;
186 stlink_usb_recv_v1_create_cmd(sg_buffer
, 31, h
->sg_tag
, rxsize
, STLINK_TX_EP
, 0x00, txsize
);
188 memcpy(sg_buffer
+15, txbuf
, 10);
190 if (jtag_libusb_bulk_write(h
->fd
, STLINK_TX_EP
, (char *)sg_buffer
, 31,
192 printf("send failed\n");
199 #define REQUEST_SENSE 0x03
200 #define REQUEST_SENSE_LENGTH 18
203 static int stlink_usb_recv_v1_get_status(void *handle
, char *sg_buffer
, int len
)
205 struct stlink_usb_handle_s
*h
;
207 assert(handle
!= NULL
);
209 h
= (struct stlink_usb_handle_s
*)handle
;
212 memset(sg_buffer
, 0x00, len
);
214 if (jtag_libusb_bulk_read(h
->fd
, STLINK_RX_EP
, (char *)sg_buffer
,
220 t1
= buf_get_u32(sg_buffer
+0, 0, 32);
221 t2
= buf_get_u32(sg_buffer
+4, 0, 32);
224 if (t1
!= 0x53425355)
231 static int stlink_usb_recv_v1_get_sense(void *handle
)
233 struct stlink_usb_handle_s
*h
;
237 assert(handle
!= NULL
);
239 h
= (struct stlink_usb_handle_s
*)handle
;
240 h
->sg_tag
= (h
->sg_tag
+ 1) & 1;
242 cdb
[0] = REQUEST_SENSE
;
243 cdb
[4] = REQUEST_SENSE_LENGTH
;
245 stlink_usb_recv_v1_create_cmd(sg_buffer
, 31, h
->sg_tag
, REQUEST_SENSE_LENGTH
, STLINK_TX_EP
,
248 memcpy(sg_buffer
+15, cdb
, 16);
250 if (jtag_libusb_bulk_write(h
->fd
, STLINK_TX_EP
, (char *)sg_buffer
, 16,
254 if (jtag_libusb_bulk_read(h
->fd
, STLINK_RX_EP
, (char *)cdb
,
258 if (stlink_usb_recv_v1_get_status(handle
, sg_buffer
, 13) != ERROR_OK
)
260 /* check for sense */
261 if (sg_buffer
[12] != 0)
264 /* if (sense[0] != 0x70 && sense[0] != 0x71) */
270 static int stlink_usb_recv_v1(void *handle
, const uint8_t *txbuf
, int txsize
, uint8_t *rxbuf
,
275 struct stlink_usb_handle_s
*h
;
277 assert(handle
!= NULL
);
279 h
= (struct stlink_usb_handle_s
*)handle
;
281 err
= stlink_usb_recv_v1_mass_storage_cmd(handle
, txbuf
, txsize
, rxbuf
, rxsize
);
286 if (rxsize
&& rxbuf
) {
287 if (jtag_libusb_bulk_read(h
->fd
, STLINK_RX_EP
, (char *)rxbuf
,
288 rxsize
, 1000) != rxsize
) {
289 LOG_DEBUG("jtag_libusb_bulk_read");
294 if (stlink_usb_recv_v1_get_status(handle
, sg_buffer
, 13) != ERROR_OK
)
296 /* check for sense */
297 if (sg_buffer
[12] == 1) {
298 LOG_DEBUG("get sense");
299 err
= stlink_usb_recv_v1_get_sense(handle
);
305 static int stlink_usb_recv_v2(void *handle
, const uint8_t *txbuf
, int txsize
, uint8_t *rxbuf
,
308 struct stlink_usb_handle_s
*h
;
310 assert(handle
!= NULL
);
312 h
= (struct stlink_usb_handle_s
*)handle
;
314 if (jtag_libusb_bulk_write(h
->fd
, STLINK_TX_EP
, (char *)txbuf
, txsize
,
318 if (rxsize
&& rxbuf
) {
319 if (jtag_libusb_bulk_read(h
->fd
, STLINK_RX_EP
, (char *)rxbuf
,
320 rxsize
, 1000) != rxsize
) {
328 static int stlink_usb_recv(void *handle
, const uint8_t *txbuf
, int txsize
, uint8_t *rxbuf
,
331 struct stlink_usb_handle_s
*h
;
333 assert(handle
!= NULL
);
335 h
= (struct stlink_usb_handle_s
*)handle
;
337 if (h
->version
.stlink
== 1) {
338 return stlink_usb_recv_v1(handle
, txbuf
, txsize
, rxbuf
, rxsize
);
340 if (txsize
< STLINK_CMD_SIZE
)
341 txsize
= STLINK_CMD_SIZE
;
342 return stlink_usb_recv_v2(handle
, txbuf
, txsize
, rxbuf
, rxsize
);
347 static void stlink_usb_init_buffer(void *handle
)
349 struct stlink_usb_handle_s
*h
;
351 assert(handle
!= NULL
);
353 h
= (struct stlink_usb_handle_s
*)handle
;
355 memset(h
->txbuf
, 0, STLINK_CMD_SIZE
);
359 static int stlink_usb_version(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_GET_VERSION
;
373 res
= stlink_usb_recv(handle
, h
->txbuf
, STLINK_CMD_SIZE
, h
->rxbuf
, 6);
378 v
= (h
->rxbuf
[0] << 8) | h
->rxbuf
[1];
380 h
->version
.stlink
= (v
>> 12) & 0x0f;
381 h
->version
.jtag
= (v
>> 6) & 0x3f;
382 h
->version
.swim
= v
& 0x3f;
383 h
->vid
= buf_get_u32(h
->rxbuf
, 16, 16);
384 h
->pid
= buf_get_u32(h
->rxbuf
, 32, 16);
386 /* set the supported jtag api version
387 * V1 doesn't support API V2 at all
388 * V2 support API V2 since JTAG V13
390 if ((h
->version
.stlink
== 2) && (h
->version
.jtag
> 12))
391 h
->version
.jtag_api_max
= STLINK_JTAG_API_V2
;
393 h
->version
.jtag_api_max
= STLINK_JTAG_API_V1
;
395 LOG_DEBUG("STLINK v%d JTAG v%d API v%d SWIM v%d VID %04X PID %04X",
398 (h
->version
.jtag_api_max
== STLINK_JTAG_API_V1
) ? 1 : 2,
407 static int stlink_usb_current_mode(void *handle
, uint8_t *mode
)
410 struct stlink_usb_handle_s
*h
;
412 assert(handle
!= NULL
);
414 h
= (struct stlink_usb_handle_s
*)handle
;
416 stlink_usb_init_buffer(handle
);
418 h
->txbuf
[0] = STLINK_GET_CURRENT_MODE
;
420 res
= stlink_usb_recv(handle
, h
->txbuf
, STLINK_CMD_SIZE
, h
->rxbuf
, 2);
431 static int stlink_usb_mode_enter(void *handle
, enum stlink_mode type
)
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
);
443 case STLINK_MODE_DEBUG_JTAG
:
444 h
->txbuf
[0] = STLINK_DEBUG_COMMAND
;
445 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
446 h
->txbuf
[1] = STLINK_DEBUG_APIV1_ENTER
;
448 h
->txbuf
[1] = STLINK_DEBUG_APIV2_ENTER
;
449 h
->txbuf
[2] = STLINK_DEBUG_ENTER_JTAG
;
451 case STLINK_MODE_DEBUG_SWD
:
452 h
->txbuf
[0] = STLINK_DEBUG_COMMAND
;
453 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
454 h
->txbuf
[1] = STLINK_DEBUG_APIV1_ENTER
;
456 h
->txbuf
[1] = STLINK_DEBUG_APIV2_ENTER
;
457 h
->txbuf
[2] = STLINK_DEBUG_ENTER_SWD
;
459 case STLINK_MODE_DEBUG_SWIM
:
460 h
->txbuf
[0] = STLINK_SWIM_COMMAND
;
461 h
->txbuf
[1] = STLINK_SWIM_ENTER
;
463 case STLINK_MODE_DFU
:
464 case STLINK_MODE_MASS
:
469 res
= stlink_usb_recv(handle
, h
->txbuf
, STLINK_CMD_SIZE
, 0, 0);
477 static int stlink_usb_mode_leave(void *handle
, enum stlink_mode type
)
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
);
489 case STLINK_MODE_DEBUG_JTAG
:
490 case STLINK_MODE_DEBUG_SWD
:
491 h
->txbuf
[0] = STLINK_DEBUG_COMMAND
;
492 h
->txbuf
[1] = STLINK_DEBUG_EXIT
;
494 case STLINK_MODE_DEBUG_SWIM
:
495 h
->txbuf
[0] = STLINK_SWIM_COMMAND
;
496 h
->txbuf
[1] = STLINK_SWIM_EXIT
;
498 case STLINK_MODE_DFU
:
499 h
->txbuf
[0] = STLINK_DFU_COMMAND
;
500 h
->txbuf
[1] = STLINK_DFU_EXIT
;
502 case STLINK_MODE_MASS
:
507 res
= stlink_usb_recv(handle
, h
->txbuf
, STLINK_CMD_SIZE
, 0, 0);
515 static int stlink_usb_init_mode(void *handle
)
519 enum stlink_mode emode
;
520 struct stlink_usb_handle_s
*h
;
522 assert(handle
!= NULL
);
524 h
= (struct stlink_usb_handle_s
*)handle
;
526 res
= stlink_usb_current_mode(handle
, &mode
);
531 LOG_DEBUG("MODE: %02X", mode
);
533 /* try to exit current mode */
535 case STLINK_DEV_DFU_MODE
:
536 emode
= STLINK_MODE_DFU
;
538 case STLINK_DEV_DEBUG_MODE
:
539 emode
= STLINK_MODE_DEBUG_SWD
;
541 case STLINK_DEV_SWIM_MODE
:
542 emode
= STLINK_MODE_DEBUG_SWIM
;
544 case STLINK_DEV_BOOTLOADER_MODE
:
546 emode
= STLINK_MODE_UNKNOWN
;
550 if (emode
!= STLINK_MODE_UNKNOWN
) {
551 res
= stlink_usb_mode_leave(handle
, emode
);
557 res
= stlink_usb_current_mode(handle
, &mode
);
562 LOG_DEBUG("MODE: %02X", mode
);
564 /* set selected mode */
565 switch (h
->transport
) {
566 case STLINK_TRANSPORT_SWD
:
567 emode
= STLINK_MODE_DEBUG_SWD
;
569 case STLINK_TRANSPORT_JTAG
:
570 emode
= STLINK_MODE_DEBUG_JTAG
;
572 case STLINK_TRANSPORT_SWIM
:
573 emode
= STLINK_MODE_DEBUG_SWIM
;
576 emode
= STLINK_MODE_UNKNOWN
;
580 if (emode
== STLINK_MODE_UNKNOWN
) {
581 LOG_ERROR("selected mode (transport) not supported");
585 res
= stlink_usb_mode_enter(handle
, emode
);
590 res
= stlink_usb_current_mode(handle
, &mode
);
595 LOG_DEBUG("MODE: %02X", mode
);
601 static int stlink_usb_idcode(void *handle
, uint32_t *idcode
)
604 struct stlink_usb_handle_s
*h
;
606 assert(handle
!= NULL
);
608 h
= (struct stlink_usb_handle_s
*)handle
;
610 stlink_usb_init_buffer(handle
);
612 h
->txbuf
[0] = STLINK_DEBUG_COMMAND
;
613 h
->txbuf
[1] = STLINK_DEBUG_READCOREID
;
615 res
= stlink_usb_recv(handle
, h
->txbuf
, STLINK_CMD_SIZE
, h
->rxbuf
, 4);
620 *idcode
= le_to_h_u32(h
->rxbuf
);
622 LOG_DEBUG("IDCODE: %08X", *idcode
);
628 static enum target_state
stlink_usb_state(void *handle
)
631 struct stlink_usb_handle_s
*h
;
633 assert(handle
!= NULL
);
635 h
= (struct stlink_usb_handle_s
*)handle
;
637 if (h
->jtag_api
== STLINK_JTAG_API_V2
)
638 return TARGET_UNKNOWN
;
640 stlink_usb_init_buffer(handle
);
642 h
->txbuf
[0] = STLINK_DEBUG_COMMAND
;
643 h
->txbuf
[1] = STLINK_DEBUG_GETSTATUS
;
645 res
= stlink_usb_recv(handle
, h
->txbuf
, STLINK_CMD_SIZE
, h
->rxbuf
, 2);
648 return TARGET_UNKNOWN
;
650 if (h
->rxbuf
[0] == STLINK_CORE_RUNNING
)
651 return TARGET_RUNNING
;
652 if (h
->rxbuf
[0] == STLINK_CORE_HALTED
)
653 return TARGET_HALTED
;
655 return TARGET_UNKNOWN
;
659 static int stlink_usb_reset(void *handle
)
662 struct stlink_usb_handle_s
*h
;
664 assert(handle
!= NULL
);
666 h
= (struct stlink_usb_handle_s
*)handle
;
668 stlink_usb_init_buffer(handle
);
670 h
->txbuf
[0] = STLINK_DEBUG_COMMAND
;
672 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
673 h
->txbuf
[1] = STLINK_DEBUG_APIV1_RESETSYS
;
675 h
->txbuf
[1] = STLINK_DEBUG_APIV2_RESETSYS
;
677 res
= stlink_usb_recv(handle
, h
->txbuf
, STLINK_CMD_SIZE
, h
->rxbuf
, 2);
682 LOG_DEBUG("RESET: %08X", h
->rxbuf
[0]);
688 static int stlink_usb_run(void *handle
)
691 struct stlink_usb_handle_s
*h
;
693 assert(handle
!= NULL
);
695 h
= (struct stlink_usb_handle_s
*)handle
;
697 if (h
->jtag_api
== STLINK_JTAG_API_V2
)
700 stlink_usb_init_buffer(handle
);
702 h
->txbuf
[0] = STLINK_DEBUG_COMMAND
;
703 h
->txbuf
[1] = STLINK_DEBUG_RUNCORE
;
705 res
= stlink_usb_recv(handle
, h
->txbuf
, STLINK_CMD_SIZE
, h
->rxbuf
, 2);
714 static int stlink_usb_halt(void *handle
)
717 struct stlink_usb_handle_s
*h
;
719 assert(handle
!= NULL
);
721 h
= (struct stlink_usb_handle_s
*)handle
;
723 if (h
->jtag_api
== STLINK_JTAG_API_V2
)
726 stlink_usb_init_buffer(handle
);
728 h
->txbuf
[0] = STLINK_DEBUG_COMMAND
;
729 h
->txbuf
[1] = STLINK_DEBUG_FORCEDEBUG
;
731 res
= stlink_usb_recv(handle
, h
->txbuf
, STLINK_CMD_SIZE
, h
->rxbuf
, 2);
740 static int stlink_usb_step(void *handle
)
743 struct stlink_usb_handle_s
*h
;
745 assert(handle
!= NULL
);
747 h
= (struct stlink_usb_handle_s
*)handle
;
749 if (h
->jtag_api
== STLINK_JTAG_API_V2
)
752 stlink_usb_init_buffer(handle
);
754 h
->txbuf
[0] = STLINK_DEBUG_COMMAND
;
755 h
->txbuf
[1] = STLINK_DEBUG_STEPCORE
;
757 res
= stlink_usb_recv(handle
, h
->txbuf
, STLINK_CMD_SIZE
, h
->rxbuf
, 2);
766 static int stlink_usb_read_regs(void *handle
)
769 struct stlink_usb_handle_s
*h
;
771 assert(handle
!= NULL
);
773 h
= (struct stlink_usb_handle_s
*)handle
;
775 stlink_usb_init_buffer(handle
);
777 h
->txbuf
[0] = STLINK_DEBUG_COMMAND
;
778 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
779 h
->txbuf
[1] = STLINK_DEBUG_APIV1_READALLREGS
;
781 h
->txbuf
[1] = STLINK_DEBUG_APIV2_READALLREGS
;
783 res
= stlink_usb_recv(handle
, h
->txbuf
, STLINK_CMD_SIZE
, h
->rxbuf
, 84);
792 static int stlink_usb_read_reg(void *handle
, int num
, uint32_t *val
)
795 struct stlink_usb_handle_s
*h
;
797 assert(handle
!= NULL
);
799 h
= (struct stlink_usb_handle_s
*)handle
;
801 stlink_usb_init_buffer(handle
);
803 h
->txbuf
[0] = STLINK_DEBUG_COMMAND
;
804 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
805 h
->txbuf
[1] = STLINK_DEBUG_APIV1_READREG
;
807 h
->txbuf
[1] = STLINK_DEBUG_APIV2_READREG
;
810 res
= stlink_usb_recv(handle
, h
->txbuf
, STLINK_CMD_SIZE
, h
->rxbuf
, 4);
815 *val
= le_to_h_u32(h
->rxbuf
);
821 static int stlink_usb_write_reg(void *handle
, int num
, uint32_t val
)
824 struct stlink_usb_handle_s
*h
;
826 assert(handle
!= NULL
);
828 h
= (struct stlink_usb_handle_s
*)handle
;
830 stlink_usb_init_buffer(handle
);
832 h
->txbuf
[0] = STLINK_DEBUG_COMMAND
;
833 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
834 h
->txbuf
[1] = STLINK_DEBUG_APIV1_WRITEREG
;
836 h
->txbuf
[1] = STLINK_DEBUG_APIV2_WRITEREG
;
838 h_u32_to_le(h
->txbuf
+ 3, val
);
840 res
= stlink_usb_recv(handle
, h
->txbuf
, STLINK_CMD_SIZE
, h
->rxbuf
, 2);
849 static int stlink_usb_read_mem8(void *handle
, uint32_t addr
, uint16_t len
,
853 uint16_t read_len
= len
;
854 struct stlink_usb_handle_s
*h
;
856 assert(handle
!= NULL
);
858 h
= (struct stlink_usb_handle_s
*)handle
;
860 stlink_usb_init_buffer(handle
);
862 h
->txbuf
[0] = STLINK_DEBUG_COMMAND
;
863 h
->txbuf
[1] = STLINK_DEBUG_READMEM_8BIT
;
864 h_u32_to_le(h
->txbuf
+ 2, addr
);
865 h_u16_to_le(h
->txbuf
+ 2 + 4, len
);
867 /* we need to fix read length for single bytes */
871 res
= stlink_usb_recv(handle
, h
->txbuf
, STLINK_CMD_SIZE
, h
->rxbuf
, read_len
);
876 memcpy(buffer
, h
->rxbuf
, len
);
882 static int stlink_usb_write_mem8(void *handle
, uint32_t addr
, uint16_t len
,
883 const uint8_t *buffer
)
886 struct stlink_usb_handle_s
*h
;
888 assert(handle
!= NULL
);
890 h
= (struct stlink_usb_handle_s
*)handle
;
892 stlink_usb_init_buffer(handle
);
894 h
->txbuf
[0] = STLINK_DEBUG_COMMAND
;
895 h
->txbuf
[1] = STLINK_DEBUG_WRITEMEM_8BIT
;
896 h_u32_to_le(h
->txbuf
+ 2, addr
);
897 h_u16_to_le(h
->txbuf
+ 2 + 4, len
);
899 res
= stlink_usb_recv(handle
, h
->txbuf
, STLINK_CMD_SIZE
, 0, 0);
904 res
= stlink_usb_recv(handle
, (uint8_t *) buffer
, len
, 0, 0);
913 static int stlink_usb_read_mem32(void *handle
, uint32_t addr
, uint16_t len
,
917 struct stlink_usb_handle_s
*h
;
919 assert(handle
!= NULL
);
921 h
= (struct stlink_usb_handle_s
*)handle
;
923 stlink_usb_init_buffer(handle
);
927 h
->txbuf
[0] = STLINK_DEBUG_COMMAND
;
928 h
->txbuf
[1] = STLINK_DEBUG_READMEM_32BIT
;
929 h_u32_to_le(h
->txbuf
+ 2, addr
);
930 h_u16_to_le(h
->txbuf
+ 2 + 4, len
);
932 res
= stlink_usb_recv(handle
, h
->txbuf
, STLINK_CMD_SIZE
, h
->rxbuf
, len
);
937 memcpy(buffer
, h
->rxbuf
, len
);
943 static int stlink_usb_write_mem32(void *handle
, uint32_t addr
, uint16_t len
,
944 const uint32_t *buffer
)
947 struct stlink_usb_handle_s
*h
;
949 assert(handle
!= NULL
);
951 h
= (struct stlink_usb_handle_s
*)handle
;
953 stlink_usb_init_buffer(handle
);
957 h
->txbuf
[0] = STLINK_DEBUG_COMMAND
;
958 h
->txbuf
[1] = STLINK_DEBUG_WRITEMEM_32BIT
;
959 h_u32_to_le(h
->txbuf
+ 2, addr
);
960 h_u16_to_le(h
->txbuf
+ 2 + 4, len
);
962 res
= stlink_usb_recv(handle
, h
->txbuf
, STLINK_CMD_SIZE
, 0, 0);
967 res
= stlink_usb_recv(handle
, (uint8_t *) buffer
, len
, 0, 0);
976 static int stlink_usb_open(struct stlink_interface_param_s
*param
, void **fd
)
979 struct stlink_usb_handle_s
*h
;
981 LOG_DEBUG("stlink_usb_open");
983 h
= malloc(sizeof(struct stlink_usb_handle_s
));
986 LOG_DEBUG("malloc failed");
990 h
->transport
= param
->transport
;
992 const uint16_t vids
[] = { param
->vid
, 0 };
993 const uint16_t pids
[] = { param
->pid
, 0 };
995 LOG_DEBUG("transport: %d vid: %04x pid: %04x", param
->transport
,
996 param
->vid
, param
->pid
);
998 if (jtag_libusb_open(vids
, pids
, &h
->fd
) != ERROR_OK
) {
999 LOG_ERROR("open failed");
1003 jtag_libusb_set_configuration(h
->fd
, 0);
1005 if (jtag_libusb_claim_interface(h
->fd
, 0) != ERROR_OK
) {
1006 LOG_DEBUG("claim interface failed");
1010 /* wrap version for first read */
1011 switch (param
->pid
) {
1013 h
->version
.stlink
= 1;
1016 h
->version
.stlink
= 2;
1020 /* get the device version */
1021 err
= stlink_usb_version(h
);
1023 if (err
!= ERROR_OK
) {
1024 LOG_ERROR("read version failed");
1025 jtag_libusb_close(h
->fd
);
1030 /* compare usb vid/pid */
1031 if ((param
->vid
!= h
->vid
) || (param
->pid
!= h
->pid
))
1032 LOG_INFO("vid/pid are not identical: %04X/%04X %04X/%04X",
1033 param
->vid
, param
->pid
,
1036 /* check if mode is supported */
1039 switch (h
->transport
) {
1040 case STLINK_TRANSPORT_SWD
:
1041 case STLINK_TRANSPORT_JTAG
:
1042 if (h
->version
.jtag
== 0)
1045 case STLINK_TRANSPORT_SWIM
:
1046 if (h
->version
.swim
== 0)
1054 if (err
!= ERROR_OK
) {
1055 LOG_ERROR("mode (transport) not supported by device");
1056 jtag_libusb_close(h
->fd
);
1061 /* set the used jtag api */
1062 h
->jtag_api
= STLINK_JTAG_API_V1
;
1064 /* initialize the debug hardware */
1065 err
= stlink_usb_init_mode(h
);
1067 if (err
!= ERROR_OK
) {
1068 LOG_ERROR("init mode failed");
1069 jtag_libusb_close(h
->fd
);
1080 static int stlink_usb_close(void *fd
)
1086 struct stlink_layout_api_s stlink_usb_layout_api
= {
1088 .open
= stlink_usb_open
,
1090 .close
= stlink_usb_close
,
1092 .idcode
= stlink_usb_idcode
,
1094 .state
= stlink_usb_state
,
1096 .reset
= stlink_usb_reset
,
1098 .run
= stlink_usb_run
,
1100 .halt
= stlink_usb_halt
,
1102 .step
= stlink_usb_step
,
1104 .read_regs
= stlink_usb_read_regs
,
1106 .read_reg
= stlink_usb_read_reg
,
1108 .write_reg
= stlink_usb_write_reg
,
1110 .read_mem8
= stlink_usb_read_mem8
,
1112 .write_mem8
= stlink_usb_write_mem8
,
1114 .read_mem32
= stlink_usb_read_mem32
,
1116 .write_mem32
= stlink_usb_write_mem32
,