stlink: add myself to copyright header
[openocd/jflash.git] / src / jtag / drivers / stlink_usb.c
blobb2f380aecb6172189cdad362769c4265e78ad15e
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/stlink/stlink_layout.h>
34 #include <jtag/stlink/stlink_transport.h>
35 #include <jtag/stlink/stlink_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 stlink_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_CORE_RUNNING 0x80
99 #define STLINK_CORE_HALTED 0x81
100 #define STLINK_CORE_STAT_UNKNOWN -1
102 #define STLINK_GET_VERSION 0xF1
103 #define STLINK_DEBUG_COMMAND 0xF2
104 #define STLINK_DFU_COMMAND 0xF3
105 #define STLINK_SWIM_COMMAND 0xF4
106 #define STLINK_GET_CURRENT_MODE 0xF5
108 #define STLINK_DEV_DFU_MODE 0x00
109 #define STLINK_DEV_MASS_MODE 0x01
110 #define STLINK_DEV_DEBUG_MODE 0x02
111 #define STLINK_DEV_SWIM_MODE 0x03
112 #define STLINK_DEV_BOOTLOADER_MODE 0x04
113 #define STLINK_DEV_UNKNOWN_MODE -1
115 #define STLINK_DFU_EXIT 0x07
117 #define STLINK_SWIM_ENTER 0x00
118 #define STLINK_SWIM_EXIT 0x01
120 #define STLINK_DEBUG_ENTER_JTAG 0x00
121 #define STLINK_DEBUG_GETSTATUS 0x01
122 #define STLINK_DEBUG_FORCEDEBUG 0x02
123 #define STLINK_DEBUG_APIV1_RESETSYS 0x03
124 #define STLINK_DEBUG_APIV1_READALLREGS 0x04
125 #define STLINK_DEBUG_APIV1_READREG 0x05
126 #define STLINK_DEBUG_APIV1_WRITEREG 0x06
127 #define STLINK_DEBUG_READMEM_32BIT 0x07
128 #define STLINK_DEBUG_WRITEMEM_32BIT 0x08
129 #define STLINK_DEBUG_RUNCORE 0x09
130 #define STLINK_DEBUG_STEPCORE 0x0a
131 #define STLINK_DEBUG_APIV1_SETFP 0x0b
132 #define STLINK_DEBUG_READMEM_8BIT 0x0c
133 #define STLINK_DEBUG_WRITEMEM_8BIT 0x0d
134 #define STLINK_DEBUG_APIV1_CLEARFP 0x0e
135 #define STLINK_DEBUG_APIV1_WRITEDEBUGREG 0x0f
136 #define STLINK_DEBUG_APIV1_SETWATCHPOINT 0x10
138 #define STLINK_DEBUG_ENTER_JTAG 0x00
139 #define STLINK_DEBUG_ENTER_SWD 0xa3
141 #define STLINK_DEBUG_APIV1_ENTER 0x20
142 #define STLINK_DEBUG_EXIT 0x21
143 #define STLINK_DEBUG_READCOREID 0x22
145 #define STLINK_DEBUG_APIV2_ENTER 0x30
146 #define STLINK_DEBUG_APIV2_READ_IDCODES 0x31
147 #define STLINK_DEBUG_APIV2_RESETSYS 0x32
148 #define STLINK_DEBUG_APIV2_READREG 0x33
149 #define STLINK_DEBUG_APIV2_WRITEREG 0x34
150 #define STLINK_DEBUG_APIV2_WRITEDEBUGREG 0x35
151 #define STLINK_DEBUG_APIV2_READDEBUGREG 0x36
153 #define STLINK_DEBUG_APIV2_READALLREGS 0x3A
154 #define STLINK_DEBUG_APIV2_GETLASTRWSTATUS 0x3B
155 #define STLINK_DEBUG_APIV2_DRIVE_NRST 0x3C
157 #define STLINK_DEBUG_APIV2_DRIVE_NRST_LOW 0x00
158 #define STLINK_DEBUG_APIV2_DRIVE_NRST_HIGH 0x01
159 #define STLINK_DEBUG_APIV2_DRIVE_NRST_PULSE 0x02
161 /** */
162 enum stlink_mode {
163 STLINK_MODE_UNKNOWN = 0,
164 STLINK_MODE_DFU,
165 STLINK_MODE_MASS,
166 STLINK_MODE_DEBUG_JTAG,
167 STLINK_MODE_DEBUG_SWD,
168 STLINK_MODE_DEBUG_SWIM
171 #define REQUEST_SENSE 0x03
172 #define REQUEST_SENSE_LENGTH 18
174 static void stlink_usb_init_buffer(void *handle, uint8_t direction, uint32_t size);
176 /** */
177 static int stlink_usb_xfer_v1_get_status(void *handle)
179 struct stlink_usb_handle_s *h;
181 assert(handle != NULL);
183 h = (struct stlink_usb_handle_s *)handle;
185 /* read status */
186 memset(h->cmdbuf, 0, STLINK_SG_SIZE);
188 if (jtag_libusb_bulk_read(h->fd, STLINK_RX_EP, (char *)h->cmdbuf,
189 13, 1000) != 13)
190 return ERROR_FAIL;
192 uint32_t t1;
194 t1 = buf_get_u32(h->cmdbuf, 0, 32);
196 /* check for USBS */
197 if (t1 != 0x53425355)
198 return ERROR_FAIL;
200 * CSW status:
201 * 0 success
202 * 1 command failure
203 * 2 phase error
205 if (h->cmdbuf[12] != 0)
206 return ERROR_FAIL;
208 return ERROR_OK;
211 /** */
212 static int stlink_usb_xfer_rw(void *handle, int cmdsize, const uint8_t *buf, int size)
214 struct stlink_usb_handle_s *h;
216 assert(handle != NULL);
218 h = (struct stlink_usb_handle_s *)handle;
220 if (jtag_libusb_bulk_write(h->fd, STLINK_TX_EP, (char *)h->cmdbuf, cmdsize,
221 1000) != cmdsize) {
222 return ERROR_FAIL;
225 if (h->direction == STLINK_TX_EP && size) {
226 if (jtag_libusb_bulk_write(h->fd, STLINK_TX_EP, (char *)buf,
227 size, 1000) != size) {
228 LOG_DEBUG("bulk write failed");
229 return ERROR_FAIL;
231 } else if (h->direction == STLINK_RX_EP && size) {
232 if (jtag_libusb_bulk_read(h->fd, STLINK_RX_EP, (char *)buf,
233 size, 1000) != size) {
234 LOG_DEBUG("bulk read failed");
235 return ERROR_FAIL;
239 return ERROR_OK;
242 /** */
243 static int stlink_usb_xfer_v1_get_sense(void *handle)
245 int res;
246 struct stlink_usb_handle_s *h;
248 assert(handle != NULL);
250 h = (struct stlink_usb_handle_s *)handle;
252 stlink_usb_init_buffer(handle, STLINK_RX_EP, 16);
254 h->cmdbuf[h->cmdidx++] = REQUEST_SENSE;
255 h->cmdbuf[h->cmdidx++] = 0;
256 h->cmdbuf[h->cmdidx++] = 0;
257 h->cmdbuf[h->cmdidx++] = 0;
258 h->cmdbuf[h->cmdidx++] = REQUEST_SENSE_LENGTH;
260 res = stlink_usb_xfer_rw(handle, REQUEST_SENSE_LENGTH, h->databuf, 16);
262 if (res != ERROR_OK)
263 return res;
265 if (stlink_usb_xfer_v1_get_status(handle) != ERROR_OK)
266 return ERROR_FAIL;
268 return ERROR_OK;
271 /** */
272 static int stlink_usb_xfer(void *handle, const uint8_t *buf, int size)
274 int err, cmdsize = STLINK_CMD_SIZE_V2;
275 struct stlink_usb_handle_s *h;
277 assert(handle != NULL);
279 h = (struct stlink_usb_handle_s *)handle;
281 if (h->version.stlink == 1)
282 cmdsize = STLINK_SG_SIZE;
284 err = stlink_usb_xfer_rw(handle, cmdsize, buf, size);
286 if (err != ERROR_OK)
287 return err;
289 if (h->version.stlink == 1) {
290 if (stlink_usb_xfer_v1_get_status(handle) != ERROR_OK) {
291 /* check csw status */
292 if (h->cmdbuf[12] == 1) {
293 LOG_DEBUG("get sense");
294 if (stlink_usb_xfer_v1_get_sense(handle) != ERROR_OK)
295 return ERROR_FAIL;
297 return ERROR_FAIL;
301 return ERROR_OK;
304 /** */
305 static void stlink_usb_xfer_v1_create_cmd(void *handle, uint8_t direction, uint32_t size)
307 struct stlink_usb_handle_s *h;
309 h = (struct stlink_usb_handle_s *)handle;
311 /* fill the send buffer */
312 strcpy((char *)h->cmdbuf, "USBC");
313 h->cmdidx += 4;
314 /* csw tag not used */
315 h->cmdidx += 4;
316 buf_set_u32(h->cmdbuf+h->cmdidx, 0, 32, size);
317 h->cmdidx += 4;
318 h->cmdbuf[h->cmdidx++] = (direction == STLINK_RX_EP ? ENDPOINT_IN : ENDPOINT_OUT);
319 h->cmdbuf[h->cmdidx++] = 0; /* lun */
320 h->cmdbuf[h->cmdidx++] = STLINK_CMD_SIZE_V1;
323 /** */
324 static void stlink_usb_init_buffer(void *handle, uint8_t direction, uint32_t size)
326 struct stlink_usb_handle_s *h;
328 h = (struct stlink_usb_handle_s *)handle;
330 h->direction = direction;
332 h->cmdidx = 0;
334 memset(h->cmdbuf, 0, STLINK_SG_SIZE);
335 memset(h->databuf, 0, STLINK_DATA_SIZE);
337 if (h->version.stlink == 1)
338 stlink_usb_xfer_v1_create_cmd(handle, direction, size);
341 static const char * const stlink_usb_error_msg[] = {
342 "unknown"
345 /** */
346 static int stlink_usb_error_check(void *handle)
348 int res;
349 const char *err_msg = 0;
350 struct stlink_usb_handle_s *h;
352 assert(handle != NULL);
354 h = (struct stlink_usb_handle_s *)handle;
356 /* TODO: no error checking yet on api V1 */
357 if (h->jtag_api == STLINK_JTAG_API_V1)
358 h->databuf[0] = STLINK_DEBUG_ERR_OK;
360 switch (h->databuf[0]) {
361 case STLINK_DEBUG_ERR_OK:
362 res = ERROR_OK;
363 break;
364 case STLINK_DEBUG_ERR_FAULT:
365 default:
366 err_msg = stlink_usb_error_msg[0];
367 res = ERROR_FAIL;
368 break;
371 if (res != ERROR_OK)
372 LOG_DEBUG("status error: %d ('%s')", h->databuf[0], err_msg);
374 return res;
377 /** */
378 static int stlink_usb_version(void *handle)
380 int res;
381 uint16_t v;
382 struct stlink_usb_handle_s *h;
384 assert(handle != NULL);
386 h = (struct stlink_usb_handle_s *)handle;
388 stlink_usb_init_buffer(handle, STLINK_RX_EP, 6);
390 h->cmdbuf[h->cmdidx++] = STLINK_GET_VERSION;
392 res = stlink_usb_xfer(handle, h->databuf, 6);
394 if (res != ERROR_OK)
395 return res;
397 v = (h->databuf[0] << 8) | h->databuf[1];
399 h->version.stlink = (v >> 12) & 0x0f;
400 h->version.jtag = (v >> 6) & 0x3f;
401 h->version.swim = v & 0x3f;
402 h->vid = buf_get_u32(h->databuf, 16, 16);
403 h->pid = buf_get_u32(h->databuf, 32, 16);
405 /* set the supported jtag api version
406 * API V2 is supported since JTAG V11
408 if (h->version.jtag >= 11)
409 h->version.jtag_api_max = STLINK_JTAG_API_V2;
410 else
411 h->version.jtag_api_max = STLINK_JTAG_API_V1;
413 LOG_DEBUG("STLINK v%d JTAG v%d API v%d SWIM v%d VID 0x%04X PID 0x%04X",
414 h->version.stlink,
415 h->version.jtag,
416 (h->version.jtag_api_max == STLINK_JTAG_API_V1) ? 1 : 2,
417 h->version.swim,
418 h->vid,
419 h->pid);
421 return ERROR_OK;
424 /** */
425 static int stlink_usb_current_mode(void *handle, uint8_t *mode)
427 int res;
428 struct stlink_usb_handle_s *h;
430 assert(handle != NULL);
432 h = (struct stlink_usb_handle_s *)handle;
434 stlink_usb_init_buffer(handle, STLINK_RX_EP, 2);
436 h->cmdbuf[h->cmdidx++] = STLINK_GET_CURRENT_MODE;
438 res = stlink_usb_xfer(handle, h->databuf, 2);
440 if (res != ERROR_OK)
441 return res;
443 *mode = h->databuf[0];
445 return ERROR_OK;
448 /** */
449 static int stlink_usb_mode_enter(void *handle, enum stlink_mode type)
451 int res;
452 int rx_size = 0;
453 struct stlink_usb_handle_s *h;
455 assert(handle != NULL);
457 h = (struct stlink_usb_handle_s *)handle;
459 /* on api V2 we are able the read the latest command
460 * status
461 * TODO: we need the test on api V1 too
463 if (h->jtag_api == STLINK_JTAG_API_V2)
464 rx_size = 2;
466 stlink_usb_init_buffer(handle, STLINK_RX_EP, rx_size);
468 switch (type) {
469 case STLINK_MODE_DEBUG_JTAG:
470 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
471 if (h->jtag_api == STLINK_JTAG_API_V1)
472 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_ENTER;
473 else
474 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_ENTER;
475 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_ENTER_JTAG;
476 break;
477 case STLINK_MODE_DEBUG_SWD:
478 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
479 if (h->jtag_api == STLINK_JTAG_API_V1)
480 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_ENTER;
481 else
482 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_ENTER;
483 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_ENTER_SWD;
484 break;
485 case STLINK_MODE_DEBUG_SWIM:
486 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_COMMAND;
487 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_ENTER;
488 break;
489 case STLINK_MODE_DFU:
490 case STLINK_MODE_MASS:
491 default:
492 return ERROR_FAIL;
495 res = stlink_usb_xfer(handle, h->databuf, rx_size);
497 if (res != ERROR_OK)
498 return res;
500 res = stlink_usb_error_check(h);
502 return res;
505 /** */
506 static int stlink_usb_mode_leave(void *handle, enum stlink_mode type)
508 int res;
509 struct stlink_usb_handle_s *h;
511 assert(handle != NULL);
513 h = (struct stlink_usb_handle_s *)handle;
515 stlink_usb_init_buffer(handle, STLINK_NULL_EP, 0);
517 switch (type) {
518 case STLINK_MODE_DEBUG_JTAG:
519 case STLINK_MODE_DEBUG_SWD:
520 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
521 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_EXIT;
522 break;
523 case STLINK_MODE_DEBUG_SWIM:
524 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_COMMAND;
525 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_EXIT;
526 break;
527 case STLINK_MODE_DFU:
528 h->cmdbuf[h->cmdidx++] = STLINK_DFU_COMMAND;
529 h->cmdbuf[h->cmdidx++] = STLINK_DFU_EXIT;
530 break;
531 case STLINK_MODE_MASS:
532 default:
533 return ERROR_FAIL;
536 res = stlink_usb_xfer(handle, 0, 0);
538 if (res != ERROR_OK)
539 return res;
541 return ERROR_OK;
544 /** */
545 static int stlink_usb_init_mode(void *handle)
547 int res;
548 uint8_t mode;
549 enum stlink_mode emode;
550 struct stlink_usb_handle_s *h;
552 assert(handle != NULL);
554 h = (struct stlink_usb_handle_s *)handle;
556 res = stlink_usb_current_mode(handle, &mode);
558 if (res != ERROR_OK)
559 return res;
561 LOG_DEBUG("MODE: 0x%02X", mode);
563 /* try to exit current mode */
564 switch (mode) {
565 case STLINK_DEV_DFU_MODE:
566 emode = STLINK_MODE_DFU;
567 break;
568 case STLINK_DEV_DEBUG_MODE:
569 emode = STLINK_MODE_DEBUG_SWD;
570 break;
571 case STLINK_DEV_SWIM_MODE:
572 emode = STLINK_MODE_DEBUG_SWIM;
573 break;
574 case STLINK_DEV_BOOTLOADER_MODE:
575 case STLINK_DEV_MASS_MODE:
576 default:
577 emode = STLINK_MODE_UNKNOWN;
578 break;
581 if (emode != STLINK_MODE_UNKNOWN) {
582 res = stlink_usb_mode_leave(handle, emode);
584 if (res != ERROR_OK)
585 return res;
588 res = stlink_usb_current_mode(handle, &mode);
590 if (res != ERROR_OK)
591 return res;
593 LOG_DEBUG("MODE: 0x%02X", mode);
595 /* set selected mode */
596 switch (h->transport) {
597 case STLINK_TRANSPORT_SWD:
598 emode = STLINK_MODE_DEBUG_SWD;
599 break;
600 case STLINK_TRANSPORT_JTAG:
601 emode = STLINK_MODE_DEBUG_JTAG;
602 break;
603 case STLINK_TRANSPORT_SWIM:
604 emode = STLINK_MODE_DEBUG_SWIM;
605 break;
606 default:
607 emode = STLINK_MODE_UNKNOWN;
608 break;
611 if (emode == STLINK_MODE_UNKNOWN) {
612 LOG_ERROR("selected mode (transport) not supported");
613 return ERROR_FAIL;
616 res = stlink_usb_mode_enter(handle, emode);
618 if (res != ERROR_OK)
619 return res;
621 res = stlink_usb_current_mode(handle, &mode);
623 if (res != ERROR_OK)
624 return res;
626 LOG_DEBUG("MODE: 0x%02X", mode);
628 return ERROR_OK;
631 /** */
632 static int stlink_usb_idcode(void *handle, uint32_t *idcode)
634 int res;
635 struct stlink_usb_handle_s *h;
637 assert(handle != NULL);
639 h = (struct stlink_usb_handle_s *)handle;
641 stlink_usb_init_buffer(handle, STLINK_RX_EP, 4);
643 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
644 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_READCOREID;
646 res = stlink_usb_xfer(handle, h->databuf, 4);
648 if (res != ERROR_OK)
649 return res;
651 *idcode = le_to_h_u32(h->databuf);
653 LOG_DEBUG("IDCODE: 0x%08X", *idcode);
655 return ERROR_OK;
658 static int stlink_usb_v2_read_debug_reg(void *handle, uint32_t addr, uint32_t *val)
660 struct stlink_usb_handle_s *h;
661 int res;
663 assert(handle != NULL);
665 h = (struct stlink_usb_handle_s *)handle;
667 stlink_usb_init_buffer(handle, STLINK_RX_EP, 8);
669 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
670 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_READDEBUGREG;
671 h_u32_to_le(h->cmdbuf+h->cmdidx, addr);
672 h->cmdidx += 4;
674 res = stlink_usb_xfer(handle, h->databuf, 8);
676 if (res != ERROR_OK)
677 return res;
679 *val = le_to_h_u32(h->databuf + 4);
681 return h->databuf[0] == STLINK_DEBUG_ERR_OK ? ERROR_OK : ERROR_FAIL;
684 static int stlink_usb_write_debug_reg(void *handle, uint32_t addr, uint32_t val)
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;
696 if (h->jtag_api == STLINK_JTAG_API_V1)
697 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_WRITEDEBUGREG;
698 else
699 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_WRITEDEBUGREG;
700 h_u32_to_le(h->cmdbuf+h->cmdidx, addr);
701 h->cmdidx += 4;
702 h_u32_to_le(h->cmdbuf+h->cmdidx, val);
703 h->cmdidx += 4;
705 res = stlink_usb_xfer(handle, h->databuf, 2);
707 if (res != ERROR_OK)
708 return res;
710 return h->databuf[0] == STLINK_DEBUG_ERR_OK ? ERROR_OK : ERROR_FAIL;
713 static enum target_state stlink_usb_v2_get_status(void *handle)
715 int result;
716 uint32_t status;
718 result = stlink_usb_v2_read_debug_reg(handle, DCB_DHCSR, &status);
719 if (result != ERROR_OK)
720 return TARGET_UNKNOWN;
722 if (status & S_HALT)
723 return TARGET_HALTED;
724 else if (status & S_RESET_ST)
725 return TARGET_RESET;
727 return TARGET_RUNNING;
730 /** */
731 static enum target_state stlink_usb_state(void *handle)
733 int res;
734 struct stlink_usb_handle_s *h;
736 assert(handle != NULL);
738 h = (struct stlink_usb_handle_s *)handle;
740 if (h->jtag_api == STLINK_JTAG_API_V2)
741 return stlink_usb_v2_get_status(handle);
743 stlink_usb_init_buffer(handle, STLINK_RX_EP, 2);
745 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
746 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_GETSTATUS;
748 res = stlink_usb_xfer(handle, h->databuf, 2);
750 if (res != ERROR_OK)
751 return TARGET_UNKNOWN;
753 if (h->databuf[0] == STLINK_CORE_RUNNING)
754 return TARGET_RUNNING;
755 if (h->databuf[0] == STLINK_CORE_HALTED)
756 return TARGET_HALTED;
758 return TARGET_UNKNOWN;
761 /** */
762 static int stlink_usb_reset(void *handle)
764 int res;
765 struct stlink_usb_handle_s *h;
767 assert(handle != NULL);
769 h = (struct stlink_usb_handle_s *)handle;
771 stlink_usb_init_buffer(handle, STLINK_RX_EP, 2);
773 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
775 if (h->jtag_api == STLINK_JTAG_API_V1)
776 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_RESETSYS;
777 else
778 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_RESETSYS;
780 res = stlink_usb_xfer(handle, h->databuf, 2);
782 if (res != ERROR_OK)
783 return res;
785 LOG_DEBUG("RESET: 0x%08X", h->databuf[0]);
787 return h->databuf[0] == STLINK_DEBUG_ERR_OK ? ERROR_OK : ERROR_FAIL;
790 static int stlink_usb_assert_srst(void *handle, int srst)
792 int res;
793 struct stlink_usb_handle_s *h;
795 assert(handle != NULL);
797 h = (struct stlink_usb_handle_s *)handle;
799 if (h->jtag_api == STLINK_JTAG_API_V1)
800 return ERROR_COMMAND_NOTFOUND;
802 stlink_usb_init_buffer(handle, STLINK_RX_EP, 2);
804 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
805 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_DRIVE_NRST;
806 h->cmdbuf[h->cmdidx++] = srst;
808 res = stlink_usb_xfer(handle, h->databuf, 2);
810 if (res != ERROR_OK)
811 return res;
813 return h->databuf[0] == STLINK_DEBUG_ERR_OK ? ERROR_OK : ERROR_FAIL;
816 /** */
817 static int stlink_usb_run(void *handle)
819 int res;
820 struct stlink_usb_handle_s *h;
822 assert(handle != NULL);
824 h = (struct stlink_usb_handle_s *)handle;
826 if (h->jtag_api == STLINK_JTAG_API_V2)
827 return stlink_usb_write_debug_reg(handle, DCB_DHCSR, DBGKEY|C_DEBUGEN);
829 stlink_usb_init_buffer(handle, STLINK_RX_EP, 2);
831 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
832 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_RUNCORE;
834 res = stlink_usb_xfer(handle, h->databuf, 2);
836 if (res != ERROR_OK)
837 return res;
839 return h->databuf[0] == STLINK_DEBUG_ERR_OK ? ERROR_OK : ERROR_FAIL;
842 /** */
843 static int stlink_usb_halt(void *handle)
845 int res;
846 struct stlink_usb_handle_s *h;
848 assert(handle != NULL);
850 h = (struct stlink_usb_handle_s *)handle;
852 if (h->jtag_api == STLINK_JTAG_API_V2)
853 return stlink_usb_write_debug_reg(handle, DCB_DHCSR, DBGKEY|C_HALT|C_DEBUGEN);
855 stlink_usb_init_buffer(handle, STLINK_RX_EP, 2);
857 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
858 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_FORCEDEBUG;
860 res = stlink_usb_xfer(handle, h->databuf, 2);
862 if (res != ERROR_OK)
863 return res;
865 return h->databuf[0] == STLINK_DEBUG_ERR_OK ? ERROR_OK : ERROR_FAIL;
868 /** */
869 static int stlink_usb_step(void *handle)
871 int res;
872 struct stlink_usb_handle_s *h;
874 assert(handle != NULL);
876 h = (struct stlink_usb_handle_s *)handle;
878 if (h->jtag_api == STLINK_JTAG_API_V2) {
879 /* TODO: this emulates the v1 api, it should really use a similar auto mask isr
880 * that the cortex-m3 currently does. */
881 stlink_usb_write_debug_reg(handle, DCB_DHCSR, DBGKEY|C_HALT|C_MASKINTS|C_DEBUGEN);
882 stlink_usb_write_debug_reg(handle, DCB_DHCSR, DBGKEY|C_STEP|C_MASKINTS|C_DEBUGEN);
883 return stlink_usb_write_debug_reg(handle, DCB_DHCSR, DBGKEY|C_HALT|C_DEBUGEN);
886 stlink_usb_init_buffer(handle, STLINK_RX_EP, 2);
888 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
889 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_STEPCORE;
891 res = stlink_usb_xfer(handle, h->databuf, 2);
893 if (res != ERROR_OK)
894 return res;
896 return h->databuf[0] == STLINK_DEBUG_ERR_OK ? ERROR_OK : ERROR_FAIL;
899 /** */
900 static int stlink_usb_read_regs(void *handle)
902 int res;
903 struct stlink_usb_handle_s *h;
905 assert(handle != NULL);
907 h = (struct stlink_usb_handle_s *)handle;
909 stlink_usb_init_buffer(handle, STLINK_RX_EP, 84);
911 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
912 if (h->jtag_api == STLINK_JTAG_API_V1)
913 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_READALLREGS;
914 else
915 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_READALLREGS;
917 res = stlink_usb_xfer(handle, h->databuf, 84);
919 if (res != ERROR_OK)
920 return res;
922 return ERROR_OK;
925 /** */
926 static int stlink_usb_read_reg(void *handle, int num, uint32_t *val)
928 int res;
929 struct stlink_usb_handle_s *h;
931 assert(handle != NULL);
933 h = (struct stlink_usb_handle_s *)handle;
935 stlink_usb_init_buffer(handle, STLINK_RX_EP, h->jtag_api == STLINK_JTAG_API_V1 ? 4 : 8);
937 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
938 if (h->jtag_api == STLINK_JTAG_API_V1)
939 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_READREG;
940 else
941 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_READREG;
942 h->cmdbuf[h->cmdidx++] = num;
944 res = stlink_usb_xfer(handle, h->databuf, h->jtag_api == STLINK_JTAG_API_V1 ? 4 : 8);
946 if (res != ERROR_OK)
947 return res;
949 if (h->jtag_api == STLINK_JTAG_API_V1)
950 *val = le_to_h_u32(h->databuf);
951 else {
952 *val = le_to_h_u32(h->databuf + 4);
953 return h->databuf[0] == STLINK_DEBUG_ERR_OK ? ERROR_OK : ERROR_FAIL;
956 return ERROR_OK;
959 /** */
960 static int stlink_usb_write_reg(void *handle, int num, uint32_t val)
962 int res;
963 struct stlink_usb_handle_s *h;
965 assert(handle != NULL);
967 h = (struct stlink_usb_handle_s *)handle;
969 stlink_usb_init_buffer(handle, STLINK_RX_EP, 2);
971 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
972 if (h->jtag_api == STLINK_JTAG_API_V1)
973 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_WRITEREG;
974 else
975 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_WRITEREG;
976 h->cmdbuf[h->cmdidx++] = num;
977 h_u32_to_le(h->cmdbuf+h->cmdidx, val);
978 h->cmdidx += 4;
980 res = stlink_usb_xfer(handle, h->databuf, 2);
982 if (res != ERROR_OK)
983 return res;
985 return h->databuf[0] == STLINK_DEBUG_ERR_OK ? ERROR_OK : ERROR_FAIL;
988 static int stlink_usb_get_rw_status(void *handle)
990 int res;
991 struct stlink_usb_handle_s *h;
993 assert(handle != NULL);
995 h = (struct stlink_usb_handle_s *)handle;
997 if (h->jtag_api == STLINK_JTAG_API_V1)
998 return ERROR_OK;
1000 stlink_usb_init_buffer(handle, STLINK_RX_EP, 2);
1002 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1003 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_GETLASTRWSTATUS;
1005 res = stlink_usb_xfer(handle, h->databuf, 2);
1007 if (res != ERROR_OK)
1008 return res;
1010 return h->databuf[0] == STLINK_DEBUG_ERR_OK ? ERROR_OK : res;
1013 /** */
1014 static int stlink_usb_read_mem8(void *handle, uint32_t addr, uint16_t len,
1015 uint8_t *buffer)
1017 int res;
1018 uint16_t read_len = len;
1019 struct stlink_usb_handle_s *h;
1021 assert(handle != NULL);
1023 h = (struct stlink_usb_handle_s *)handle;
1025 stlink_usb_init_buffer(handle, STLINK_RX_EP, read_len);
1027 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1028 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_READMEM_8BIT;
1029 h_u32_to_le(h->cmdbuf+h->cmdidx, addr);
1030 h->cmdidx += 4;
1031 h_u16_to_le(h->cmdbuf+h->cmdidx, len);
1032 h->cmdidx += 2;
1034 /* we need to fix read length for single bytes */
1035 if (read_len == 1)
1036 read_len++;
1038 res = stlink_usb_xfer(handle, h->databuf, read_len);
1040 if (res != ERROR_OK)
1041 return res;
1043 memcpy(buffer, h->databuf, len);
1045 return stlink_usb_get_rw_status(handle);
1048 /** */
1049 static int stlink_usb_write_mem8(void *handle, uint32_t addr, uint16_t len,
1050 const uint8_t *buffer)
1052 int res;
1053 struct stlink_usb_handle_s *h;
1055 assert(handle != NULL);
1057 h = (struct stlink_usb_handle_s *)handle;
1059 stlink_usb_init_buffer(handle, STLINK_TX_EP, len);
1061 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1062 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_WRITEMEM_8BIT;
1063 h_u32_to_le(h->cmdbuf+h->cmdidx, addr);
1064 h->cmdidx += 4;
1065 h_u16_to_le(h->cmdbuf+h->cmdidx, len);
1066 h->cmdidx += 2;
1068 res = stlink_usb_xfer(handle, buffer, len);
1070 if (res != ERROR_OK)
1071 return res;
1073 return stlink_usb_get_rw_status(handle);
1076 /** */
1077 static int stlink_usb_read_mem32(void *handle, uint32_t addr, uint16_t len,
1078 uint8_t *buffer)
1080 int res;
1081 struct stlink_usb_handle_s *h;
1083 assert(handle != NULL);
1085 h = (struct stlink_usb_handle_s *)handle;
1087 len *= 4;
1089 stlink_usb_init_buffer(handle, STLINK_RX_EP, len);
1091 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1092 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_READMEM_32BIT;
1093 h_u32_to_le(h->cmdbuf+h->cmdidx, addr);
1094 h->cmdidx += 4;
1095 h_u16_to_le(h->cmdbuf+h->cmdidx, len);
1096 h->cmdidx += 2;
1098 res = stlink_usb_xfer(handle, h->databuf, len);
1100 if (res != ERROR_OK)
1101 return res;
1103 memcpy(buffer, h->databuf, len);
1105 return stlink_usb_get_rw_status(handle);
1108 /** */
1109 static int stlink_usb_write_mem32(void *handle, uint32_t addr, uint16_t len,
1110 const uint8_t *buffer)
1112 int res;
1113 struct stlink_usb_handle_s *h;
1115 assert(handle != NULL);
1117 h = (struct stlink_usb_handle_s *)handle;
1119 len *= 4;
1121 stlink_usb_init_buffer(handle, STLINK_TX_EP, len);
1123 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1124 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_WRITEMEM_32BIT;
1125 h_u32_to_le(h->cmdbuf+h->cmdidx, addr);
1126 h->cmdidx += 4;
1127 h_u16_to_le(h->cmdbuf+h->cmdidx, len);
1128 h->cmdidx += 2;
1130 res = stlink_usb_xfer(handle, buffer, len);
1132 if (res != ERROR_OK)
1133 return res;
1135 return stlink_usb_get_rw_status(handle);
1138 /** */
1139 static int stlink_usb_open(struct stlink_interface_param_s *param, void **fd)
1141 int err;
1142 struct stlink_usb_handle_s *h;
1143 enum stlink_jtag_api_version api;
1145 LOG_DEBUG("stlink_usb_open");
1147 h = malloc(sizeof(struct stlink_usb_handle_s));
1149 if (h == 0) {
1150 LOG_DEBUG("malloc failed");
1151 return ERROR_FAIL;
1154 h->transport = param->transport;
1156 const uint16_t vids[] = { param->vid, 0 };
1157 const uint16_t pids[] = { param->pid, 0 };
1159 LOG_DEBUG("transport: %d vid: 0x%04x pid: 0x%04x", param->transport,
1160 param->vid, param->pid);
1162 if (jtag_libusb_open(vids, pids, &h->fd) != ERROR_OK) {
1163 LOG_ERROR("open failed");
1164 return ERROR_FAIL;
1167 jtag_libusb_set_configuration(h->fd, 0);
1169 if (jtag_libusb_claim_interface(h->fd, 0) != ERROR_OK) {
1170 LOG_DEBUG("claim interface failed");
1171 return ERROR_FAIL;
1174 /* wrap version for first read */
1175 switch (param->pid) {
1176 case 0x3744:
1177 h->version.stlink = 1;
1178 break;
1179 default:
1180 h->version.stlink = 2;
1181 break;
1184 /* get the device version */
1185 err = stlink_usb_version(h);
1187 if (err != ERROR_OK) {
1188 LOG_ERROR("read version failed");
1189 jtag_libusb_close(h->fd);
1190 free(h);
1191 return err;
1194 /* compare usb vid/pid */
1195 if ((param->vid != h->vid) || (param->pid != h->pid))
1196 LOG_INFO("vid/pid are not identical: 0x%04X/0x%04X 0x%04X/0x%04X",
1197 param->vid, param->pid,
1198 h->vid, h->pid);
1200 /* check if mode is supported */
1201 err = ERROR_OK;
1203 switch (h->transport) {
1204 case STLINK_TRANSPORT_SWD:
1205 case STLINK_TRANSPORT_JTAG:
1206 if (h->version.jtag == 0)
1207 err = ERROR_FAIL;
1208 break;
1209 case STLINK_TRANSPORT_SWIM:
1210 if (h->version.swim == 0)
1211 err = ERROR_FAIL;
1212 break;
1213 default:
1214 err = ERROR_FAIL;
1215 break;
1218 if (err != ERROR_OK) {
1219 LOG_ERROR("mode (transport) not supported by device");
1220 jtag_libusb_close(h->fd);
1221 free(h);
1222 return err;
1225 api = h->version.jtag_api_max;
1227 /* check that user has not requested certain api version
1228 * and if they have check it is supported */
1229 if ((param->api != 0) && (param->api <= h->version.jtag_api_max)) {
1230 api = param->api;
1231 LOG_INFO("using stlink api v%d", api);
1234 /* set the used jtag api, this will default to the newest supported version */
1235 h->jtag_api = api;
1237 /* initialize the debug hardware */
1238 err = stlink_usb_init_mode(h);
1240 if (err != ERROR_OK) {
1241 LOG_ERROR("init mode failed");
1242 jtag_libusb_close(h->fd);
1243 free(h);
1244 return err;
1247 *fd = h;
1249 return ERROR_OK;
1252 /** */
1253 static int stlink_usb_close(void *fd)
1255 return ERROR_OK;
1258 /** */
1259 struct stlink_layout_api_s stlink_usb_layout_api = {
1260 /** */
1261 .open = stlink_usb_open,
1262 /** */
1263 .close = stlink_usb_close,
1264 /** */
1265 .idcode = stlink_usb_idcode,
1266 /** */
1267 .state = stlink_usb_state,
1268 /** */
1269 .reset = stlink_usb_reset,
1270 /** */
1271 .assert_srst = stlink_usb_assert_srst,
1272 /** */
1273 .run = stlink_usb_run,
1274 /** */
1275 .halt = stlink_usb_halt,
1276 /** */
1277 .step = stlink_usb_step,
1278 /** */
1279 .read_regs = stlink_usb_read_regs,
1280 /** */
1281 .read_reg = stlink_usb_read_reg,
1282 /** */
1283 .write_reg = stlink_usb_write_reg,
1284 /** */
1285 .read_mem8 = stlink_usb_read_mem8,
1286 /** */
1287 .write_mem8 = stlink_usb_write_mem8,
1288 /** */
1289 .read_mem32 = stlink_usb_read_mem32,
1290 /** */
1291 .write_mem32 = stlink_usb_write_mem32,
1292 /** */
1293 .write_debug_reg = stlink_usb_write_debug_reg