stlink: add generic open error routine
[openocd.git] / src / jtag / drivers / stlink_usb.c
blob0a16f2c07691b6d1b903b1cb7be4771c2478011a
1 /***************************************************************************
2 * Copyright (C) 2011-2012 by Mathias Kuester *
3 * Mathias Kuester <kesmtp@freenet.de> *
4 * *
5 * Copyright (C) 2012 by Spencer Oliver *
6 * spen@spen-soft.co.uk *
7 * *
8 * This code is based on https://github.com/texane/stlink *
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
14 * *
15 * This program is distributed in the hope that it will be useful, *
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
18 * GNU General Public License for more details. *
19 * *
20 * You should have received a copy of the GNU General Public License *
21 * along with this program; if not, write to the *
22 * Free Software Foundation, Inc., *
23 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
24 ***************************************************************************/
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
30 /* project specific includes */
31 #include <helper/binarybuffer.h>
32 #include <jtag/interface.h>
33 #include <jtag/hla/hla_layout.h>
34 #include <jtag/hla/hla_transport.h>
35 #include <jtag/hla/hla_interface.h>
36 #include <target/target.h>
38 #include <target/cortex_m.h>
40 #include "libusb_common.h"
42 #define ENDPOINT_IN 0x80
43 #define ENDPOINT_OUT 0x00
45 #define STLINK_NULL_EP 0
46 #define STLINK_RX_EP (1|ENDPOINT_IN)
47 #define STLINK_TX_EP (2|ENDPOINT_OUT)
48 #define STLINK_SG_SIZE (31)
49 #define STLINK_DATA_SIZE (4*128)
50 #define STLINK_CMD_SIZE_V2 (16)
51 #define STLINK_CMD_SIZE_V1 (10)
53 enum stlink_jtag_api_version {
54 STLINK_JTAG_API_V1 = 1,
55 STLINK_JTAG_API_V2,
58 /** */
59 struct stlink_usb_version {
60 /** */
61 int stlink;
62 /** */
63 int jtag;
64 /** */
65 int swim;
66 /** highest supported jtag api version */
67 enum stlink_jtag_api_version jtag_api_max;
70 /** */
71 struct stlink_usb_handle_s {
72 /** */
73 struct jtag_libusb_device_handle *fd;
74 /** */
75 struct libusb_transfer *trans;
76 /** */
77 uint8_t cmdbuf[STLINK_SG_SIZE];
78 /** */
79 uint8_t cmdidx;
80 /** */
81 uint8_t direction;
82 /** */
83 uint8_t databuf[STLINK_DATA_SIZE];
84 /** */
85 enum hl_transports transport;
86 /** */
87 struct stlink_usb_version version;
88 /** */
89 uint16_t vid;
90 /** */
91 uint16_t pid;
92 /** this is the currently used jtag api */
93 enum stlink_jtag_api_version jtag_api;
96 #define STLINK_DEBUG_ERR_OK 0x80
97 #define STLINK_DEBUG_ERR_FAULT 0x81
98 #define STLINK_SWD_AP_WAIT 0x10
99 #define STLINK_SWD_DP_WAIT 0x14
101 #define STLINK_CORE_RUNNING 0x80
102 #define STLINK_CORE_HALTED 0x81
103 #define STLINK_CORE_STAT_UNKNOWN -1
105 #define STLINK_GET_VERSION 0xF1
106 #define STLINK_DEBUG_COMMAND 0xF2
107 #define STLINK_DFU_COMMAND 0xF3
108 #define STLINK_SWIM_COMMAND 0xF4
109 #define STLINK_GET_CURRENT_MODE 0xF5
111 #define STLINK_DEV_DFU_MODE 0x00
112 #define STLINK_DEV_MASS_MODE 0x01
113 #define STLINK_DEV_DEBUG_MODE 0x02
114 #define STLINK_DEV_SWIM_MODE 0x03
115 #define STLINK_DEV_BOOTLOADER_MODE 0x04
116 #define STLINK_DEV_UNKNOWN_MODE -1
118 #define STLINK_DFU_EXIT 0x07
120 #define STLINK_SWIM_ENTER 0x00
121 #define STLINK_SWIM_EXIT 0x01
123 #define STLINK_DEBUG_ENTER_JTAG 0x00
124 #define STLINK_DEBUG_GETSTATUS 0x01
125 #define STLINK_DEBUG_FORCEDEBUG 0x02
126 #define STLINK_DEBUG_APIV1_RESETSYS 0x03
127 #define STLINK_DEBUG_APIV1_READALLREGS 0x04
128 #define STLINK_DEBUG_APIV1_READREG 0x05
129 #define STLINK_DEBUG_APIV1_WRITEREG 0x06
130 #define STLINK_DEBUG_READMEM_32BIT 0x07
131 #define STLINK_DEBUG_WRITEMEM_32BIT 0x08
132 #define STLINK_DEBUG_RUNCORE 0x09
133 #define STLINK_DEBUG_STEPCORE 0x0a
134 #define STLINK_DEBUG_APIV1_SETFP 0x0b
135 #define STLINK_DEBUG_READMEM_8BIT 0x0c
136 #define STLINK_DEBUG_WRITEMEM_8BIT 0x0d
137 #define STLINK_DEBUG_APIV1_CLEARFP 0x0e
138 #define STLINK_DEBUG_APIV1_WRITEDEBUGREG 0x0f
139 #define STLINK_DEBUG_APIV1_SETWATCHPOINT 0x10
141 #define STLINK_DEBUG_ENTER_JTAG 0x00
142 #define STLINK_DEBUG_ENTER_SWD 0xa3
144 #define STLINK_DEBUG_APIV1_ENTER 0x20
145 #define STLINK_DEBUG_EXIT 0x21
146 #define STLINK_DEBUG_READCOREID 0x22
148 #define STLINK_DEBUG_APIV2_ENTER 0x30
149 #define STLINK_DEBUG_APIV2_READ_IDCODES 0x31
150 #define STLINK_DEBUG_APIV2_RESETSYS 0x32
151 #define STLINK_DEBUG_APIV2_READREG 0x33
152 #define STLINK_DEBUG_APIV2_WRITEREG 0x34
153 #define STLINK_DEBUG_APIV2_WRITEDEBUGREG 0x35
154 #define STLINK_DEBUG_APIV2_READDEBUGREG 0x36
156 #define STLINK_DEBUG_APIV2_READALLREGS 0x3A
157 #define STLINK_DEBUG_APIV2_GETLASTRWSTATUS 0x3B
158 #define STLINK_DEBUG_APIV2_DRIVE_NRST 0x3C
160 #define STLINK_DEBUG_APIV2_DRIVE_NRST_LOW 0x00
161 #define STLINK_DEBUG_APIV2_DRIVE_NRST_HIGH 0x01
162 #define STLINK_DEBUG_APIV2_DRIVE_NRST_PULSE 0x02
164 /** */
165 enum stlink_mode {
166 STLINK_MODE_UNKNOWN = 0,
167 STLINK_MODE_DFU,
168 STLINK_MODE_MASS,
169 STLINK_MODE_DEBUG_JTAG,
170 STLINK_MODE_DEBUG_SWD,
171 STLINK_MODE_DEBUG_SWIM
174 #define REQUEST_SENSE 0x03
175 #define REQUEST_SENSE_LENGTH 18
177 static void stlink_usb_init_buffer(void *handle, uint8_t direction, uint32_t size);
179 /** */
180 static int stlink_usb_xfer_v1_get_status(void *handle)
182 struct stlink_usb_handle_s *h;
184 assert(handle != NULL);
186 h = (struct stlink_usb_handle_s *)handle;
188 /* read status */
189 memset(h->cmdbuf, 0, STLINK_SG_SIZE);
191 if (jtag_libusb_bulk_read(h->fd, STLINK_RX_EP, (char *)h->cmdbuf,
192 13, 1000) != 13)
193 return ERROR_FAIL;
195 uint32_t t1;
197 t1 = buf_get_u32(h->cmdbuf, 0, 32);
199 /* check for USBS */
200 if (t1 != 0x53425355)
201 return ERROR_FAIL;
203 * CSW status:
204 * 0 success
205 * 1 command failure
206 * 2 phase error
208 if (h->cmdbuf[12] != 0)
209 return ERROR_FAIL;
211 return ERROR_OK;
214 /** */
215 static int stlink_usb_xfer_rw(void *handle, int cmdsize, const uint8_t *buf, int size)
217 struct stlink_usb_handle_s *h;
219 assert(handle != NULL);
221 h = (struct stlink_usb_handle_s *)handle;
223 if (jtag_libusb_bulk_write(h->fd, STLINK_TX_EP, (char *)h->cmdbuf, cmdsize,
224 1000) != cmdsize) {
225 return ERROR_FAIL;
228 if (h->direction == STLINK_TX_EP && size) {
229 if (jtag_libusb_bulk_write(h->fd, STLINK_TX_EP, (char *)buf,
230 size, 1000) != size) {
231 LOG_DEBUG("bulk write failed");
232 return ERROR_FAIL;
234 } else if (h->direction == STLINK_RX_EP && size) {
235 if (jtag_libusb_bulk_read(h->fd, STLINK_RX_EP, (char *)buf,
236 size, 1000) != size) {
237 LOG_DEBUG("bulk read failed");
238 return ERROR_FAIL;
242 return ERROR_OK;
245 /** */
246 static int stlink_usb_xfer_v1_get_sense(void *handle)
248 int res;
249 struct stlink_usb_handle_s *h;
251 assert(handle != NULL);
253 h = (struct stlink_usb_handle_s *)handle;
255 stlink_usb_init_buffer(handle, STLINK_RX_EP, 16);
257 h->cmdbuf[h->cmdidx++] = REQUEST_SENSE;
258 h->cmdbuf[h->cmdidx++] = 0;
259 h->cmdbuf[h->cmdidx++] = 0;
260 h->cmdbuf[h->cmdidx++] = 0;
261 h->cmdbuf[h->cmdidx++] = REQUEST_SENSE_LENGTH;
263 res = stlink_usb_xfer_rw(handle, REQUEST_SENSE_LENGTH, h->databuf, 16);
265 if (res != ERROR_OK)
266 return res;
268 if (stlink_usb_xfer_v1_get_status(handle) != ERROR_OK)
269 return ERROR_FAIL;
271 return ERROR_OK;
274 /** */
275 static int stlink_usb_xfer(void *handle, const uint8_t *buf, int size)
277 int err, cmdsize = STLINK_CMD_SIZE_V2;
278 struct stlink_usb_handle_s *h;
280 assert(handle != NULL);
282 h = (struct stlink_usb_handle_s *)handle;
284 if (h->version.stlink == 1)
285 cmdsize = STLINK_SG_SIZE;
287 err = stlink_usb_xfer_rw(handle, cmdsize, buf, size);
289 if (err != ERROR_OK)
290 return err;
292 if (h->version.stlink == 1) {
293 if (stlink_usb_xfer_v1_get_status(handle) != ERROR_OK) {
294 /* check csw status */
295 if (h->cmdbuf[12] == 1) {
296 LOG_DEBUG("get sense");
297 if (stlink_usb_xfer_v1_get_sense(handle) != ERROR_OK)
298 return ERROR_FAIL;
300 return ERROR_FAIL;
304 return ERROR_OK;
307 /** */
308 static void stlink_usb_xfer_v1_create_cmd(void *handle, uint8_t direction, uint32_t size)
310 struct stlink_usb_handle_s *h;
312 h = (struct stlink_usb_handle_s *)handle;
314 /* fill the send buffer */
315 strcpy((char *)h->cmdbuf, "USBC");
316 h->cmdidx += 4;
317 /* csw tag not used */
318 h->cmdidx += 4;
319 buf_set_u32(h->cmdbuf+h->cmdidx, 0, 32, size);
320 h->cmdidx += 4;
321 h->cmdbuf[h->cmdidx++] = (direction == STLINK_RX_EP ? ENDPOINT_IN : ENDPOINT_OUT);
322 h->cmdbuf[h->cmdidx++] = 0; /* lun */
323 h->cmdbuf[h->cmdidx++] = STLINK_CMD_SIZE_V1;
326 /** */
327 static void stlink_usb_init_buffer(void *handle, uint8_t direction, uint32_t size)
329 struct stlink_usb_handle_s *h;
331 h = (struct stlink_usb_handle_s *)handle;
333 h->direction = direction;
335 h->cmdidx = 0;
337 memset(h->cmdbuf, 0, STLINK_SG_SIZE);
338 memset(h->databuf, 0, STLINK_DATA_SIZE);
340 if (h->version.stlink == 1)
341 stlink_usb_xfer_v1_create_cmd(handle, direction, size);
344 static const char * const stlink_usb_error_msg[] = {
345 "unknown"
348 /** */
349 static int stlink_usb_error_check(void *handle)
351 int res;
352 const char *err_msg = 0;
353 struct stlink_usb_handle_s *h;
355 assert(handle != NULL);
357 h = (struct stlink_usb_handle_s *)handle;
359 /* TODO: no error checking yet on api V1 */
360 if (h->jtag_api == STLINK_JTAG_API_V1)
361 h->databuf[0] = STLINK_DEBUG_ERR_OK;
363 switch (h->databuf[0]) {
364 case STLINK_DEBUG_ERR_OK:
365 res = ERROR_OK;
366 break;
367 case STLINK_DEBUG_ERR_FAULT:
368 default:
369 err_msg = stlink_usb_error_msg[0];
370 res = ERROR_FAIL;
371 break;
374 if (res != ERROR_OK)
375 LOG_DEBUG("status error: %d ('%s')", h->databuf[0], err_msg);
377 return res;
380 /** */
381 static int stlink_usb_version(void *handle)
383 int res;
384 uint16_t v;
385 struct stlink_usb_handle_s *h;
387 assert(handle != NULL);
389 h = (struct stlink_usb_handle_s *)handle;
391 stlink_usb_init_buffer(handle, STLINK_RX_EP, 6);
393 h->cmdbuf[h->cmdidx++] = STLINK_GET_VERSION;
395 res = stlink_usb_xfer(handle, h->databuf, 6);
397 if (res != ERROR_OK)
398 return res;
400 v = (h->databuf[0] << 8) | h->databuf[1];
402 h->version.stlink = (v >> 12) & 0x0f;
403 h->version.jtag = (v >> 6) & 0x3f;
404 h->version.swim = v & 0x3f;
405 h->vid = buf_get_u32(h->databuf, 16, 16);
406 h->pid = buf_get_u32(h->databuf, 32, 16);
408 /* set the supported jtag api version
409 * API V2 is supported since JTAG V11
411 if (h->version.jtag >= 11)
412 h->version.jtag_api_max = STLINK_JTAG_API_V2;
413 else
414 h->version.jtag_api_max = STLINK_JTAG_API_V1;
416 LOG_INFO("STLINK v%d JTAG v%d API v%d SWIM v%d VID 0x%04X PID 0x%04X",
417 h->version.stlink,
418 h->version.jtag,
419 (h->version.jtag_api_max == STLINK_JTAG_API_V1) ? 1 : 2,
420 h->version.swim,
421 h->vid,
422 h->pid);
424 return ERROR_OK;
427 /** */
428 static int stlink_usb_current_mode(void *handle, uint8_t *mode)
430 int res;
431 struct stlink_usb_handle_s *h;
433 assert(handle != NULL);
435 h = (struct stlink_usb_handle_s *)handle;
437 stlink_usb_init_buffer(handle, STLINK_RX_EP, 2);
439 h->cmdbuf[h->cmdidx++] = STLINK_GET_CURRENT_MODE;
441 res = stlink_usb_xfer(handle, h->databuf, 2);
443 if (res != ERROR_OK)
444 return res;
446 *mode = h->databuf[0];
448 return ERROR_OK;
451 /** */
452 static int stlink_usb_mode_enter(void *handle, enum stlink_mode type)
454 int res;
455 int rx_size = 0;
456 struct stlink_usb_handle_s *h;
458 assert(handle != NULL);
460 h = (struct stlink_usb_handle_s *)handle;
462 /* on api V2 we are able the read the latest command
463 * status
464 * TODO: we need the test on api V1 too
466 if (h->jtag_api == STLINK_JTAG_API_V2)
467 rx_size = 2;
469 stlink_usb_init_buffer(handle, STLINK_RX_EP, rx_size);
471 switch (type) {
472 case STLINK_MODE_DEBUG_JTAG:
473 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
474 if (h->jtag_api == STLINK_JTAG_API_V1)
475 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_ENTER;
476 else
477 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_ENTER;
478 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_ENTER_JTAG;
479 break;
480 case STLINK_MODE_DEBUG_SWD:
481 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
482 if (h->jtag_api == STLINK_JTAG_API_V1)
483 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_ENTER;
484 else
485 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_ENTER;
486 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_ENTER_SWD;
487 break;
488 case STLINK_MODE_DEBUG_SWIM:
489 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_COMMAND;
490 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_ENTER;
491 break;
492 case STLINK_MODE_DFU:
493 case STLINK_MODE_MASS:
494 default:
495 return ERROR_FAIL;
498 res = stlink_usb_xfer(handle, h->databuf, rx_size);
500 if (res != ERROR_OK)
501 return res;
503 res = stlink_usb_error_check(h);
505 return res;
508 /** */
509 static int stlink_usb_mode_leave(void *handle, enum stlink_mode type)
511 int res;
512 struct stlink_usb_handle_s *h;
514 assert(handle != NULL);
516 h = (struct stlink_usb_handle_s *)handle;
518 stlink_usb_init_buffer(handle, STLINK_NULL_EP, 0);
520 switch (type) {
521 case STLINK_MODE_DEBUG_JTAG:
522 case STLINK_MODE_DEBUG_SWD:
523 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
524 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_EXIT;
525 break;
526 case STLINK_MODE_DEBUG_SWIM:
527 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_COMMAND;
528 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_EXIT;
529 break;
530 case STLINK_MODE_DFU:
531 h->cmdbuf[h->cmdidx++] = STLINK_DFU_COMMAND;
532 h->cmdbuf[h->cmdidx++] = STLINK_DFU_EXIT;
533 break;
534 case STLINK_MODE_MASS:
535 default:
536 return ERROR_FAIL;
539 res = stlink_usb_xfer(handle, 0, 0);
541 if (res != ERROR_OK)
542 return res;
544 return ERROR_OK;
547 /** */
548 static int stlink_usb_init_mode(void *handle)
550 int res;
551 uint8_t mode;
552 enum stlink_mode emode;
553 struct stlink_usb_handle_s *h;
555 assert(handle != NULL);
557 h = (struct stlink_usb_handle_s *)handle;
559 res = stlink_usb_current_mode(handle, &mode);
561 if (res != ERROR_OK)
562 return res;
564 LOG_DEBUG("MODE: 0x%02X", mode);
566 /* try to exit current mode */
567 switch (mode) {
568 case STLINK_DEV_DFU_MODE:
569 emode = STLINK_MODE_DFU;
570 break;
571 case STLINK_DEV_DEBUG_MODE:
572 emode = STLINK_MODE_DEBUG_SWD;
573 break;
574 case STLINK_DEV_SWIM_MODE:
575 emode = STLINK_MODE_DEBUG_SWIM;
576 break;
577 case STLINK_DEV_BOOTLOADER_MODE:
578 case STLINK_DEV_MASS_MODE:
579 default:
580 emode = STLINK_MODE_UNKNOWN;
581 break;
584 if (emode != STLINK_MODE_UNKNOWN) {
585 res = stlink_usb_mode_leave(handle, emode);
587 if (res != ERROR_OK)
588 return res;
591 res = stlink_usb_current_mode(handle, &mode);
593 if (res != ERROR_OK)
594 return res;
596 LOG_DEBUG("MODE: 0x%02X", mode);
598 /* set selected mode */
599 switch (h->transport) {
600 case HL_TRANSPORT_SWD:
601 emode = STLINK_MODE_DEBUG_SWD;
602 break;
603 case HL_TRANSPORT_JTAG:
604 emode = STLINK_MODE_DEBUG_JTAG;
605 break;
606 case HL_TRANSPORT_SWIM:
607 emode = STLINK_MODE_DEBUG_SWIM;
608 break;
609 default:
610 emode = STLINK_MODE_UNKNOWN;
611 break;
614 if (emode == STLINK_MODE_UNKNOWN) {
615 LOG_ERROR("selected mode (transport) not supported");
616 return ERROR_FAIL;
619 res = stlink_usb_mode_enter(handle, emode);
621 if (res != ERROR_OK)
622 return res;
624 res = stlink_usb_current_mode(handle, &mode);
626 if (res != ERROR_OK)
627 return res;
629 LOG_DEBUG("MODE: 0x%02X", mode);
631 return ERROR_OK;
634 /** */
635 static int stlink_usb_idcode(void *handle, uint32_t *idcode)
637 int res;
638 struct stlink_usb_handle_s *h;
640 assert(handle != NULL);
642 h = (struct stlink_usb_handle_s *)handle;
644 stlink_usb_init_buffer(handle, STLINK_RX_EP, 4);
646 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
647 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_READCOREID;
649 res = stlink_usb_xfer(handle, h->databuf, 4);
651 if (res != ERROR_OK)
652 return res;
654 *idcode = le_to_h_u32(h->databuf);
656 LOG_DEBUG("IDCODE: 0x%08X", *idcode);
658 return ERROR_OK;
661 static int stlink_usb_v2_read_debug_reg(void *handle, uint32_t addr, uint32_t *val)
663 struct stlink_usb_handle_s *h;
664 int res;
666 assert(handle != NULL);
668 h = (struct stlink_usb_handle_s *)handle;
670 stlink_usb_init_buffer(handle, STLINK_RX_EP, 8);
672 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
673 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_READDEBUGREG;
674 h_u32_to_le(h->cmdbuf+h->cmdidx, addr);
675 h->cmdidx += 4;
677 res = stlink_usb_xfer(handle, h->databuf, 8);
679 if (res != ERROR_OK)
680 return res;
682 *val = le_to_h_u32(h->databuf + 4);
684 return h->databuf[0] == STLINK_DEBUG_ERR_OK ? ERROR_OK : ERROR_FAIL;
687 static int stlink_usb_write_debug_reg(void *handle, uint32_t addr, uint32_t val)
689 int res;
690 struct stlink_usb_handle_s *h;
692 assert(handle != NULL);
694 h = (struct stlink_usb_handle_s *)handle;
696 stlink_usb_init_buffer(handle, STLINK_RX_EP, 2);
698 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
699 if (h->jtag_api == STLINK_JTAG_API_V1)
700 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_WRITEDEBUGREG;
701 else
702 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_WRITEDEBUGREG;
703 h_u32_to_le(h->cmdbuf+h->cmdidx, addr);
704 h->cmdidx += 4;
705 h_u32_to_le(h->cmdbuf+h->cmdidx, val);
706 h->cmdidx += 4;
708 res = stlink_usb_xfer(handle, h->databuf, 2);
710 if (res != ERROR_OK)
711 return res;
713 return h->databuf[0] == STLINK_DEBUG_ERR_OK ? ERROR_OK : ERROR_FAIL;
716 static enum target_state stlink_usb_v2_get_status(void *handle)
718 int result;
719 uint32_t status;
721 result = stlink_usb_v2_read_debug_reg(handle, DCB_DHCSR, &status);
722 if (result != ERROR_OK)
723 return TARGET_UNKNOWN;
725 if (status & S_HALT)
726 return TARGET_HALTED;
727 else if (status & S_RESET_ST)
728 return TARGET_RESET;
730 return TARGET_RUNNING;
733 /** */
734 static enum target_state stlink_usb_state(void *handle)
736 int res;
737 struct stlink_usb_handle_s *h;
739 assert(handle != NULL);
741 h = (struct stlink_usb_handle_s *)handle;
743 if (h->jtag_api == STLINK_JTAG_API_V2)
744 return stlink_usb_v2_get_status(handle);
746 stlink_usb_init_buffer(handle, STLINK_RX_EP, 2);
748 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
749 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_GETSTATUS;
751 res = stlink_usb_xfer(handle, h->databuf, 2);
753 if (res != ERROR_OK)
754 return TARGET_UNKNOWN;
756 if (h->databuf[0] == STLINK_CORE_RUNNING)
757 return TARGET_RUNNING;
758 if (h->databuf[0] == STLINK_CORE_HALTED)
759 return TARGET_HALTED;
761 return TARGET_UNKNOWN;
764 /** */
765 static int stlink_usb_reset(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 stlink_usb_init_buffer(handle, STLINK_RX_EP, 2);
776 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
778 if (h->jtag_api == STLINK_JTAG_API_V1)
779 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_RESETSYS;
780 else
781 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_RESETSYS;
783 res = stlink_usb_xfer(handle, h->databuf, 2);
785 if (res != ERROR_OK)
786 return res;
788 LOG_DEBUG("RESET: 0x%08X", h->databuf[0]);
790 /* the following is not a error under swd (using hardware srst), so return success */
791 if (h->databuf[0] == STLINK_SWD_AP_WAIT || h->databuf[0] == STLINK_SWD_DP_WAIT)
792 return ERROR_OK;
794 return h->databuf[0] == STLINK_DEBUG_ERR_OK ? ERROR_OK : ERROR_FAIL;
797 static int stlink_usb_assert_srst(void *handle, int srst)
799 int res;
800 struct stlink_usb_handle_s *h;
802 assert(handle != NULL);
804 h = (struct stlink_usb_handle_s *)handle;
806 if (h->jtag_api == STLINK_JTAG_API_V1)
807 return ERROR_COMMAND_NOTFOUND;
809 stlink_usb_init_buffer(handle, STLINK_RX_EP, 2);
811 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
812 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_DRIVE_NRST;
813 h->cmdbuf[h->cmdidx++] = srst;
815 res = stlink_usb_xfer(handle, h->databuf, 2);
817 if (res != ERROR_OK)
818 return res;
820 return h->databuf[0] == STLINK_DEBUG_ERR_OK ? ERROR_OK : ERROR_FAIL;
823 /** */
824 static int stlink_usb_run(void *handle)
826 int res;
827 struct stlink_usb_handle_s *h;
829 assert(handle != NULL);
831 h = (struct stlink_usb_handle_s *)handle;
833 if (h->jtag_api == STLINK_JTAG_API_V2)
834 return stlink_usb_write_debug_reg(handle, DCB_DHCSR, DBGKEY|C_DEBUGEN);
836 stlink_usb_init_buffer(handle, STLINK_RX_EP, 2);
838 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
839 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_RUNCORE;
841 res = stlink_usb_xfer(handle, h->databuf, 2);
843 if (res != ERROR_OK)
844 return res;
846 return h->databuf[0] == STLINK_DEBUG_ERR_OK ? ERROR_OK : ERROR_FAIL;
849 /** */
850 static int stlink_usb_halt(void *handle)
852 int res;
853 struct stlink_usb_handle_s *h;
855 assert(handle != NULL);
857 h = (struct stlink_usb_handle_s *)handle;
859 if (h->jtag_api == STLINK_JTAG_API_V2)
860 return stlink_usb_write_debug_reg(handle, DCB_DHCSR, DBGKEY|C_HALT|C_DEBUGEN);
862 stlink_usb_init_buffer(handle, STLINK_RX_EP, 2);
864 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
865 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_FORCEDEBUG;
867 res = stlink_usb_xfer(handle, h->databuf, 2);
869 if (res != ERROR_OK)
870 return res;
872 return h->databuf[0] == STLINK_DEBUG_ERR_OK ? ERROR_OK : ERROR_FAIL;
875 /** */
876 static int stlink_usb_step(void *handle)
878 int res;
879 struct stlink_usb_handle_s *h;
881 assert(handle != NULL);
883 h = (struct stlink_usb_handle_s *)handle;
885 if (h->jtag_api == STLINK_JTAG_API_V2) {
886 /* TODO: this emulates the v1 api, it should really use a similar auto mask isr
887 * that the cortex-m3 currently does. */
888 stlink_usb_write_debug_reg(handle, DCB_DHCSR, DBGKEY|C_HALT|C_MASKINTS|C_DEBUGEN);
889 stlink_usb_write_debug_reg(handle, DCB_DHCSR, DBGKEY|C_STEP|C_MASKINTS|C_DEBUGEN);
890 return stlink_usb_write_debug_reg(handle, DCB_DHCSR, DBGKEY|C_HALT|C_DEBUGEN);
893 stlink_usb_init_buffer(handle, STLINK_RX_EP, 2);
895 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
896 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_STEPCORE;
898 res = stlink_usb_xfer(handle, h->databuf, 2);
900 if (res != ERROR_OK)
901 return res;
903 return h->databuf[0] == STLINK_DEBUG_ERR_OK ? ERROR_OK : ERROR_FAIL;
906 /** */
907 static int stlink_usb_read_regs(void *handle)
909 int res;
910 struct stlink_usb_handle_s *h;
912 assert(handle != NULL);
914 h = (struct stlink_usb_handle_s *)handle;
916 stlink_usb_init_buffer(handle, STLINK_RX_EP, 84);
918 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
919 if (h->jtag_api == STLINK_JTAG_API_V1)
920 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_READALLREGS;
921 else
922 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_READALLREGS;
924 res = stlink_usb_xfer(handle, h->databuf, 84);
926 if (res != ERROR_OK)
927 return res;
929 return ERROR_OK;
932 /** */
933 static int stlink_usb_read_reg(void *handle, int num, uint32_t *val)
935 int res;
936 struct stlink_usb_handle_s *h;
938 assert(handle != NULL);
940 h = (struct stlink_usb_handle_s *)handle;
942 stlink_usb_init_buffer(handle, STLINK_RX_EP, h->jtag_api == STLINK_JTAG_API_V1 ? 4 : 8);
944 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
945 if (h->jtag_api == STLINK_JTAG_API_V1)
946 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_READREG;
947 else
948 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_READREG;
949 h->cmdbuf[h->cmdidx++] = num;
951 res = stlink_usb_xfer(handle, h->databuf, h->jtag_api == STLINK_JTAG_API_V1 ? 4 : 8);
953 if (res != ERROR_OK)
954 return res;
956 if (h->jtag_api == STLINK_JTAG_API_V1)
957 *val = le_to_h_u32(h->databuf);
958 else {
959 *val = le_to_h_u32(h->databuf + 4);
960 return h->databuf[0] == STLINK_DEBUG_ERR_OK ? ERROR_OK : ERROR_FAIL;
963 return ERROR_OK;
966 /** */
967 static int stlink_usb_write_reg(void *handle, int num, uint32_t val)
969 int res;
970 struct stlink_usb_handle_s *h;
972 assert(handle != NULL);
974 h = (struct stlink_usb_handle_s *)handle;
976 stlink_usb_init_buffer(handle, STLINK_RX_EP, 2);
978 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
979 if (h->jtag_api == STLINK_JTAG_API_V1)
980 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_WRITEREG;
981 else
982 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_WRITEREG;
983 h->cmdbuf[h->cmdidx++] = num;
984 h_u32_to_le(h->cmdbuf+h->cmdidx, val);
985 h->cmdidx += 4;
987 res = stlink_usb_xfer(handle, h->databuf, 2);
989 if (res != ERROR_OK)
990 return res;
992 return h->databuf[0] == STLINK_DEBUG_ERR_OK ? ERROR_OK : ERROR_FAIL;
995 static int stlink_usb_get_rw_status(void *handle)
997 int res;
998 struct stlink_usb_handle_s *h;
1000 assert(handle != NULL);
1002 h = (struct stlink_usb_handle_s *)handle;
1004 if (h->jtag_api == STLINK_JTAG_API_V1)
1005 return ERROR_OK;
1007 stlink_usb_init_buffer(handle, STLINK_RX_EP, 2);
1009 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1010 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_GETLASTRWSTATUS;
1012 res = stlink_usb_xfer(handle, h->databuf, 2);
1014 if (res != ERROR_OK)
1015 return res;
1017 return h->databuf[0] == STLINK_DEBUG_ERR_OK ? ERROR_OK : res;
1020 /** */
1021 static int stlink_usb_read_mem8(void *handle, uint32_t addr, uint16_t len,
1022 uint8_t *buffer)
1024 int res;
1025 uint16_t read_len = len;
1026 struct stlink_usb_handle_s *h;
1028 assert(handle != NULL);
1030 h = (struct stlink_usb_handle_s *)handle;
1032 stlink_usb_init_buffer(handle, STLINK_RX_EP, read_len);
1034 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1035 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_READMEM_8BIT;
1036 h_u32_to_le(h->cmdbuf+h->cmdidx, addr);
1037 h->cmdidx += 4;
1038 h_u16_to_le(h->cmdbuf+h->cmdidx, len);
1039 h->cmdidx += 2;
1041 /* we need to fix read length for single bytes */
1042 if (read_len == 1)
1043 read_len++;
1045 res = stlink_usb_xfer(handle, h->databuf, read_len);
1047 if (res != ERROR_OK)
1048 return res;
1050 memcpy(buffer, h->databuf, len);
1052 return stlink_usb_get_rw_status(handle);
1055 /** */
1056 static int stlink_usb_write_mem8(void *handle, uint32_t addr, uint16_t len,
1057 const uint8_t *buffer)
1059 int res;
1060 struct stlink_usb_handle_s *h;
1062 assert(handle != NULL);
1064 h = (struct stlink_usb_handle_s *)handle;
1066 stlink_usb_init_buffer(handle, STLINK_TX_EP, len);
1068 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1069 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_WRITEMEM_8BIT;
1070 h_u32_to_le(h->cmdbuf+h->cmdidx, addr);
1071 h->cmdidx += 4;
1072 h_u16_to_le(h->cmdbuf+h->cmdidx, len);
1073 h->cmdidx += 2;
1075 res = stlink_usb_xfer(handle, buffer, len);
1077 if (res != ERROR_OK)
1078 return res;
1080 return stlink_usb_get_rw_status(handle);
1083 /** */
1084 static int stlink_usb_read_mem32(void *handle, uint32_t addr, uint16_t len,
1085 uint8_t *buffer)
1087 int res;
1088 struct stlink_usb_handle_s *h;
1090 assert(handle != NULL);
1092 h = (struct stlink_usb_handle_s *)handle;
1094 len *= 4;
1096 stlink_usb_init_buffer(handle, STLINK_RX_EP, len);
1098 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1099 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_READMEM_32BIT;
1100 h_u32_to_le(h->cmdbuf+h->cmdidx, addr);
1101 h->cmdidx += 4;
1102 h_u16_to_le(h->cmdbuf+h->cmdidx, len);
1103 h->cmdidx += 2;
1105 res = stlink_usb_xfer(handle, h->databuf, len);
1107 if (res != ERROR_OK)
1108 return res;
1110 memcpy(buffer, h->databuf, len);
1112 return stlink_usb_get_rw_status(handle);
1115 /** */
1116 static int stlink_usb_write_mem32(void *handle, uint32_t addr, uint16_t len,
1117 const uint8_t *buffer)
1119 int res;
1120 struct stlink_usb_handle_s *h;
1122 assert(handle != NULL);
1124 h = (struct stlink_usb_handle_s *)handle;
1126 len *= 4;
1128 stlink_usb_init_buffer(handle, STLINK_TX_EP, len);
1130 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1131 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_WRITEMEM_32BIT;
1132 h_u32_to_le(h->cmdbuf+h->cmdidx, addr);
1133 h->cmdidx += 4;
1134 h_u16_to_le(h->cmdbuf+h->cmdidx, len);
1135 h->cmdidx += 2;
1137 res = stlink_usb_xfer(handle, buffer, len);
1139 if (res != ERROR_OK)
1140 return res;
1142 return stlink_usb_get_rw_status(handle);
1145 /** */
1146 static int stlink_usb_close(void *fd)
1148 struct stlink_usb_handle_s *h;
1150 h = (struct stlink_usb_handle_s *)fd;
1152 if (h->fd)
1153 jtag_libusb_close(h->fd);
1155 free(fd);
1157 return ERROR_OK;
1160 /** */
1161 static int stlink_usb_open(struct hl_interface_param_s *param, void **fd)
1163 int err;
1164 struct stlink_usb_handle_s *h;
1165 enum stlink_jtag_api_version api;
1167 LOG_DEBUG("stlink_usb_open");
1169 h = calloc(1, sizeof(struct stlink_usb_handle_s));
1171 if (h == 0) {
1172 LOG_DEBUG("malloc failed");
1173 return ERROR_FAIL;
1176 h->transport = param->transport;
1178 /* set max read/write buffer size in bytes */
1179 param->max_buffer = 512;
1181 const uint16_t vids[] = { param->vid, 0 };
1182 const uint16_t pids[] = { param->pid, 0 };
1184 LOG_DEBUG("transport: %d vid: 0x%04x pid: 0x%04x", param->transport,
1185 param->vid, param->pid);
1187 if (jtag_libusb_open(vids, pids, &h->fd) != ERROR_OK) {
1188 LOG_ERROR("open failed");
1189 goto error_open;
1192 jtag_libusb_set_configuration(h->fd, 0);
1194 if (jtag_libusb_claim_interface(h->fd, 0) != ERROR_OK) {
1195 LOG_DEBUG("claim interface failed");
1196 goto error_open;
1199 /* wrap version for first read */
1200 switch (param->pid) {
1201 case 0x3744:
1202 h->version.stlink = 1;
1203 break;
1204 default:
1205 h->version.stlink = 2;
1206 break;
1209 /* get the device version */
1210 err = stlink_usb_version(h);
1212 if (err != ERROR_OK) {
1213 LOG_ERROR("read version failed");
1214 goto error_open;
1217 /* compare usb vid/pid */
1218 if ((param->vid != h->vid) || (param->pid != h->pid))
1219 LOG_INFO("vid/pid are not identical: 0x%04X/0x%04X 0x%04X/0x%04X",
1220 param->vid, param->pid,
1221 h->vid, h->pid);
1223 /* check if mode is supported */
1224 err = ERROR_OK;
1226 switch (h->transport) {
1227 case HL_TRANSPORT_SWD:
1228 case HL_TRANSPORT_JTAG:
1229 if (h->version.jtag == 0)
1230 err = ERROR_FAIL;
1231 break;
1232 case HL_TRANSPORT_SWIM:
1233 if (h->version.swim == 0)
1234 err = ERROR_FAIL;
1235 break;
1236 default:
1237 err = ERROR_FAIL;
1238 break;
1241 if (err != ERROR_OK) {
1242 LOG_ERROR("mode (transport) not supported by device");
1243 goto error_open;
1246 api = h->version.jtag_api_max;
1248 /* check that user has not requested certain api version
1249 * and if they have check it is supported */
1250 if ((param->api != 0) && (param->api <= h->version.jtag_api_max)) {
1251 api = param->api;
1252 LOG_INFO("using stlink api v%d", api);
1255 /* set the used jtag api, this will default to the newest supported version */
1256 h->jtag_api = api;
1258 /* initialize the debug hardware */
1259 err = stlink_usb_init_mode(h);
1261 if (err != ERROR_OK) {
1262 LOG_ERROR("init mode failed");
1263 goto error_open;
1266 *fd = h;
1268 return ERROR_OK;
1270 error_open:
1271 stlink_usb_close(h);
1273 return ERROR_FAIL;
1276 /** */
1277 struct hl_layout_api_s stlink_usb_layout_api = {
1278 /** */
1279 .open = stlink_usb_open,
1280 /** */
1281 .close = stlink_usb_close,
1282 /** */
1283 .idcode = stlink_usb_idcode,
1284 /** */
1285 .state = stlink_usb_state,
1286 /** */
1287 .reset = stlink_usb_reset,
1288 /** */
1289 .assert_srst = stlink_usb_assert_srst,
1290 /** */
1291 .run = stlink_usb_run,
1292 /** */
1293 .halt = stlink_usb_halt,
1294 /** */
1295 .step = stlink_usb_step,
1296 /** */
1297 .read_regs = stlink_usb_read_regs,
1298 /** */
1299 .read_reg = stlink_usb_read_reg,
1300 /** */
1301 .write_reg = stlink_usb_write_reg,
1302 /** */
1303 .read_mem8 = stlink_usb_read_mem8,
1304 /** */
1305 .write_mem8 = stlink_usb_write_mem8,
1306 /** */
1307 .read_mem32 = stlink_usb_read_mem32,
1308 /** */
1309 .write_mem32 = stlink_usb_write_mem32,
1310 /** */
1311 .write_debug_reg = stlink_usb_write_debug_reg