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)
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_UNKNOWN_MODE -1
105 #define STLINK_DFU_EXIT 0x07
107 #define STLINK_SWIM_ENTER 0x00
108 #define STLINK_SWIM_EXIT 0x01
110 #define STLINK_DEBUG_ENTER_JTAG 0x00
111 #define STLINK_DEBUG_GETSTATUS 0x01
112 #define STLINK_DEBUG_FORCEDEBUG 0x02
113 #define STLINK_DEBUG_APIV1_RESETSYS 0x03
114 #define STLINK_DEBUG_APIV1_READALLREGS 0x04
115 #define STLINK_DEBUG_APIV1_READREG 0x05
116 #define STLINK_DEBUG_APIV1_WRITEREG 0x06
117 #define STLINK_DEBUG_READMEM_32BIT 0x07
118 #define STLINK_DEBUG_WRITEMEM_32BIT 0x08
119 #define STLINK_DEBUG_RUNCORE 0x09
120 #define STLINK_DEBUG_STEPCORE 0x0a
121 #define STLINK_DEBUG_APIV1_SETFP 0x0b
122 #define STLINK_DEBUG_READMEM_8BIT 0x0c
123 #define STLINK_DEBUG_WRITEMEM_8BIT 0x0d
124 #define STLINK_DEBUG_APIV1_CLEARFP 0x0e
125 #define STLINK_DEBUG_APIV1_WRITEDEBUGREG 0x0f
126 #define STLINK_DEBUG_APIV1_SETWATCHPOINT 0x10
128 #define STLINK_DEBUG_ENTER_JTAG 0x00
129 #define STLINK_DEBUG_ENTER_SWD 0xa3
131 #define STLINK_DEBUG_APIV1_ENTER 0x20
132 #define STLINK_DEBUG_EXIT 0x21
133 #define STLINK_DEBUG_READCOREID 0x22
135 #define STLINK_DEBUG_APIV2_ENTER 0x30
137 #define STLINK_DEBUG_APIV2_RESETSYS 0x32
138 #define STLINK_DEBUG_APIV2_READREG 0x33
139 #define STLINK_DEBUG_APIV2_WRITEREG 0x34
141 #define STLINK_DEBUG_APIV2_READALLREGS 0x3A
144 STLINK_MODE_UNKNOWN = 0,
147 STLINK_MODE_DEBUG_JTAG,
148 STLINK_MODE_DEBUG_SWD,
149 STLINK_MODE_DEBUG_SWIM
153 static void stlink_usb_recv_v1_create_cmd(char *b, int s, uint32_t tag, uint32_t rxsize,
154 uint8_t flag, uint8_t lun, uint8_t length)
160 /* fill the send buffer */
164 buf_set_u32(b+i, 0, 32, tag);
166 buf_set_u32(b+i, 0, 32, rxsize);
174 static int stlink_usb_recv_v1_mass_storage_cmd(void *handle, const uint8_t *txbuf, int txsize, uint8_t *rxbuf,
178 struct stlink_usb_handle_s *h;
180 assert(handle != NULL);
182 h = (struct stlink_usb_handle_s *)handle;
183 h->sg_tag = (h->sg_tag + 1) & 1;
185 stlink_usb_recv_v1_create_cmd(sg_buffer, 31, h->sg_tag, rxsize, STLINK_TX_EP, 0x00, txsize);
187 memcpy(sg_buffer+15, txbuf, 10);
189 if (jtag_libusb_bulk_write(h->fd, STLINK_TX_EP, (char *)sg_buffer, 31,
191 printf("send failed\n");
198 #define REQUEST_SENSE 0x03
199 #define REQUEST_SENSE_LENGTH 18
202 static int stlink_usb_recv_v1_get_status(void *handle, char *sg_buffer, int len)
204 struct stlink_usb_handle_s *h;
206 assert(handle != NULL);
208 h = (struct stlink_usb_handle_s *)handle;
211 memset(sg_buffer, 0x00, len);
213 if (jtag_libusb_bulk_read(h->fd, STLINK_RX_EP, (char *)sg_buffer,
219 t1 = buf_get_u32(sg_buffer+0, 0, 32);
220 t2 = buf_get_u32(sg_buffer+4, 0, 32);
223 if (t1 != 0x53425355)
230 static int stlink_usb_recv_v1_get_sense(void *handle)
232 struct stlink_usb_handle_s *h;
236 assert(handle != NULL);
238 h = (struct stlink_usb_handle_s *)handle;
239 h->sg_tag = (h->sg_tag + 1) & 1;
241 cdb[0] = REQUEST_SENSE;
242 cdb[4] = REQUEST_SENSE_LENGTH;
244 stlink_usb_recv_v1_create_cmd(sg_buffer, 31, h->sg_tag, REQUEST_SENSE_LENGTH, STLINK_TX_EP,
247 memcpy(sg_buffer+15, cdb, 16);
249 if (jtag_libusb_bulk_write(h->fd, STLINK_TX_EP, (char *)sg_buffer, 16,
253 if (jtag_libusb_bulk_read(h->fd, STLINK_RX_EP, (char *)cdb,
257 if (stlink_usb_recv_v1_get_status(handle, sg_buffer, 13) != ERROR_OK)
259 /* check for sense */
260 if (sg_buffer[12] != 0)
263 /* if (sense[0] != 0x70 && sense[0] != 0x71) */
269 static int stlink_usb_recv_v1(void *handle, const uint8_t *txbuf, int txsize, uint8_t *rxbuf,
274 struct stlink_usb_handle_s *h;
276 assert(handle != NULL);
278 h = (struct stlink_usb_handle_s *)handle;
280 err = stlink_usb_recv_v1_mass_storage_cmd(handle, txbuf, txsize, rxbuf, rxsize);
285 if (rxsize && rxbuf) {
286 if (jtag_libusb_bulk_read(h->fd, STLINK_RX_EP, (char *)rxbuf,
287 rxsize, 1000) != rxsize) {
288 LOG_DEBUG("jtag_libusb_bulk_read");
293 if (stlink_usb_recv_v1_get_status(handle, sg_buffer, 13) != ERROR_OK)
295 /* check for sense */
296 if (sg_buffer[12] == 1) {
297 LOG_DEBUG("get sense");
298 err = stlink_usb_recv_v1_get_sense(handle);
304 static int stlink_usb_recv_v2(void *handle, const uint8_t *txbuf, int txsize, uint8_t *rxbuf,
307 struct stlink_usb_handle_s *h;
309 assert(handle != NULL);
311 h = (struct stlink_usb_handle_s *)handle;
313 if (jtag_libusb_bulk_write(h->fd, STLINK_TX_EP, (char *)txbuf, txsize,
317 if (rxsize && rxbuf) {
318 if (jtag_libusb_bulk_read(h->fd, STLINK_RX_EP, (char *)rxbuf,
319 rxsize, 1000) != rxsize) {
327 static int stlink_usb_recv(void *handle, const uint8_t *txbuf, int txsize, uint8_t *rxbuf,
330 struct stlink_usb_handle_s *h;
332 assert(handle != NULL);
334 h = (struct stlink_usb_handle_s *)handle;
336 if (h->version.stlink == 1) {
337 return stlink_usb_recv_v1(handle, txbuf, txsize, rxbuf, rxsize);
339 if (txsize < STLINK_CMD_SIZE)
340 txsize = STLINK_CMD_SIZE;
341 return stlink_usb_recv_v2(handle, txbuf, txsize, rxbuf, rxsize);
346 static void stlink_usb_init_buffer(void *handle)
348 struct stlink_usb_handle_s *h;
350 assert(handle != NULL);
352 h = (struct stlink_usb_handle_s *)handle;
354 memset(h->txbuf, 0, STLINK_CMD_SIZE);
358 static int stlink_usb_version(void *handle)
362 struct stlink_usb_handle_s *h;
364 assert(handle != NULL);
366 h = (struct stlink_usb_handle_s *)handle;
368 stlink_usb_init_buffer(handle);
370 h->txbuf[0] = STLINK_GET_VERSION;
372 res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, 6);
377 v = (h->rxbuf[0] << 8) | h->rxbuf[1];
379 h->version.stlink = (v >> 12) & 0x0f;
380 h->version.jtag = (v >> 6) & 0x3f;
381 h->version.swim = v & 0x3f;
382 h->vid = buf_get_u32(h->rxbuf, 16, 16);
383 h->pid = buf_get_u32(h->rxbuf, 32, 16);
385 /* set the supported jtag api version
386 * V1 doesn't support API V2 at all
387 * V2 support API V2 since JTAG V13
389 if ((h->version.stlink == 2) && (h->version.jtag > 12))
390 h->version.jtag_api_max = STLINK_JTAG_API_V2;
392 h->version.jtag_api_max = STLINK_JTAG_API_V1;
394 LOG_DEBUG("STLINK v%d JTAG v%d API v%d SWIM v%d VID %04X PID %04X",
397 (h->version.jtag_api_max == STLINK_JTAG_API_V1) ? 1 : 2,
406 static int stlink_usb_current_mode(void *handle, uint8_t *mode)
409 struct stlink_usb_handle_s *h;
411 assert(handle != NULL);
413 h = (struct stlink_usb_handle_s *)handle;
415 stlink_usb_init_buffer(handle);
417 h->txbuf[0] = STLINK_GET_CURRENT_MODE;
419 res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, 2);
430 static int stlink_usb_mode_enter(void *handle, enum stlink_mode type)
433 struct stlink_usb_handle_s *h;
435 assert(handle != NULL);
437 h = (struct stlink_usb_handle_s *)handle;
439 stlink_usb_init_buffer(handle);
442 case STLINK_MODE_DEBUG_JTAG:
443 h->txbuf[0] = STLINK_DEBUG_COMMAND;
444 if (h->jtag_api == STLINK_JTAG_API_V1)
445 h->txbuf[1] = STLINK_DEBUG_APIV1_ENTER;
447 h->txbuf[1] = STLINK_DEBUG_APIV2_ENTER;
448 h->txbuf[2] = STLINK_DEBUG_ENTER_JTAG;
450 case STLINK_MODE_DEBUG_SWD:
451 h->txbuf[0] = STLINK_DEBUG_COMMAND;
452 if (h->jtag_api == STLINK_JTAG_API_V1)
453 h->txbuf[1] = STLINK_DEBUG_APIV1_ENTER;
455 h->txbuf[1] = STLINK_DEBUG_APIV2_ENTER;
456 h->txbuf[2] = STLINK_DEBUG_ENTER_SWD;
458 case STLINK_MODE_DEBUG_SWIM:
459 h->txbuf[0] = STLINK_SWIM_COMMAND;
460 h->txbuf[1] = STLINK_SWIM_ENTER;
462 case STLINK_MODE_DFU:
463 case STLINK_MODE_MASS:
468 res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, 0, 0);
476 static int stlink_usb_mode_leave(void *handle, enum stlink_mode type)
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);
488 case STLINK_MODE_DEBUG_JTAG:
489 case STLINK_MODE_DEBUG_SWD:
490 h->txbuf[0] = STLINK_DEBUG_COMMAND;
491 h->txbuf[1] = STLINK_DEBUG_EXIT;
493 case STLINK_MODE_DEBUG_SWIM:
494 h->txbuf[0] = STLINK_SWIM_COMMAND;
495 h->txbuf[1] = STLINK_SWIM_EXIT;
497 case STLINK_MODE_DFU:
498 h->txbuf[0] = STLINK_DFU_COMMAND;
499 h->txbuf[1] = STLINK_DFU_EXIT;
501 case STLINK_MODE_MASS:
506 res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, 0, 0);
514 static int stlink_usb_init_mode(void *handle)
518 enum stlink_mode emode;
519 struct stlink_usb_handle_s *h;
521 assert(handle != NULL);
523 h = (struct stlink_usb_handle_s *)handle;
525 res = stlink_usb_current_mode(handle, &mode);
530 LOG_DEBUG("MODE: %02X", mode);
532 /* try to exit current mode */
534 case STLINK_DEV_DFU_MODE:
535 emode = STLINK_MODE_DFU;
537 case STLINK_DEV_DEBUG_MODE:
538 emode = STLINK_MODE_DEBUG_SWD;
540 case STLINK_DEV_SWIM_MODE:
541 emode = STLINK_MODE_DEBUG_SWIM;
544 emode = STLINK_MODE_UNKNOWN;
548 if (emode != STLINK_MODE_UNKNOWN) {
549 res = stlink_usb_mode_leave(handle, emode);
555 res = stlink_usb_current_mode(handle, &mode);
560 LOG_DEBUG("MODE: %02X", mode);
562 /* set selected mode */
563 switch (h->transport) {
564 case STLINK_TRANSPORT_SWD:
565 emode = STLINK_MODE_DEBUG_SWD;
567 case STLINK_TRANSPORT_JTAG:
568 emode = STLINK_MODE_DEBUG_JTAG;
570 case STLINK_TRANSPORT_SWIM:
571 emode = STLINK_MODE_DEBUG_SWIM;
574 emode = STLINK_MODE_UNKNOWN;
578 if (emode == STLINK_MODE_UNKNOWN) {
579 LOG_ERROR("selected mode (transport) not supported");
583 res = stlink_usb_mode_enter(handle, emode);
588 res = stlink_usb_current_mode(handle, &mode);
593 LOG_DEBUG("MODE: %02X", mode);
599 static int stlink_usb_idcode(void *handle, uint32_t *idcode)
602 struct stlink_usb_handle_s *h;
604 assert(handle != NULL);
606 h = (struct stlink_usb_handle_s *)handle;
608 stlink_usb_init_buffer(handle);
610 h->txbuf[0] = STLINK_DEBUG_COMMAND;
611 h->txbuf[1] = STLINK_DEBUG_READCOREID;
613 res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, 4);
618 *idcode = le_to_h_u32(h->rxbuf);
620 LOG_DEBUG("IDCODE: %08X", *idcode);
626 static enum target_state stlink_usb_state(void *handle)
629 struct stlink_usb_handle_s *h;
631 assert(handle != NULL);
633 h = (struct stlink_usb_handle_s *)handle;
635 if (h->jtag_api == STLINK_JTAG_API_V2)
636 return TARGET_UNKNOWN;
638 stlink_usb_init_buffer(handle);
640 h->txbuf[0] = STLINK_DEBUG_COMMAND;
641 h->txbuf[1] = STLINK_DEBUG_GETSTATUS;
643 res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, 2);
646 return TARGET_UNKNOWN;
648 if (h->rxbuf[0] == STLINK_CORE_RUNNING)
649 return TARGET_RUNNING;
650 if (h->rxbuf[0] == STLINK_CORE_HALTED)
651 return TARGET_HALTED;
653 return TARGET_UNKNOWN;
657 static int stlink_usb_reset(void *handle)
660 struct stlink_usb_handle_s *h;
662 assert(handle != NULL);
664 h = (struct stlink_usb_handle_s *)handle;
666 stlink_usb_init_buffer(handle);
668 h->txbuf[0] = STLINK_DEBUG_COMMAND;
670 if (h->jtag_api == STLINK_JTAG_API_V1)
671 h->txbuf[1] = STLINK_DEBUG_APIV1_RESETSYS;
673 h->txbuf[1] = STLINK_DEBUG_APIV2_RESETSYS;
675 res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, 2);
680 LOG_DEBUG("RESET: %08X", h->rxbuf[0]);
686 static int stlink_usb_run(void *handle)
689 struct stlink_usb_handle_s *h;
691 assert(handle != NULL);
693 h = (struct stlink_usb_handle_s *)handle;
695 if (h->jtag_api == STLINK_JTAG_API_V2)
698 stlink_usb_init_buffer(handle);
700 h->txbuf[0] = STLINK_DEBUG_COMMAND;
701 h->txbuf[1] = STLINK_DEBUG_RUNCORE;
703 res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, 2);
712 static int stlink_usb_halt(void *handle)
715 struct stlink_usb_handle_s *h;
717 assert(handle != NULL);
719 h = (struct stlink_usb_handle_s *)handle;
721 if (h->jtag_api == STLINK_JTAG_API_V2)
724 stlink_usb_init_buffer(handle);
726 h->txbuf[0] = STLINK_DEBUG_COMMAND;
727 h->txbuf[1] = STLINK_DEBUG_FORCEDEBUG;
729 res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, 2);
738 static int stlink_usb_step(void *handle)
741 struct stlink_usb_handle_s *h;
743 assert(handle != NULL);
745 h = (struct stlink_usb_handle_s *)handle;
747 if (h->jtag_api == STLINK_JTAG_API_V2)
750 stlink_usb_init_buffer(handle);
752 h->txbuf[0] = STLINK_DEBUG_COMMAND;
753 h->txbuf[1] = STLINK_DEBUG_STEPCORE;
755 res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, 2);
764 static int stlink_usb_read_regs(void *handle)
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 if (h->jtag_api == STLINK_JTAG_API_V1)
777 h->txbuf[1] = STLINK_DEBUG_APIV1_READALLREGS;
779 h->txbuf[1] = STLINK_DEBUG_APIV2_READALLREGS;
782 res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, 84);
791 static int stlink_usb_read_reg(void *handle, int num, uint32_t *val)
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 if (h->jtag_api == STLINK_JTAG_API_V1)
804 h->txbuf[1] = STLINK_DEBUG_APIV1_READREG;
806 h->txbuf[1] = STLINK_DEBUG_APIV2_READREG;
809 res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, 4);
814 *val = le_to_h_u32(h->rxbuf);
820 static int stlink_usb_write_reg(void *handle, int num, uint32_t val)
823 struct stlink_usb_handle_s *h;
825 assert(handle != NULL);
827 h = (struct stlink_usb_handle_s *)handle;
829 stlink_usb_init_buffer(handle);
831 h->txbuf[0] = STLINK_DEBUG_COMMAND;
832 if (h->jtag_api == STLINK_JTAG_API_V1)
833 h->txbuf[1] = STLINK_DEBUG_APIV1_WRITEREG;
835 h->txbuf[1] = STLINK_DEBUG_APIV2_WRITEREG;
837 h_u32_to_le(h->txbuf + 3, val);
839 res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, 2);
848 static int stlink_usb_read_mem8(void *handle, uint32_t addr, uint16_t len,
852 uint16_t read_len = len;
853 struct stlink_usb_handle_s *h;
855 assert(handle != NULL);
857 h = (struct stlink_usb_handle_s *)handle;
859 stlink_usb_init_buffer(handle);
861 h->txbuf[0] = STLINK_DEBUG_COMMAND;
862 h->txbuf[1] = STLINK_DEBUG_READMEM_8BIT;
863 h_u32_to_le(h->txbuf + 2, addr);
864 h_u16_to_le(h->txbuf + 2 + 4, len);
866 /* we need to fix read length for single bytes */
870 res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, read_len);
875 memcpy(buffer, h->rxbuf, len);
881 static int stlink_usb_write_mem8(void *handle, uint32_t addr, uint16_t len,
882 const uint8_t *buffer)
885 struct stlink_usb_handle_s *h;
887 assert(handle != NULL);
889 h = (struct stlink_usb_handle_s *)handle;
891 stlink_usb_init_buffer(handle);
893 h->txbuf[0] = STLINK_DEBUG_COMMAND;
894 h->txbuf[1] = STLINK_DEBUG_WRITEMEM_8BIT;
895 h_u32_to_le(h->txbuf + 2, addr);
896 h_u16_to_le(h->txbuf + 2 + 4, len);
898 res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, 0, 0);
903 res = stlink_usb_recv(handle, (uint8_t *) buffer, len, 0, 0);
912 static int stlink_usb_read_mem32(void *handle, uint32_t addr, uint16_t len,
916 struct stlink_usb_handle_s *h;
918 assert(handle != NULL);
920 h = (struct stlink_usb_handle_s *)handle;
922 stlink_usb_init_buffer(handle);
926 h->txbuf[0] = STLINK_DEBUG_COMMAND;
927 h->txbuf[1] = STLINK_DEBUG_READMEM_32BIT;
928 h_u32_to_le(h->txbuf + 2, addr);
929 h_u16_to_le(h->txbuf + 2 + 4, len);
931 res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, len);
936 memcpy(buffer, h->rxbuf, len);
942 static int stlink_usb_write_mem32(void *handle, uint32_t addr, uint16_t len,
943 const uint32_t *buffer)
946 struct stlink_usb_handle_s *h;
948 assert(handle != NULL);
950 h = (struct stlink_usb_handle_s *)handle;
952 stlink_usb_init_buffer(handle);
956 h->txbuf[0] = STLINK_DEBUG_COMMAND;
957 h->txbuf[1] = STLINK_DEBUG_WRITEMEM_32BIT;
958 h_u32_to_le(h->txbuf + 2, addr);
959 h_u16_to_le(h->txbuf + 2 + 4, len);
961 res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, 0, 0);
966 res = stlink_usb_recv(handle, (uint8_t *) buffer, len, 0, 0);
975 static int stlink_usb_open(struct stlink_interface_param_s *param, void **fd)
978 struct stlink_usb_handle_s *h;
980 LOG_DEBUG("stlink_usb_open");
982 h = malloc(sizeof(struct stlink_usb_handle_s));
985 LOG_DEBUG("malloc failed");
989 h->transport = param->transport;
991 const uint16_t vids[] = { param->vid, 0 };
992 const uint16_t pids[] = { param->pid, 0 };
994 LOG_DEBUG("transport: %d vid: %04x pid: %04x", param->transport,
995 param->vid, param->pid);
997 if (jtag_libusb_open(vids, pids, &h->fd) != ERROR_OK) {
998 LOG_ERROR("open failed");
1002 jtag_libusb_set_configuration(h->fd, 0);
1004 if (jtag_libusb_claim_interface(h->fd, 0) != ERROR_OK) {
1005 LOG_DEBUG("claim interface failed");
1009 /* wrap version for first read */
1010 switch (param->pid) {
1012 h->version.stlink = 1;
1015 h->version.stlink = 2;
1019 /* get the device version */
1020 err = stlink_usb_version(h);
1022 if (err != ERROR_OK) {
1023 LOG_ERROR("read version failed");
1024 jtag_libusb_close(h->fd);
1029 /* compare usb vid/pid */
1030 if ((param->vid != h->vid) || (param->pid != h->pid))
1031 LOG_INFO("vid/pid are not identical: %04X/%04X %04X/%04X",
1032 param->vid, param->pid,
1035 /* check if mode is supported */
1038 switch (h->transport) {
1039 case STLINK_TRANSPORT_SWD:
1040 case STLINK_TRANSPORT_JTAG:
1041 if (h->version.jtag == 0)
1044 case STLINK_TRANSPORT_SWIM:
1045 if (h->version.swim == 0)
1053 if (err != ERROR_OK) {
1054 LOG_ERROR("mode (transport) not supported by device");
1055 jtag_libusb_close(h->fd);
1060 /* set the used jtag api */
1061 h->jtag_api = STLINK_JTAG_API_V1;
1063 /* initialize the debug hardware */
1064 err = stlink_usb_init_mode(h);
1066 if (err != ERROR_OK) {
1067 LOG_ERROR("init mode failed");
1068 jtag_libusb_close(h->fd);
1079 static int stlink_usb_close(void *fd)
1085 struct stlink_layout_api_s stlink_usb_layout_api = {
1087 .open = stlink_usb_open,
1089 .close = stlink_usb_close,
1091 .idcode = stlink_usb_idcode,
1093 .state = stlink_usb_state,
1095 .reset = stlink_usb_reset,
1097 .run = stlink_usb_run,
1099 .halt = stlink_usb_halt,
1101 .step = stlink_usb_step,
1103 .read_regs = stlink_usb_read_regs,
1105 .read_reg = stlink_usb_read_reg,
1107 .write_reg = stlink_usb_write_reg,
1109 .read_mem8 = stlink_usb_read_mem8,
1111 .write_mem8 = stlink_usb_write_mem8,
1113 .read_mem32 = stlink_usb_read_mem32,
1115 .write_mem32 = stlink_usb_write_mem32,