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_NULL_EP 0
41 #define STLINK_RX_EP (1|ENDPOINT_IN)
42 #define STLINK_TX_EP (2|ENDPOINT_OUT)
43 #define STLINK_SG_SIZE (31)
44 #define STLINK_DATA_SIZE (4*128)
45 #define STLINK_CMD_SIZE_V2 (16)
46 #define STLINK_CMD_SIZE_V1 (10)
48 enum stlink_jtag_api_version {
49 STLINK_JTAG_API_V1 = 0,
54 struct stlink_usb_version {
61 /** highest supported jtag api version */
62 enum stlink_jtag_api_version jtag_api_max;
66 struct stlink_usb_handle_s {
68 struct jtag_libusb_device_handle *fd;
70 struct libusb_transfer *trans;
72 uint8_t cmdbuf[STLINK_SG_SIZE];
78 uint8_t databuf[STLINK_DATA_SIZE];
80 enum stlink_transports transport;
82 struct stlink_usb_version version;
87 /** this is the currently used jtag api */
88 enum stlink_jtag_api_version jtag_api;
91 #define STLINK_DEBUG_ERR_OK 0x80
92 #define STLINK_DEBUG_ERR_FAULT 0x81
93 #define STLINK_CORE_RUNNING 0x80
94 #define STLINK_CORE_HALTED 0x81
95 #define STLINK_CORE_STAT_UNKNOWN -1
97 #define STLINK_GET_VERSION 0xF1
98 #define STLINK_DEBUG_COMMAND 0xF2
99 #define STLINK_DFU_COMMAND 0xF3
100 #define STLINK_SWIM_COMMAND 0xF4
101 #define STLINK_GET_CURRENT_MODE 0xF5
103 #define STLINK_DEV_DFU_MODE 0x00
104 #define STLINK_DEV_MASS_MODE 0x01
105 #define STLINK_DEV_DEBUG_MODE 0x02
106 #define STLINK_DEV_SWIM_MODE 0x03
107 #define STLINK_DEV_BOOTLOADER_MODE 0x04
108 #define STLINK_DEV_UNKNOWN_MODE -1
110 #define STLINK_DFU_EXIT 0x07
112 #define STLINK_SWIM_ENTER 0x00
113 #define STLINK_SWIM_EXIT 0x01
115 #define STLINK_DEBUG_ENTER_JTAG 0x00
116 #define STLINK_DEBUG_GETSTATUS 0x01
117 #define STLINK_DEBUG_FORCEDEBUG 0x02
118 #define STLINK_DEBUG_APIV1_RESETSYS 0x03
119 #define STLINK_DEBUG_APIV1_READALLREGS 0x04
120 #define STLINK_DEBUG_APIV1_READREG 0x05
121 #define STLINK_DEBUG_APIV1_WRITEREG 0x06
122 #define STLINK_DEBUG_READMEM_32BIT 0x07
123 #define STLINK_DEBUG_WRITEMEM_32BIT 0x08
124 #define STLINK_DEBUG_RUNCORE 0x09
125 #define STLINK_DEBUG_STEPCORE 0x0a
126 #define STLINK_DEBUG_APIV1_SETFP 0x0b
127 #define STLINK_DEBUG_READMEM_8BIT 0x0c
128 #define STLINK_DEBUG_WRITEMEM_8BIT 0x0d
129 #define STLINK_DEBUG_APIV1_CLEARFP 0x0e
130 #define STLINK_DEBUG_APIV1_WRITEDEBUGREG 0x0f
131 #define STLINK_DEBUG_APIV1_SETWATCHPOINT 0x10
133 #define STLINK_DEBUG_ENTER_JTAG 0x00
134 #define STLINK_DEBUG_ENTER_SWD 0xa3
136 #define STLINK_DEBUG_APIV1_ENTER 0x20
137 #define STLINK_DEBUG_EXIT 0x21
138 #define STLINK_DEBUG_READCOREID 0x22
140 #define STLINK_DEBUG_APIV2_ENTER 0x30
141 #define STLINK_DEBUG_APIV2_READ_IDCODES 0x31
142 #define STLINK_DEBUG_APIV2_RESETSYS 0x32
143 #define STLINK_DEBUG_APIV2_READREG 0x33
144 #define STLINK_DEBUG_APIV2_WRITEREG 0x34
146 #define STLINK_DEBUG_APIV2_READALLREGS 0x3A
148 #define STLINK_DEBUG_APIV2_DRIVE_NRST 0x3C
150 #define STLINK_DEBUG_APIV2_DRIVE_NRST_LOW 0x00
151 #define STLINK_DEBUG_APIV2_DRIVE_NRST_HIGH 0x01
152 #define STLINK_DEBUG_APIV2_DRIVE_NRST_PULSE 0x02
156 STLINK_MODE_UNKNOWN = 0,
159 STLINK_MODE_DEBUG_JTAG,
160 STLINK_MODE_DEBUG_SWD,
161 STLINK_MODE_DEBUG_SWIM
164 #define REQUEST_SENSE 0x03
165 #define REQUEST_SENSE_LENGTH 18
167 static void stlink_usb_init_buffer(void *handle, uint8_t direction, uint32_t size);
170 static int stlink_usb_xfer_v1_get_status(void *handle)
172 struct stlink_usb_handle_s *h;
174 assert(handle != NULL);
176 h = (struct stlink_usb_handle_s *)handle;
179 memset(h->cmdbuf, 0, STLINK_SG_SIZE);
181 if (jtag_libusb_bulk_read(h->fd, STLINK_RX_EP, (char *)h->cmdbuf,
187 t1 = buf_get_u32(h->cmdbuf, 0, 32);
190 if (t1 != 0x53425355)
198 if (h->cmdbuf[12] != 0)
205 static int stlink_usb_xfer_rw(void *handle, int cmdsize, const uint8_t *buf, int size)
207 struct stlink_usb_handle_s *h;
209 assert(handle != NULL);
211 h = (struct stlink_usb_handle_s *)handle;
213 if (jtag_libusb_bulk_write(h->fd, STLINK_TX_EP, (char *)h->cmdbuf, cmdsize,
218 if (h->direction == STLINK_TX_EP && size) {
219 if (jtag_libusb_bulk_write(h->fd, STLINK_TX_EP, (char *)buf,
220 size, 1000) != size) {
221 LOG_DEBUG("bulk write failed");
224 } else if (h->direction == STLINK_RX_EP && size) {
225 if (jtag_libusb_bulk_read(h->fd, STLINK_RX_EP, (char *)buf,
226 size, 1000) != size) {
227 LOG_DEBUG("bulk read failed");
236 static int stlink_usb_xfer_v1_get_sense(void *handle)
239 struct stlink_usb_handle_s *h;
241 assert(handle != NULL);
243 h = (struct stlink_usb_handle_s *)handle;
245 stlink_usb_init_buffer(handle, STLINK_RX_EP, 16);
247 h->cmdbuf[h->cmdidx++] = REQUEST_SENSE;
248 h->cmdbuf[h->cmdidx++] = 0;
249 h->cmdbuf[h->cmdidx++] = 0;
250 h->cmdbuf[h->cmdidx++] = 0;
251 h->cmdbuf[h->cmdidx++] = REQUEST_SENSE_LENGTH;
253 res = stlink_usb_xfer_rw(handle, REQUEST_SENSE_LENGTH, h->databuf, 16);
258 if (stlink_usb_xfer_v1_get_status(handle) != ERROR_OK)
265 static int stlink_usb_xfer(void *handle, const uint8_t *buf, int size)
267 int err, cmdsize = STLINK_CMD_SIZE_V2;
268 struct stlink_usb_handle_s *h;
270 assert(handle != NULL);
272 h = (struct stlink_usb_handle_s *)handle;
274 if (h->version.stlink == 1)
275 cmdsize = STLINK_SG_SIZE;
277 err = stlink_usb_xfer_rw(handle, cmdsize, buf, size);
282 if (h->version.stlink == 1) {
283 if (stlink_usb_xfer_v1_get_status(handle) != ERROR_OK) {
284 /* check csw status */
285 if (h->cmdbuf[12] == 1) {
286 LOG_DEBUG("get sense");
287 if (stlink_usb_xfer_v1_get_sense(handle) != ERROR_OK)
298 static void stlink_usb_xfer_v1_create_cmd(void *handle, uint8_t direction, uint32_t size)
300 struct stlink_usb_handle_s *h;
302 h = (struct stlink_usb_handle_s *)handle;
304 /* fill the send buffer */
305 strcpy((char *)h->cmdbuf, "USBC");
307 /* csw tag not used */
309 buf_set_u32(h->cmdbuf+h->cmdidx, 0, 32, size);
311 h->cmdbuf[h->cmdidx++] = (direction == STLINK_RX_EP ? ENDPOINT_IN : ENDPOINT_OUT);
312 h->cmdbuf[h->cmdidx++] = 0; /* lun */
313 h->cmdbuf[h->cmdidx++] = STLINK_CMD_SIZE_V1;
317 static void stlink_usb_init_buffer(void *handle, uint8_t direction, uint32_t size)
319 struct stlink_usb_handle_s *h;
321 h = (struct stlink_usb_handle_s *)handle;
323 h->direction = direction;
327 memset(h->cmdbuf, 0, STLINK_SG_SIZE);
328 memset(h->databuf, 0, STLINK_DATA_SIZE);
330 if (h->version.stlink == 1)
331 stlink_usb_xfer_v1_create_cmd(handle, direction, size);
334 static const char * const stlink_usb_error_msg[] = {
339 static int stlink_usb_error_check(void *handle)
342 const char *err_msg = 0;
343 struct stlink_usb_handle_s *h;
345 assert(handle != NULL);
347 h = (struct stlink_usb_handle_s *)handle;
349 /* TODO: no error checking yet on api V1 */
350 if (h->jtag_api == STLINK_JTAG_API_V1)
351 h->databuf[0] = STLINK_DEBUG_ERR_OK;
353 switch (h->databuf[0]) {
354 case STLINK_DEBUG_ERR_OK:
357 case STLINK_DEBUG_ERR_FAULT:
359 err_msg = stlink_usb_error_msg[0];
365 LOG_DEBUG("status error: %d ('%s')", h->databuf[0], err_msg);
371 static int stlink_usb_version(void *handle)
375 struct stlink_usb_handle_s *h;
377 assert(handle != NULL);
379 h = (struct stlink_usb_handle_s *)handle;
381 stlink_usb_init_buffer(handle, STLINK_RX_EP, 6);
383 h->cmdbuf[h->cmdidx++] = STLINK_GET_VERSION;
385 res = stlink_usb_xfer(handle, h->databuf, 6);
390 v = (h->databuf[0] << 8) | h->databuf[1];
392 h->version.stlink = (v >> 12) & 0x0f;
393 h->version.jtag = (v >> 6) & 0x3f;
394 h->version.swim = v & 0x3f;
395 h->vid = buf_get_u32(h->databuf, 16, 16);
396 h->pid = buf_get_u32(h->databuf, 32, 16);
398 /* set the supported jtag api version
399 * V1 doesn't support API V2 at all
400 * V2 support API V2 since JTAG V13
402 if ((h->version.stlink == 2) && (h->version.jtag > 12))
403 h->version.jtag_api_max = STLINK_JTAG_API_V2;
405 h->version.jtag_api_max = STLINK_JTAG_API_V1;
407 LOG_DEBUG("STLINK v%d JTAG v%d API v%d SWIM v%d VID %04X PID %04X",
410 (h->version.jtag_api_max == STLINK_JTAG_API_V1) ? 1 : 2,
419 static int stlink_usb_current_mode(void *handle, uint8_t *mode)
422 struct stlink_usb_handle_s *h;
424 assert(handle != NULL);
426 h = (struct stlink_usb_handle_s *)handle;
428 stlink_usb_init_buffer(handle, STLINK_RX_EP, 2);
430 h->cmdbuf[h->cmdidx++] = STLINK_GET_CURRENT_MODE;
432 res = stlink_usb_xfer(handle, h->databuf, 2);
437 *mode = h->databuf[0];
443 static int stlink_usb_mode_enter(void *handle, enum stlink_mode type)
447 struct stlink_usb_handle_s *h;
449 assert(handle != NULL);
451 h = (struct stlink_usb_handle_s *)handle;
453 /* on api V2 we are able the read the latest command
455 * TODO: we need the test on api V1 too
457 if (h->jtag_api == STLINK_JTAG_API_V2)
460 stlink_usb_init_buffer(handle, STLINK_RX_EP, rx_size);
463 case STLINK_MODE_DEBUG_JTAG:
464 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
465 if (h->jtag_api == STLINK_JTAG_API_V1)
466 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_ENTER;
468 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_ENTER;
469 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_ENTER_JTAG;
471 case STLINK_MODE_DEBUG_SWD:
472 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
473 if (h->jtag_api == STLINK_JTAG_API_V1)
474 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_ENTER;
476 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_ENTER;
477 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_ENTER_SWD;
479 case STLINK_MODE_DEBUG_SWIM:
480 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_COMMAND;
481 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_ENTER;
483 case STLINK_MODE_DFU:
484 case STLINK_MODE_MASS:
489 res = stlink_usb_xfer(handle, h->databuf, rx_size);
494 res = stlink_usb_error_check(h);
500 static int stlink_usb_mode_leave(void *handle, enum stlink_mode type)
503 struct stlink_usb_handle_s *h;
505 assert(handle != NULL);
507 h = (struct stlink_usb_handle_s *)handle;
509 stlink_usb_init_buffer(handle, STLINK_NULL_EP, 0);
512 case STLINK_MODE_DEBUG_JTAG:
513 case STLINK_MODE_DEBUG_SWD:
514 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
515 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_EXIT;
517 case STLINK_MODE_DEBUG_SWIM:
518 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_COMMAND;
519 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_EXIT;
521 case STLINK_MODE_DFU:
522 h->cmdbuf[h->cmdidx++] = STLINK_DFU_COMMAND;
523 h->cmdbuf[h->cmdidx++] = STLINK_DFU_EXIT;
525 case STLINK_MODE_MASS:
530 res = stlink_usb_xfer(handle, 0, 0);
539 static int stlink_usb_init_mode(void *handle)
543 enum stlink_mode emode;
544 struct stlink_usb_handle_s *h;
546 assert(handle != NULL);
548 h = (struct stlink_usb_handle_s *)handle;
550 res = stlink_usb_current_mode(handle, &mode);
555 LOG_DEBUG("MODE: %02X", mode);
557 /* try to exit current mode */
559 case STLINK_DEV_DFU_MODE:
560 emode = STLINK_MODE_DFU;
562 case STLINK_DEV_DEBUG_MODE:
563 emode = STLINK_MODE_DEBUG_SWD;
565 case STLINK_DEV_SWIM_MODE:
566 emode = STLINK_MODE_DEBUG_SWIM;
568 case STLINK_DEV_BOOTLOADER_MODE:
569 case STLINK_DEV_MASS_MODE:
571 emode = STLINK_MODE_UNKNOWN;
575 if (emode != STLINK_MODE_UNKNOWN) {
576 res = stlink_usb_mode_leave(handle, emode);
582 res = stlink_usb_current_mode(handle, &mode);
587 LOG_DEBUG("MODE: %02X", mode);
589 /* set selected mode */
590 switch (h->transport) {
591 case STLINK_TRANSPORT_SWD:
592 emode = STLINK_MODE_DEBUG_SWD;
594 case STLINK_TRANSPORT_JTAG:
595 emode = STLINK_MODE_DEBUG_JTAG;
597 case STLINK_TRANSPORT_SWIM:
598 emode = STLINK_MODE_DEBUG_SWIM;
601 emode = STLINK_MODE_UNKNOWN;
605 if (emode == STLINK_MODE_UNKNOWN) {
606 LOG_ERROR("selected mode (transport) not supported");
610 res = stlink_usb_mode_enter(handle, emode);
615 res = stlink_usb_current_mode(handle, &mode);
620 LOG_DEBUG("MODE: %02X", mode);
626 static int stlink_usb_idcode(void *handle, uint32_t *idcode)
629 struct stlink_usb_handle_s *h;
631 assert(handle != NULL);
633 h = (struct stlink_usb_handle_s *)handle;
635 stlink_usb_init_buffer(handle, STLINK_RX_EP, 4);
637 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
638 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_READCOREID;
640 res = stlink_usb_xfer(handle, h->databuf, 4);
645 *idcode = le_to_h_u32(h->databuf);
647 LOG_DEBUG("IDCODE: %08X", *idcode);
653 static enum target_state stlink_usb_state(void *handle)
656 struct stlink_usb_handle_s *h;
658 assert(handle != NULL);
660 h = (struct stlink_usb_handle_s *)handle;
662 if (h->jtag_api == STLINK_JTAG_API_V2)
663 return TARGET_UNKNOWN;
665 stlink_usb_init_buffer(handle, STLINK_RX_EP, 2);
667 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
668 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_GETSTATUS;
670 res = stlink_usb_xfer(handle, h->databuf, 2);
673 return TARGET_UNKNOWN;
675 if (h->databuf[0] == STLINK_CORE_RUNNING)
676 return TARGET_RUNNING;
677 if (h->databuf[0] == STLINK_CORE_HALTED)
678 return TARGET_HALTED;
680 return TARGET_UNKNOWN;
684 static int stlink_usb_reset(void *handle)
687 struct stlink_usb_handle_s *h;
689 assert(handle != NULL);
691 h = (struct stlink_usb_handle_s *)handle;
693 stlink_usb_init_buffer(handle, STLINK_RX_EP, 2);
695 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
697 if (h->jtag_api == STLINK_JTAG_API_V1)
698 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_RESETSYS;
700 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_RESETSYS;
702 res = stlink_usb_xfer(handle, h->databuf, 2);
707 LOG_DEBUG("RESET: %08X", h->databuf[0]);
713 static int stlink_usb_run(void *handle)
716 struct stlink_usb_handle_s *h;
718 assert(handle != NULL);
720 h = (struct stlink_usb_handle_s *)handle;
722 if (h->jtag_api == STLINK_JTAG_API_V2)
725 stlink_usb_init_buffer(handle, STLINK_RX_EP, 2);
727 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
728 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_RUNCORE;
730 res = stlink_usb_xfer(handle, h->databuf, 2);
739 static int stlink_usb_halt(void *handle)
742 struct stlink_usb_handle_s *h;
744 assert(handle != NULL);
746 h = (struct stlink_usb_handle_s *)handle;
748 if (h->jtag_api == STLINK_JTAG_API_V2)
751 stlink_usb_init_buffer(handle, STLINK_RX_EP, 2);
753 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
754 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_FORCEDEBUG;
756 res = stlink_usb_xfer(handle, h->databuf, 2);
765 static int stlink_usb_step(void *handle)
768 struct stlink_usb_handle_s *h;
770 assert(handle != NULL);
772 h = (struct stlink_usb_handle_s *)handle;
774 if (h->jtag_api == STLINK_JTAG_API_V2)
777 stlink_usb_init_buffer(handle, STLINK_RX_EP, 2);
779 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
780 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_STEPCORE;
782 res = stlink_usb_xfer(handle, h->databuf, 2);
791 static int stlink_usb_read_regs(void *handle)
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, STLINK_RX_EP, 84);
802 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
803 if (h->jtag_api == STLINK_JTAG_API_V1)
804 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_READALLREGS;
806 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_READALLREGS;
808 res = stlink_usb_xfer(handle, h->databuf, 84);
817 static int stlink_usb_read_reg(void *handle, int num, uint32_t *val)
820 struct stlink_usb_handle_s *h;
822 assert(handle != NULL);
824 h = (struct stlink_usb_handle_s *)handle;
826 stlink_usb_init_buffer(handle, STLINK_RX_EP, 4);
828 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
829 if (h->jtag_api == STLINK_JTAG_API_V1)
830 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_READREG;
832 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_READREG;
833 h->cmdbuf[h->cmdidx++] = num;
835 res = stlink_usb_xfer(handle, h->databuf, 4);
840 *val = le_to_h_u32(h->databuf);
846 static int stlink_usb_write_reg(void *handle, int num, uint32_t val)
849 struct stlink_usb_handle_s *h;
851 assert(handle != NULL);
853 h = (struct stlink_usb_handle_s *)handle;
855 stlink_usb_init_buffer(handle, STLINK_RX_EP, 2);
857 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
858 if (h->jtag_api == STLINK_JTAG_API_V1)
859 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_WRITEREG;
861 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_WRITEREG;
862 h->cmdbuf[h->cmdidx++] = num;
863 h_u32_to_le(h->cmdbuf+h->cmdidx, val);
866 res = stlink_usb_xfer(handle, h->databuf, 2);
875 static int stlink_usb_read_mem8(void *handle, uint32_t addr, uint16_t len,
879 uint16_t read_len = len;
880 struct stlink_usb_handle_s *h;
882 assert(handle != NULL);
884 h = (struct stlink_usb_handle_s *)handle;
886 stlink_usb_init_buffer(handle, STLINK_RX_EP, read_len);
888 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
889 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_READMEM_8BIT;
890 h_u32_to_le(h->cmdbuf+h->cmdidx, addr);
892 h_u16_to_le(h->cmdbuf+h->cmdidx, len);
895 /* we need to fix read length for single bytes */
899 res = stlink_usb_xfer(handle, h->databuf, read_len);
904 memcpy(buffer, h->databuf, len);
910 static int stlink_usb_write_mem8(void *handle, uint32_t addr, uint16_t len,
911 const uint8_t *buffer)
914 struct stlink_usb_handle_s *h;
916 assert(handle != NULL);
918 h = (struct stlink_usb_handle_s *)handle;
920 stlink_usb_init_buffer(handle, STLINK_TX_EP, len);
922 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
923 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_WRITEMEM_8BIT;
924 h_u32_to_le(h->cmdbuf+h->cmdidx, addr);
926 h_u16_to_le(h->cmdbuf+h->cmdidx, len);
929 res = stlink_usb_xfer(handle, buffer, len);
938 static int stlink_usb_read_mem32(void *handle, uint32_t addr, uint16_t len,
942 struct stlink_usb_handle_s *h;
944 assert(handle != NULL);
946 h = (struct stlink_usb_handle_s *)handle;
950 stlink_usb_init_buffer(handle, STLINK_RX_EP, len);
952 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
953 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_READMEM_32BIT;
954 h_u32_to_le(h->cmdbuf+h->cmdidx, addr);
956 h_u16_to_le(h->cmdbuf+h->cmdidx, len);
959 res = stlink_usb_xfer(handle, h->databuf, len);
964 memcpy(buffer, h->databuf, len);
970 static int stlink_usb_write_mem32(void *handle, uint32_t addr, uint16_t len,
971 const uint8_t *buffer)
974 struct stlink_usb_handle_s *h;
976 assert(handle != NULL);
978 h = (struct stlink_usb_handle_s *)handle;
982 stlink_usb_init_buffer(handle, STLINK_TX_EP, len);
984 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
985 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_WRITEMEM_32BIT;
986 h_u32_to_le(h->cmdbuf+h->cmdidx, addr);
988 h_u16_to_le(h->cmdbuf+h->cmdidx, len);
991 res = stlink_usb_xfer(handle, buffer, len);
1000 static int stlink_usb_open(struct stlink_interface_param_s *param, void **fd)
1003 struct stlink_usb_handle_s *h;
1005 LOG_DEBUG("stlink_usb_open");
1007 h = malloc(sizeof(struct stlink_usb_handle_s));
1010 LOG_DEBUG("malloc failed");
1014 h->transport = param->transport;
1016 const uint16_t vids[] = { param->vid, 0 };
1017 const uint16_t pids[] = { param->pid, 0 };
1019 LOG_DEBUG("transport: %d vid: %04x pid: %04x", param->transport,
1020 param->vid, param->pid);
1022 if (jtag_libusb_open(vids, pids, &h->fd) != ERROR_OK) {
1023 LOG_ERROR("open failed");
1027 jtag_libusb_set_configuration(h->fd, 0);
1029 if (jtag_libusb_claim_interface(h->fd, 0) != ERROR_OK) {
1030 LOG_DEBUG("claim interface failed");
1034 /* wrap version for first read */
1035 switch (param->pid) {
1037 h->version.stlink = 1;
1040 h->version.stlink = 2;
1044 /* get the device version */
1045 err = stlink_usb_version(h);
1047 if (err != ERROR_OK) {
1048 LOG_ERROR("read version failed");
1049 jtag_libusb_close(h->fd);
1054 /* compare usb vid/pid */
1055 if ((param->vid != h->vid) || (param->pid != h->pid))
1056 LOG_INFO("vid/pid are not identical: %04X/%04X %04X/%04X",
1057 param->vid, param->pid,
1060 /* check if mode is supported */
1063 switch (h->transport) {
1064 case STLINK_TRANSPORT_SWD:
1065 case STLINK_TRANSPORT_JTAG:
1066 if (h->version.jtag == 0)
1069 case STLINK_TRANSPORT_SWIM:
1070 if (h->version.swim == 0)
1078 if (err != ERROR_OK) {
1079 LOG_ERROR("mode (transport) not supported by device");
1080 jtag_libusb_close(h->fd);
1085 /* set the used jtag api */
1086 h->jtag_api = STLINK_JTAG_API_V1;
1088 /* initialize the debug hardware */
1089 err = stlink_usb_init_mode(h);
1091 if (err != ERROR_OK) {
1092 LOG_ERROR("init mode failed");
1093 jtag_libusb_close(h->fd);
1104 static int stlink_usb_close(void *fd)
1110 struct stlink_layout_api_s stlink_usb_layout_api = {
1112 .open = stlink_usb_open,
1114 .close = stlink_usb_close,
1116 .idcode = stlink_usb_idcode,
1118 .state = stlink_usb_state,
1120 .reset = stlink_usb_reset,
1122 .run = stlink_usb_run,
1124 .halt = stlink_usb_halt,
1126 .step = stlink_usb_step,
1128 .read_regs = stlink_usb_read_regs,
1130 .read_reg = stlink_usb_read_reg,
1132 .write_reg = stlink_usb_write_reg,
1134 .read_mem8 = stlink_usb_read_mem8,
1136 .write_mem8 = stlink_usb_write_mem8,
1138 .read_mem32 = stlink_usb_read_mem32,
1140 .write_mem32 = stlink_usb_write_mem32,