stlink: support stlink api result
[openocd/jflash.git] / src / jtag / drivers / stlink_usb.c
blobe373a27a19e7d5a5d6777ec01f92a7ab2ed5b01c
1 /***************************************************************************
2 * Copyright (C) 2011-2012 by Mathias Kuester *
3 * Mathias Kuester <kesmtp@freenet.de> *
4 * *
5 * This code is based on https://github.com/texane/stlink *
6 * *
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. *
11 * *
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. *
16 * *
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 ***************************************************************************/
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
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,
50 STLINK_JTAG_API_V2,
53 /** */
54 struct stlink_usb_version {
55 /** */
56 int stlink;
57 /** */
58 int jtag;
59 /** */
60 int swim;
61 /** highest supported jtag api version */
62 enum stlink_jtag_api_version jtag_api_max;
65 /** */
66 struct stlink_usb_handle_s {
67 /** */
68 struct jtag_libusb_device_handle *fd;
69 /** */
70 struct libusb_transfer *trans;
71 /** */
72 uint8_t cmdbuf[STLINK_SG_SIZE];
73 /** */
74 uint8_t cmdidx;
75 /** */
76 uint8_t direction;
77 /** */
78 uint8_t databuf[STLINK_DATA_SIZE];
79 /** */
80 enum stlink_transports transport;
81 /** */
82 struct stlink_usb_version version;
83 /** */
84 uint16_t vid;
85 /** */
86 uint16_t pid;
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
154 /** */
155 enum stlink_mode {
156 STLINK_MODE_UNKNOWN = 0,
157 STLINK_MODE_DFU,
158 STLINK_MODE_MASS,
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);
169 /** */
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;
178 /* read status */
179 memset(h->cmdbuf, 0, STLINK_SG_SIZE);
181 if (jtag_libusb_bulk_read(h->fd, STLINK_RX_EP, (char *)h->cmdbuf,
182 13, 1000) != 13)
183 return ERROR_FAIL;
185 uint32_t t1;
187 t1 = buf_get_u32(h->cmdbuf, 0, 32);
189 /* check for USBS */
190 if (t1 != 0x53425355)
191 return ERROR_FAIL;
193 * CSW status:
194 * 0 success
195 * 1 command failure
196 * 2 phase error
198 if (h->cmdbuf[12] != 0)
199 return ERROR_FAIL;
201 return ERROR_OK;
204 /** */
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,
214 1000) != cmdsize) {
215 return ERROR_FAIL;
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");
222 return ERROR_FAIL;
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");
228 return ERROR_FAIL;
232 return ERROR_OK;
235 /** */
236 static int stlink_usb_xfer_v1_get_sense(void *handle)
238 int res;
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);
255 if (res != ERROR_OK)
256 return res;
258 if (stlink_usb_xfer_v1_get_status(handle) != ERROR_OK)
259 return ERROR_FAIL;
261 return ERROR_OK;
264 /** */
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);
279 if (err != ERROR_OK)
280 return err;
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)
288 return ERROR_FAIL;
290 return ERROR_FAIL;
294 return ERROR_OK;
297 /** */
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");
306 h->cmdidx += 4;
307 /* csw tag not used */
308 h->cmdidx += 4;
309 buf_set_u32(h->cmdbuf+h->cmdidx, 0, 32, size);
310 h->cmdidx += 4;
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;
316 /** */
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;
325 h->cmdidx = 0;
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[] = {
335 "unknown"
338 /** */
339 static int stlink_usb_error_check(void *handle)
341 int res;
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:
355 res = ERROR_OK;
356 break;
357 case STLINK_DEBUG_ERR_FAULT:
358 default:
359 err_msg = stlink_usb_error_msg[0];
360 res = ERROR_FAIL;
361 break;
364 if (res != ERROR_OK)
365 LOG_DEBUG("status error: %d ('%s')", h->databuf[0], err_msg);
367 return res;
370 /** */
371 static int stlink_usb_version(void *handle)
373 int res;
374 uint16_t v;
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);
387 if (res != ERROR_OK)
388 return res;
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;
404 else
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 0x%04X PID 0x%04X",
408 h->version.stlink,
409 h->version.jtag,
410 (h->version.jtag_api_max == STLINK_JTAG_API_V1) ? 1 : 2,
411 h->version.swim,
412 h->vid,
413 h->pid);
415 return ERROR_OK;
418 /** */
419 static int stlink_usb_current_mode(void *handle, uint8_t *mode)
421 int res;
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);
434 if (res != ERROR_OK)
435 return res;
437 *mode = h->databuf[0];
439 return ERROR_OK;
442 /** */
443 static int stlink_usb_mode_enter(void *handle, enum stlink_mode type)
445 int res;
446 int rx_size = 0;
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
454 * status
455 * TODO: we need the test on api V1 too
457 if (h->jtag_api == STLINK_JTAG_API_V2)
458 rx_size = 2;
460 stlink_usb_init_buffer(handle, STLINK_RX_EP, rx_size);
462 switch (type) {
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;
467 else
468 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_ENTER;
469 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_ENTER_JTAG;
470 break;
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;
475 else
476 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_ENTER;
477 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_ENTER_SWD;
478 break;
479 case STLINK_MODE_DEBUG_SWIM:
480 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_COMMAND;
481 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_ENTER;
482 break;
483 case STLINK_MODE_DFU:
484 case STLINK_MODE_MASS:
485 default:
486 return ERROR_FAIL;
489 res = stlink_usb_xfer(handle, h->databuf, rx_size);
491 if (res != ERROR_OK)
492 return res;
494 res = stlink_usb_error_check(h);
496 return res;
499 /** */
500 static int stlink_usb_mode_leave(void *handle, enum stlink_mode type)
502 int res;
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);
511 switch (type) {
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;
516 break;
517 case STLINK_MODE_DEBUG_SWIM:
518 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_COMMAND;
519 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_EXIT;
520 break;
521 case STLINK_MODE_DFU:
522 h->cmdbuf[h->cmdidx++] = STLINK_DFU_COMMAND;
523 h->cmdbuf[h->cmdidx++] = STLINK_DFU_EXIT;
524 break;
525 case STLINK_MODE_MASS:
526 default:
527 return ERROR_FAIL;
530 res = stlink_usb_xfer(handle, 0, 0);
532 if (res != ERROR_OK)
533 return res;
535 return ERROR_OK;
538 /** */
539 static int stlink_usb_init_mode(void *handle)
541 int res;
542 uint8_t mode;
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);
552 if (res != ERROR_OK)
553 return res;
555 LOG_DEBUG("MODE: 0x%02X", mode);
557 /* try to exit current mode */
558 switch (mode) {
559 case STLINK_DEV_DFU_MODE:
560 emode = STLINK_MODE_DFU;
561 break;
562 case STLINK_DEV_DEBUG_MODE:
563 emode = STLINK_MODE_DEBUG_SWD;
564 break;
565 case STLINK_DEV_SWIM_MODE:
566 emode = STLINK_MODE_DEBUG_SWIM;
567 break;
568 case STLINK_DEV_BOOTLOADER_MODE:
569 case STLINK_DEV_MASS_MODE:
570 default:
571 emode = STLINK_MODE_UNKNOWN;
572 break;
575 if (emode != STLINK_MODE_UNKNOWN) {
576 res = stlink_usb_mode_leave(handle, emode);
578 if (res != ERROR_OK)
579 return res;
582 res = stlink_usb_current_mode(handle, &mode);
584 if (res != ERROR_OK)
585 return res;
587 LOG_DEBUG("MODE: 0x%02X", mode);
589 /* set selected mode */
590 switch (h->transport) {
591 case STLINK_TRANSPORT_SWD:
592 emode = STLINK_MODE_DEBUG_SWD;
593 break;
594 case STLINK_TRANSPORT_JTAG:
595 emode = STLINK_MODE_DEBUG_JTAG;
596 break;
597 case STLINK_TRANSPORT_SWIM:
598 emode = STLINK_MODE_DEBUG_SWIM;
599 break;
600 default:
601 emode = STLINK_MODE_UNKNOWN;
602 break;
605 if (emode == STLINK_MODE_UNKNOWN) {
606 LOG_ERROR("selected mode (transport) not supported");
607 return ERROR_FAIL;
610 res = stlink_usb_mode_enter(handle, emode);
612 if (res != ERROR_OK)
613 return res;
615 res = stlink_usb_current_mode(handle, &mode);
617 if (res != ERROR_OK)
618 return res;
620 LOG_DEBUG("MODE: 0x%02X", mode);
622 return ERROR_OK;
625 /** */
626 static int stlink_usb_idcode(void *handle, uint32_t *idcode)
628 int res;
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);
642 if (res != ERROR_OK)
643 return res;
645 *idcode = le_to_h_u32(h->databuf);
647 LOG_DEBUG("IDCODE: 0x%08X", *idcode);
649 return ERROR_OK;
652 /** */
653 static enum target_state stlink_usb_state(void *handle)
655 int res;
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);
672 if (res != ERROR_OK)
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;
683 /** */
684 static int stlink_usb_reset(void *handle)
686 int res;
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;
699 else
700 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_RESETSYS;
702 res = stlink_usb_xfer(handle, h->databuf, 2);
704 if (res != ERROR_OK)
705 return res;
707 LOG_DEBUG("RESET: 0x%08X", h->databuf[0]);
709 return h->databuf[0] == STLINK_DEBUG_ERR_OK ? ERROR_OK : ERROR_FAIL;
712 /** */
713 static int stlink_usb_run(void *handle)
715 int res;
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)
723 return ERROR_FAIL;
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);
732 if (res != ERROR_OK)
733 return res;
735 return h->databuf[0] == STLINK_DEBUG_ERR_OK ? ERROR_OK : ERROR_FAIL;
738 /** */
739 static int stlink_usb_halt(void *handle)
741 int res;
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)
749 return ERROR_FAIL;
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);
758 if (res != ERROR_OK)
759 return res;
761 return h->databuf[0] == STLINK_DEBUG_ERR_OK ? ERROR_OK : ERROR_FAIL;
764 /** */
765 static int stlink_usb_step(void *handle)
767 int res;
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)
775 return ERROR_FAIL;
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);
784 if (res != ERROR_OK)
785 return res;
787 return h->databuf[0] == STLINK_DEBUG_ERR_OK ? ERROR_OK : ERROR_FAIL;
790 /** */
791 static int stlink_usb_read_regs(void *handle)
793 int res;
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;
805 else
806 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_READALLREGS;
808 res = stlink_usb_xfer(handle, h->databuf, 84);
810 if (res != ERROR_OK)
811 return res;
813 return ERROR_OK;
816 /** */
817 static int stlink_usb_read_reg(void *handle, int num, uint32_t *val)
819 int res;
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;
831 else
832 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_READREG;
833 h->cmdbuf[h->cmdidx++] = num;
835 res = stlink_usb_xfer(handle, h->databuf, 4);
837 if (res != ERROR_OK)
838 return res;
840 *val = le_to_h_u32(h->databuf);
842 return ERROR_OK;
845 /** */
846 static int stlink_usb_write_reg(void *handle, int num, uint32_t val)
848 int res;
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;
860 else
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);
864 h->cmdidx += 4;
866 res = stlink_usb_xfer(handle, h->databuf, 2);
868 if (res != ERROR_OK)
869 return res;
871 return h->databuf[0] == STLINK_DEBUG_ERR_OK ? ERROR_OK : ERROR_FAIL;
874 /** */
875 static int stlink_usb_read_mem8(void *handle, uint32_t addr, uint16_t len,
876 uint8_t *buffer)
878 int res;
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);
891 h->cmdidx += 4;
892 h_u16_to_le(h->cmdbuf+h->cmdidx, len);
893 h->cmdidx += 2;
895 /* we need to fix read length for single bytes */
896 if (read_len == 1)
897 read_len++;
899 res = stlink_usb_xfer(handle, h->databuf, read_len);
901 if (res != ERROR_OK)
902 return res;
904 memcpy(buffer, h->databuf, len);
906 return ERROR_OK;
909 /** */
910 static int stlink_usb_write_mem8(void *handle, uint32_t addr, uint16_t len,
911 const uint8_t *buffer)
913 int res;
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);
925 h->cmdidx += 4;
926 h_u16_to_le(h->cmdbuf+h->cmdidx, len);
927 h->cmdidx += 2;
929 res = stlink_usb_xfer(handle, buffer, len);
931 if (res != ERROR_OK)
932 return res;
934 return ERROR_OK;
937 /** */
938 static int stlink_usb_read_mem32(void *handle, uint32_t addr, uint16_t len,
939 uint8_t *buffer)
941 int res;
942 struct stlink_usb_handle_s *h;
944 assert(handle != NULL);
946 h = (struct stlink_usb_handle_s *)handle;
948 len *= 4;
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);
955 h->cmdidx += 4;
956 h_u16_to_le(h->cmdbuf+h->cmdidx, len);
957 h->cmdidx += 2;
959 res = stlink_usb_xfer(handle, h->databuf, len);
961 if (res != ERROR_OK)
962 return res;
964 memcpy(buffer, h->databuf, len);
966 return ERROR_OK;
969 /** */
970 static int stlink_usb_write_mem32(void *handle, uint32_t addr, uint16_t len,
971 const uint8_t *buffer)
973 int res;
974 struct stlink_usb_handle_s *h;
976 assert(handle != NULL);
978 h = (struct stlink_usb_handle_s *)handle;
980 len *= 4;
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);
987 h->cmdidx += 4;
988 h_u16_to_le(h->cmdbuf+h->cmdidx, len);
989 h->cmdidx += 2;
991 res = stlink_usb_xfer(handle, buffer, len);
993 if (res != ERROR_OK)
994 return res;
996 return ERROR_OK;
999 /** */
1000 static int stlink_usb_open(struct stlink_interface_param_s *param, void **fd)
1002 int err;
1003 struct stlink_usb_handle_s *h;
1005 LOG_DEBUG("stlink_usb_open");
1007 h = malloc(sizeof(struct stlink_usb_handle_s));
1009 if (h == 0) {
1010 LOG_DEBUG("malloc failed");
1011 return ERROR_FAIL;
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: 0x%04x pid: 0x%04x", param->transport,
1020 param->vid, param->pid);
1022 if (jtag_libusb_open(vids, pids, &h->fd) != ERROR_OK) {
1023 LOG_ERROR("open failed");
1024 return ERROR_FAIL;
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");
1031 return ERROR_FAIL;
1034 /* wrap version for first read */
1035 switch (param->pid) {
1036 case 0x3744:
1037 h->version.stlink = 1;
1038 break;
1039 default:
1040 h->version.stlink = 2;
1041 break;
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);
1050 free(h);
1051 return err;
1054 /* compare usb vid/pid */
1055 if ((param->vid != h->vid) || (param->pid != h->pid))
1056 LOG_INFO("vid/pid are not identical: 0x%04X/0x%04X 0x%04X/0x%04X",
1057 param->vid, param->pid,
1058 h->vid, h->pid);
1060 /* check if mode is supported */
1061 err = ERROR_OK;
1063 switch (h->transport) {
1064 case STLINK_TRANSPORT_SWD:
1065 case STLINK_TRANSPORT_JTAG:
1066 if (h->version.jtag == 0)
1067 err = ERROR_FAIL;
1068 break;
1069 case STLINK_TRANSPORT_SWIM:
1070 if (h->version.swim == 0)
1071 err = ERROR_FAIL;
1072 break;
1073 default:
1074 err = ERROR_FAIL;
1075 break;
1078 if (err != ERROR_OK) {
1079 LOG_ERROR("mode (transport) not supported by device");
1080 jtag_libusb_close(h->fd);
1081 free(h);
1082 return err;
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);
1094 free(h);
1095 return err;
1098 *fd = h;
1100 return ERROR_OK;
1103 /** */
1104 static int stlink_usb_close(void *fd)
1106 return ERROR_OK;
1109 /** */
1110 struct stlink_layout_api_s stlink_usb_layout_api = {
1111 /** */
1112 .open = stlink_usb_open,
1113 /** */
1114 .close = stlink_usb_close,
1115 /** */
1116 .idcode = stlink_usb_idcode,
1117 /** */
1118 .state = stlink_usb_state,
1119 /** */
1120 .reset = stlink_usb_reset,
1121 /** */
1122 .run = stlink_usb_run,
1123 /** */
1124 .halt = stlink_usb_halt,
1125 /** */
1126 .step = stlink_usb_step,
1127 /** */
1128 .read_regs = stlink_usb_read_regs,
1129 /** */
1130 .read_reg = stlink_usb_read_reg,
1131 /** */
1132 .write_reg = stlink_usb_write_reg,
1133 /** */
1134 .read_mem8 = stlink_usb_read_mem8,
1135 /** */
1136 .write_mem8 = stlink_usb_write_mem8,
1137 /** */
1138 .read_mem32 = stlink_usb_read_mem32,
1139 /** */
1140 .write_mem32 = stlink_usb_write_mem32,