72b31c96a6e65a7a0bc1fed6b7ba6e4dd5eb34ee
[openocd.git] / src / jtag / drivers / stlink_usb.c
blob72b31c96a6e65a7a0bc1fed6b7ba6e4dd5eb34ee
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
110 #define STLINK_GET_TARGET_VOLTAGE 0xF7
112 #define STLINK_DEV_DFU_MODE 0x00
113 #define STLINK_DEV_MASS_MODE 0x01
114 #define STLINK_DEV_DEBUG_MODE 0x02
115 #define STLINK_DEV_SWIM_MODE 0x03
116 #define STLINK_DEV_BOOTLOADER_MODE 0x04
117 #define STLINK_DEV_UNKNOWN_MODE -1
119 #define STLINK_DFU_EXIT 0x07
121 #define STLINK_SWIM_ENTER 0x00
122 #define STLINK_SWIM_EXIT 0x01
124 #define STLINK_DEBUG_ENTER_JTAG 0x00
125 #define STLINK_DEBUG_GETSTATUS 0x01
126 #define STLINK_DEBUG_FORCEDEBUG 0x02
127 #define STLINK_DEBUG_APIV1_RESETSYS 0x03
128 #define STLINK_DEBUG_APIV1_READALLREGS 0x04
129 #define STLINK_DEBUG_APIV1_READREG 0x05
130 #define STLINK_DEBUG_APIV1_WRITEREG 0x06
131 #define STLINK_DEBUG_READMEM_32BIT 0x07
132 #define STLINK_DEBUG_WRITEMEM_32BIT 0x08
133 #define STLINK_DEBUG_RUNCORE 0x09
134 #define STLINK_DEBUG_STEPCORE 0x0a
135 #define STLINK_DEBUG_APIV1_SETFP 0x0b
136 #define STLINK_DEBUG_READMEM_8BIT 0x0c
137 #define STLINK_DEBUG_WRITEMEM_8BIT 0x0d
138 #define STLINK_DEBUG_APIV1_CLEARFP 0x0e
139 #define STLINK_DEBUG_APIV1_WRITEDEBUGREG 0x0f
140 #define STLINK_DEBUG_APIV1_SETWATCHPOINT 0x10
142 #define STLINK_DEBUG_ENTER_JTAG 0x00
143 #define STLINK_DEBUG_ENTER_SWD 0xa3
145 #define STLINK_DEBUG_APIV1_ENTER 0x20
146 #define STLINK_DEBUG_EXIT 0x21
147 #define STLINK_DEBUG_READCOREID 0x22
149 #define STLINK_DEBUG_APIV2_ENTER 0x30
150 #define STLINK_DEBUG_APIV2_READ_IDCODES 0x31
151 #define STLINK_DEBUG_APIV2_RESETSYS 0x32
152 #define STLINK_DEBUG_APIV2_READREG 0x33
153 #define STLINK_DEBUG_APIV2_WRITEREG 0x34
154 #define STLINK_DEBUG_APIV2_WRITEDEBUGREG 0x35
155 #define STLINK_DEBUG_APIV2_READDEBUGREG 0x36
157 #define STLINK_DEBUG_APIV2_READALLREGS 0x3A
158 #define STLINK_DEBUG_APIV2_GETLASTRWSTATUS 0x3B
159 #define STLINK_DEBUG_APIV2_DRIVE_NRST 0x3C
161 #define STLINK_DEBUG_APIV2_DRIVE_NRST_LOW 0x00
162 #define STLINK_DEBUG_APIV2_DRIVE_NRST_HIGH 0x01
163 #define STLINK_DEBUG_APIV2_DRIVE_NRST_PULSE 0x02
165 /** */
166 enum stlink_mode {
167 STLINK_MODE_UNKNOWN = 0,
168 STLINK_MODE_DFU,
169 STLINK_MODE_MASS,
170 STLINK_MODE_DEBUG_JTAG,
171 STLINK_MODE_DEBUG_SWD,
172 STLINK_MODE_DEBUG_SWIM
175 #define REQUEST_SENSE 0x03
176 #define REQUEST_SENSE_LENGTH 18
178 static void stlink_usb_init_buffer(void *handle, uint8_t direction, uint32_t size);
180 /** */
181 static int stlink_usb_xfer_v1_get_status(void *handle)
183 struct stlink_usb_handle_s *h;
185 assert(handle != NULL);
187 h = (struct stlink_usb_handle_s *)handle;
189 /* read status */
190 memset(h->cmdbuf, 0, STLINK_SG_SIZE);
192 if (jtag_libusb_bulk_read(h->fd, STLINK_RX_EP, (char *)h->cmdbuf,
193 13, 1000) != 13)
194 return ERROR_FAIL;
196 uint32_t t1;
198 t1 = buf_get_u32(h->cmdbuf, 0, 32);
200 /* check for USBS */
201 if (t1 != 0x53425355)
202 return ERROR_FAIL;
204 * CSW status:
205 * 0 success
206 * 1 command failure
207 * 2 phase error
209 if (h->cmdbuf[12] != 0)
210 return ERROR_FAIL;
212 return ERROR_OK;
215 /** */
216 static int stlink_usb_xfer_rw(void *handle, int cmdsize, const uint8_t *buf, int size)
218 struct stlink_usb_handle_s *h;
220 assert(handle != NULL);
222 h = (struct stlink_usb_handle_s *)handle;
224 if (jtag_libusb_bulk_write(h->fd, STLINK_TX_EP, (char *)h->cmdbuf, cmdsize,
225 1000) != cmdsize) {
226 return ERROR_FAIL;
229 if (h->direction == STLINK_TX_EP && size) {
230 if (jtag_libusb_bulk_write(h->fd, STLINK_TX_EP, (char *)buf,
231 size, 1000) != size) {
232 LOG_DEBUG("bulk write failed");
233 return ERROR_FAIL;
235 } else if (h->direction == STLINK_RX_EP && size) {
236 if (jtag_libusb_bulk_read(h->fd, STLINK_RX_EP, (char *)buf,
237 size, 1000) != size) {
238 LOG_DEBUG("bulk read failed");
239 return ERROR_FAIL;
243 return ERROR_OK;
246 /** */
247 static int stlink_usb_xfer_v1_get_sense(void *handle)
249 int res;
250 struct stlink_usb_handle_s *h;
252 assert(handle != NULL);
254 h = (struct stlink_usb_handle_s *)handle;
256 stlink_usb_init_buffer(handle, STLINK_RX_EP, 16);
258 h->cmdbuf[h->cmdidx++] = REQUEST_SENSE;
259 h->cmdbuf[h->cmdidx++] = 0;
260 h->cmdbuf[h->cmdidx++] = 0;
261 h->cmdbuf[h->cmdidx++] = 0;
262 h->cmdbuf[h->cmdidx++] = REQUEST_SENSE_LENGTH;
264 res = stlink_usb_xfer_rw(handle, REQUEST_SENSE_LENGTH, h->databuf, 16);
266 if (res != ERROR_OK)
267 return res;
269 if (stlink_usb_xfer_v1_get_status(handle) != ERROR_OK)
270 return ERROR_FAIL;
272 return ERROR_OK;
275 /** */
276 static int stlink_usb_xfer(void *handle, const uint8_t *buf, int size)
278 int err, cmdsize = STLINK_CMD_SIZE_V2;
279 struct stlink_usb_handle_s *h;
281 assert(handle != NULL);
283 h = (struct stlink_usb_handle_s *)handle;
285 if (h->version.stlink == 1)
286 cmdsize = STLINK_SG_SIZE;
288 err = stlink_usb_xfer_rw(handle, cmdsize, buf, size);
290 if (err != ERROR_OK)
291 return err;
293 if (h->version.stlink == 1) {
294 if (stlink_usb_xfer_v1_get_status(handle) != ERROR_OK) {
295 /* check csw status */
296 if (h->cmdbuf[12] == 1) {
297 LOG_DEBUG("get sense");
298 if (stlink_usb_xfer_v1_get_sense(handle) != ERROR_OK)
299 return ERROR_FAIL;
301 return ERROR_FAIL;
305 return ERROR_OK;
308 /** */
309 static void stlink_usb_xfer_v1_create_cmd(void *handle, uint8_t direction, uint32_t size)
311 struct stlink_usb_handle_s *h;
313 h = (struct stlink_usb_handle_s *)handle;
315 /* fill the send buffer */
316 strcpy((char *)h->cmdbuf, "USBC");
317 h->cmdidx += 4;
318 /* csw tag not used */
319 h->cmdidx += 4;
320 buf_set_u32(h->cmdbuf+h->cmdidx, 0, 32, size);
321 h->cmdidx += 4;
322 h->cmdbuf[h->cmdidx++] = (direction == STLINK_RX_EP ? ENDPOINT_IN : ENDPOINT_OUT);
323 h->cmdbuf[h->cmdidx++] = 0; /* lun */
324 h->cmdbuf[h->cmdidx++] = STLINK_CMD_SIZE_V1;
327 /** */
328 static void stlink_usb_init_buffer(void *handle, uint8_t direction, uint32_t size)
330 struct stlink_usb_handle_s *h;
332 h = (struct stlink_usb_handle_s *)handle;
334 h->direction = direction;
336 h->cmdidx = 0;
338 memset(h->cmdbuf, 0, STLINK_SG_SIZE);
339 memset(h->databuf, 0, STLINK_DATA_SIZE);
341 if (h->version.stlink == 1)
342 stlink_usb_xfer_v1_create_cmd(handle, direction, size);
345 static const char * const stlink_usb_error_msg[] = {
346 "unknown"
349 /** */
350 static int stlink_usb_error_check(void *handle)
352 int res;
353 const char *err_msg = 0;
354 struct stlink_usb_handle_s *h;
356 assert(handle != NULL);
358 h = (struct stlink_usb_handle_s *)handle;
360 /* TODO: no error checking yet on api V1 */
361 if (h->jtag_api == STLINK_JTAG_API_V1)
362 h->databuf[0] = STLINK_DEBUG_ERR_OK;
364 switch (h->databuf[0]) {
365 case STLINK_DEBUG_ERR_OK:
366 res = ERROR_OK;
367 break;
368 case STLINK_DEBUG_ERR_FAULT:
369 default:
370 err_msg = stlink_usb_error_msg[0];
371 res = ERROR_FAIL;
372 break;
375 if (res != ERROR_OK)
376 LOG_DEBUG("status error: %d ('%s')", h->databuf[0], err_msg);
378 return res;
381 /** */
382 static int stlink_usb_version(void *handle)
384 int res;
385 uint16_t v;
386 struct stlink_usb_handle_s *h;
388 assert(handle != NULL);
390 h = (struct stlink_usb_handle_s *)handle;
392 stlink_usb_init_buffer(handle, STLINK_RX_EP, 6);
394 h->cmdbuf[h->cmdidx++] = STLINK_GET_VERSION;
396 res = stlink_usb_xfer(handle, h->databuf, 6);
398 if (res != ERROR_OK)
399 return res;
401 v = (h->databuf[0] << 8) | h->databuf[1];
403 h->version.stlink = (v >> 12) & 0x0f;
404 h->version.jtag = (v >> 6) & 0x3f;
405 h->version.swim = v & 0x3f;
406 h->vid = buf_get_u32(h->databuf, 16, 16);
407 h->pid = buf_get_u32(h->databuf, 32, 16);
409 /* set the supported jtag api version
410 * API V2 is supported since JTAG V11
412 if (h->version.jtag >= 11)
413 h->version.jtag_api_max = STLINK_JTAG_API_V2;
414 else
415 h->version.jtag_api_max = STLINK_JTAG_API_V1;
417 LOG_INFO("STLINK v%d JTAG v%d API v%d SWIM v%d VID 0x%04X PID 0x%04X",
418 h->version.stlink,
419 h->version.jtag,
420 (h->version.jtag_api_max == STLINK_JTAG_API_V1) ? 1 : 2,
421 h->version.swim,
422 h->vid,
423 h->pid);
425 return ERROR_OK;
428 static int stlink_usb_check_voltage(void *handle, float *target_voltage)
430 struct stlink_usb_handle_s *h;
431 uint32_t adc_results[2];
433 h = (struct stlink_usb_handle_s *)handle;
435 /* only supported by stlink/v2 and for firmware >= 13 */
436 if (h->version.stlink == 1 || h->version.jtag < 13)
437 return ERROR_COMMAND_NOTFOUND;
439 stlink_usb_init_buffer(handle, STLINK_RX_EP, 8);
441 h->cmdbuf[h->cmdidx++] = STLINK_GET_TARGET_VOLTAGE;
443 int result = stlink_usb_xfer(handle, h->databuf, 8);
445 if (result != ERROR_OK)
446 return result;
448 /* convert result */
449 adc_results[0] = le_to_h_u32(h->databuf);
450 adc_results[1] = le_to_h_u32(h->databuf + 4);
452 *target_voltage = 0;
454 if (adc_results[0])
455 *target_voltage = 2 * ((float)adc_results[1]) * (float)(1.2 / adc_results[0]);
457 LOG_INFO("Target voltage: %f", (double)*target_voltage);
459 return ERROR_OK;
462 /** */
463 static int stlink_usb_current_mode(void *handle, uint8_t *mode)
465 int res;
466 struct stlink_usb_handle_s *h;
468 assert(handle != NULL);
470 h = (struct stlink_usb_handle_s *)handle;
472 stlink_usb_init_buffer(handle, STLINK_RX_EP, 2);
474 h->cmdbuf[h->cmdidx++] = STLINK_GET_CURRENT_MODE;
476 res = stlink_usb_xfer(handle, h->databuf, 2);
478 if (res != ERROR_OK)
479 return res;
481 *mode = h->databuf[0];
483 return ERROR_OK;
486 /** */
487 static int stlink_usb_mode_enter(void *handle, enum stlink_mode type)
489 int res;
490 int rx_size = 0;
491 struct stlink_usb_handle_s *h;
493 assert(handle != NULL);
495 h = (struct stlink_usb_handle_s *)handle;
497 /* on api V2 we are able the read the latest command
498 * status
499 * TODO: we need the test on api V1 too
501 if (h->jtag_api == STLINK_JTAG_API_V2)
502 rx_size = 2;
504 stlink_usb_init_buffer(handle, STLINK_RX_EP, rx_size);
506 switch (type) {
507 case STLINK_MODE_DEBUG_JTAG:
508 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
509 if (h->jtag_api == STLINK_JTAG_API_V1)
510 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_ENTER;
511 else
512 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_ENTER;
513 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_ENTER_JTAG;
514 break;
515 case STLINK_MODE_DEBUG_SWD:
516 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
517 if (h->jtag_api == STLINK_JTAG_API_V1)
518 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_ENTER;
519 else
520 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_ENTER;
521 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_ENTER_SWD;
522 break;
523 case STLINK_MODE_DEBUG_SWIM:
524 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_COMMAND;
525 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_ENTER;
526 break;
527 case STLINK_MODE_DFU:
528 case STLINK_MODE_MASS:
529 default:
530 return ERROR_FAIL;
533 res = stlink_usb_xfer(handle, h->databuf, rx_size);
535 if (res != ERROR_OK)
536 return res;
538 res = stlink_usb_error_check(h);
540 return res;
543 /** */
544 static int stlink_usb_mode_leave(void *handle, enum stlink_mode type)
546 int res;
547 struct stlink_usb_handle_s *h;
549 assert(handle != NULL);
551 h = (struct stlink_usb_handle_s *)handle;
553 stlink_usb_init_buffer(handle, STLINK_NULL_EP, 0);
555 switch (type) {
556 case STLINK_MODE_DEBUG_JTAG:
557 case STLINK_MODE_DEBUG_SWD:
558 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
559 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_EXIT;
560 break;
561 case STLINK_MODE_DEBUG_SWIM:
562 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_COMMAND;
563 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_EXIT;
564 break;
565 case STLINK_MODE_DFU:
566 h->cmdbuf[h->cmdidx++] = STLINK_DFU_COMMAND;
567 h->cmdbuf[h->cmdidx++] = STLINK_DFU_EXIT;
568 break;
569 case STLINK_MODE_MASS:
570 default:
571 return ERROR_FAIL;
574 res = stlink_usb_xfer(handle, 0, 0);
576 if (res != ERROR_OK)
577 return res;
579 return ERROR_OK;
582 static int stlink_usb_assert_srst(void *handle, int srst);
584 /** */
585 static int stlink_usb_init_mode(void *handle, bool connect_under_reset)
587 int res;
588 uint8_t mode;
589 enum stlink_mode emode;
590 struct stlink_usb_handle_s *h;
592 assert(handle != NULL);
594 h = (struct stlink_usb_handle_s *)handle;
596 res = stlink_usb_current_mode(handle, &mode);
598 if (res != ERROR_OK)
599 return res;
601 LOG_DEBUG("MODE: 0x%02X", mode);
603 /* try to exit current mode */
604 switch (mode) {
605 case STLINK_DEV_DFU_MODE:
606 emode = STLINK_MODE_DFU;
607 break;
608 case STLINK_DEV_DEBUG_MODE:
609 emode = STLINK_MODE_DEBUG_SWD;
610 break;
611 case STLINK_DEV_SWIM_MODE:
612 emode = STLINK_MODE_DEBUG_SWIM;
613 break;
614 case STLINK_DEV_BOOTLOADER_MODE:
615 case STLINK_DEV_MASS_MODE:
616 default:
617 emode = STLINK_MODE_UNKNOWN;
618 break;
621 if (emode != STLINK_MODE_UNKNOWN) {
622 res = stlink_usb_mode_leave(handle, emode);
624 if (res != ERROR_OK)
625 return res;
628 res = stlink_usb_current_mode(handle, &mode);
630 if (res != ERROR_OK)
631 return res;
633 /* we check the target voltage here as an aid to debugging connection problems.
634 * the stlink requires the target Vdd to be connected for reliable debugging.
635 * this cmd is supported in all modes except DFU
637 if (mode != STLINK_DEV_DFU_MODE) {
639 float target_voltage;
641 /* check target voltage (if supported) */
642 res = stlink_usb_check_voltage(h, &target_voltage);
644 if (res != ERROR_OK) {
645 if (res != ERROR_COMMAND_NOTFOUND)
646 LOG_ERROR("voltage check failed");
647 /* attempt to continue as it is not a catastrophic failure */
648 } else {
649 /* check for a sensible target voltage, operating range is 1.65-5.5v
650 * according to datasheet */
651 if (target_voltage < 1.5)
652 LOG_ERROR("target voltage may be too low for reliable debugging");
656 LOG_DEBUG("MODE: 0x%02X", mode);
658 /* set selected mode */
659 switch (h->transport) {
660 case HL_TRANSPORT_SWD:
661 emode = STLINK_MODE_DEBUG_SWD;
662 break;
663 case HL_TRANSPORT_JTAG:
664 emode = STLINK_MODE_DEBUG_JTAG;
665 break;
666 case HL_TRANSPORT_SWIM:
667 emode = STLINK_MODE_DEBUG_SWIM;
668 break;
669 default:
670 emode = STLINK_MODE_UNKNOWN;
671 break;
674 if (emode == STLINK_MODE_UNKNOWN) {
675 LOG_ERROR("selected mode (transport) not supported");
676 return ERROR_FAIL;
679 if (connect_under_reset) {
680 res = stlink_usb_assert_srst(handle, 0);
681 if (res != ERROR_OK)
682 return res;
685 res = stlink_usb_mode_enter(handle, emode);
687 if (res != ERROR_OK)
688 return res;
690 res = stlink_usb_current_mode(handle, &mode);
692 if (res != ERROR_OK)
693 return res;
695 LOG_DEBUG("MODE: 0x%02X", mode);
697 return ERROR_OK;
700 /** */
701 static int stlink_usb_idcode(void *handle, uint32_t *idcode)
703 int res;
704 struct stlink_usb_handle_s *h;
706 assert(handle != NULL);
708 h = (struct stlink_usb_handle_s *)handle;
710 stlink_usb_init_buffer(handle, STLINK_RX_EP, 4);
712 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
713 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_READCOREID;
715 res = stlink_usb_xfer(handle, h->databuf, 4);
717 if (res != ERROR_OK)
718 return res;
720 *idcode = le_to_h_u32(h->databuf);
722 LOG_DEBUG("IDCODE: 0x%08X", *idcode);
724 return ERROR_OK;
727 static int stlink_usb_v2_read_debug_reg(void *handle, uint32_t addr, uint32_t *val)
729 struct stlink_usb_handle_s *h;
730 int res;
732 assert(handle != NULL);
734 h = (struct stlink_usb_handle_s *)handle;
736 stlink_usb_init_buffer(handle, STLINK_RX_EP, 8);
738 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
739 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_READDEBUGREG;
740 h_u32_to_le(h->cmdbuf+h->cmdidx, addr);
741 h->cmdidx += 4;
743 res = stlink_usb_xfer(handle, h->databuf, 8);
745 if (res != ERROR_OK)
746 return res;
748 *val = le_to_h_u32(h->databuf + 4);
750 return h->databuf[0] == STLINK_DEBUG_ERR_OK ? ERROR_OK : ERROR_FAIL;
753 static int stlink_usb_write_debug_reg(void *handle, uint32_t addr, uint32_t val)
755 int res;
756 struct stlink_usb_handle_s *h;
758 assert(handle != NULL);
760 h = (struct stlink_usb_handle_s *)handle;
762 stlink_usb_init_buffer(handle, STLINK_RX_EP, 2);
764 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
765 if (h->jtag_api == STLINK_JTAG_API_V1)
766 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_WRITEDEBUGREG;
767 else
768 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_WRITEDEBUGREG;
769 h_u32_to_le(h->cmdbuf+h->cmdidx, addr);
770 h->cmdidx += 4;
771 h_u32_to_le(h->cmdbuf+h->cmdidx, val);
772 h->cmdidx += 4;
774 res = stlink_usb_xfer(handle, h->databuf, 2);
776 if (res != ERROR_OK)
777 return res;
779 return h->databuf[0] == STLINK_DEBUG_ERR_OK ? ERROR_OK : ERROR_FAIL;
782 static enum target_state stlink_usb_v2_get_status(void *handle)
784 int result;
785 uint32_t status;
787 result = stlink_usb_v2_read_debug_reg(handle, DCB_DHCSR, &status);
788 if (result != ERROR_OK)
789 return TARGET_UNKNOWN;
791 if (status & S_HALT)
792 return TARGET_HALTED;
793 else if (status & S_RESET_ST)
794 return TARGET_RESET;
796 return TARGET_RUNNING;
799 /** */
800 static enum target_state stlink_usb_state(void *handle)
802 int res;
803 struct stlink_usb_handle_s *h;
805 assert(handle != NULL);
807 h = (struct stlink_usb_handle_s *)handle;
809 if (h->jtag_api == STLINK_JTAG_API_V2)
810 return stlink_usb_v2_get_status(handle);
812 stlink_usb_init_buffer(handle, STLINK_RX_EP, 2);
814 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
815 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_GETSTATUS;
817 res = stlink_usb_xfer(handle, h->databuf, 2);
819 if (res != ERROR_OK)
820 return TARGET_UNKNOWN;
822 if (h->databuf[0] == STLINK_CORE_RUNNING)
823 return TARGET_RUNNING;
824 if (h->databuf[0] == STLINK_CORE_HALTED)
825 return TARGET_HALTED;
827 return TARGET_UNKNOWN;
830 /** */
831 static int stlink_usb_reset(void *handle)
833 int res;
834 struct stlink_usb_handle_s *h;
836 assert(handle != NULL);
838 h = (struct stlink_usb_handle_s *)handle;
840 stlink_usb_init_buffer(handle, STLINK_RX_EP, 2);
842 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
844 if (h->jtag_api == STLINK_JTAG_API_V1)
845 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_RESETSYS;
846 else
847 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_RESETSYS;
849 res = stlink_usb_xfer(handle, h->databuf, 2);
851 if (res != ERROR_OK)
852 return res;
854 LOG_DEBUG("RESET: 0x%08X", h->databuf[0]);
856 /* the following is not a error under swd (using hardware srst), so return success */
857 if (h->databuf[0] == STLINK_SWD_AP_WAIT || h->databuf[0] == STLINK_SWD_DP_WAIT)
858 return ERROR_OK;
860 return h->databuf[0] == STLINK_DEBUG_ERR_OK ? ERROR_OK : ERROR_FAIL;
863 static int stlink_usb_assert_srst(void *handle, int srst)
865 int res;
866 struct stlink_usb_handle_s *h;
868 assert(handle != NULL);
870 h = (struct stlink_usb_handle_s *)handle;
872 if (h->jtag_api == STLINK_JTAG_API_V1)
873 return ERROR_COMMAND_NOTFOUND;
875 stlink_usb_init_buffer(handle, STLINK_RX_EP, 2);
877 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
878 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_DRIVE_NRST;
879 h->cmdbuf[h->cmdidx++] = srst;
881 res = stlink_usb_xfer(handle, h->databuf, 2);
883 if (res != ERROR_OK)
884 return res;
886 return h->databuf[0] == STLINK_DEBUG_ERR_OK ? ERROR_OK : ERROR_FAIL;
889 /** */
890 static int stlink_usb_run(void *handle)
892 int res;
893 struct stlink_usb_handle_s *h;
895 assert(handle != NULL);
897 h = (struct stlink_usb_handle_s *)handle;
899 if (h->jtag_api == STLINK_JTAG_API_V2)
900 return stlink_usb_write_debug_reg(handle, DCB_DHCSR, DBGKEY|C_DEBUGEN);
902 stlink_usb_init_buffer(handle, STLINK_RX_EP, 2);
904 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
905 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_RUNCORE;
907 res = stlink_usb_xfer(handle, h->databuf, 2);
909 if (res != ERROR_OK)
910 return res;
912 return h->databuf[0] == STLINK_DEBUG_ERR_OK ? ERROR_OK : ERROR_FAIL;
915 /** */
916 static int stlink_usb_halt(void *handle)
918 int res;
919 struct stlink_usb_handle_s *h;
921 assert(handle != NULL);
923 h = (struct stlink_usb_handle_s *)handle;
925 if (h->jtag_api == STLINK_JTAG_API_V2)
926 return stlink_usb_write_debug_reg(handle, DCB_DHCSR, DBGKEY|C_HALT|C_DEBUGEN);
928 stlink_usb_init_buffer(handle, STLINK_RX_EP, 2);
930 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
931 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_FORCEDEBUG;
933 res = stlink_usb_xfer(handle, h->databuf, 2);
935 if (res != ERROR_OK)
936 return res;
938 return h->databuf[0] == STLINK_DEBUG_ERR_OK ? ERROR_OK : ERROR_FAIL;
941 /** */
942 static int stlink_usb_step(void *handle)
944 int res;
945 struct stlink_usb_handle_s *h;
947 assert(handle != NULL);
949 h = (struct stlink_usb_handle_s *)handle;
951 if (h->jtag_api == STLINK_JTAG_API_V2) {
952 /* TODO: this emulates the v1 api, it should really use a similar auto mask isr
953 * that the cortex-m3 currently does. */
954 stlink_usb_write_debug_reg(handle, DCB_DHCSR, DBGKEY|C_HALT|C_MASKINTS|C_DEBUGEN);
955 stlink_usb_write_debug_reg(handle, DCB_DHCSR, DBGKEY|C_STEP|C_MASKINTS|C_DEBUGEN);
956 return stlink_usb_write_debug_reg(handle, DCB_DHCSR, DBGKEY|C_HALT|C_DEBUGEN);
959 stlink_usb_init_buffer(handle, STLINK_RX_EP, 2);
961 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
962 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_STEPCORE;
964 res = stlink_usb_xfer(handle, h->databuf, 2);
966 if (res != ERROR_OK)
967 return res;
969 return h->databuf[0] == STLINK_DEBUG_ERR_OK ? ERROR_OK : ERROR_FAIL;
972 /** */
973 static int stlink_usb_read_regs(void *handle)
975 int res;
976 struct stlink_usb_handle_s *h;
978 assert(handle != NULL);
980 h = (struct stlink_usb_handle_s *)handle;
982 stlink_usb_init_buffer(handle, STLINK_RX_EP, 84);
984 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
985 if (h->jtag_api == STLINK_JTAG_API_V1)
986 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_READALLREGS;
987 else
988 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_READALLREGS;
990 res = stlink_usb_xfer(handle, h->databuf, 84);
992 if (res != ERROR_OK)
993 return res;
995 return ERROR_OK;
998 /** */
999 static int stlink_usb_read_reg(void *handle, int num, uint32_t *val)
1001 int res;
1002 struct stlink_usb_handle_s *h;
1004 assert(handle != NULL);
1006 h = (struct stlink_usb_handle_s *)handle;
1008 stlink_usb_init_buffer(handle, STLINK_RX_EP, h->jtag_api == STLINK_JTAG_API_V1 ? 4 : 8);
1010 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1011 if (h->jtag_api == STLINK_JTAG_API_V1)
1012 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_READREG;
1013 else
1014 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_READREG;
1015 h->cmdbuf[h->cmdidx++] = num;
1017 res = stlink_usb_xfer(handle, h->databuf, h->jtag_api == STLINK_JTAG_API_V1 ? 4 : 8);
1019 if (res != ERROR_OK)
1020 return res;
1022 if (h->jtag_api == STLINK_JTAG_API_V1)
1023 *val = le_to_h_u32(h->databuf);
1024 else {
1025 *val = le_to_h_u32(h->databuf + 4);
1026 return h->databuf[0] == STLINK_DEBUG_ERR_OK ? ERROR_OK : ERROR_FAIL;
1029 return ERROR_OK;
1032 /** */
1033 static int stlink_usb_write_reg(void *handle, int num, uint32_t val)
1035 int res;
1036 struct stlink_usb_handle_s *h;
1038 assert(handle != NULL);
1040 h = (struct stlink_usb_handle_s *)handle;
1042 stlink_usb_init_buffer(handle, STLINK_RX_EP, 2);
1044 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1045 if (h->jtag_api == STLINK_JTAG_API_V1)
1046 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_WRITEREG;
1047 else
1048 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_WRITEREG;
1049 h->cmdbuf[h->cmdidx++] = num;
1050 h_u32_to_le(h->cmdbuf+h->cmdidx, val);
1051 h->cmdidx += 4;
1053 res = stlink_usb_xfer(handle, h->databuf, 2);
1055 if (res != ERROR_OK)
1056 return res;
1058 return h->databuf[0] == STLINK_DEBUG_ERR_OK ? ERROR_OK : ERROR_FAIL;
1061 static int stlink_usb_get_rw_status(void *handle)
1063 int res;
1064 struct stlink_usb_handle_s *h;
1066 assert(handle != NULL);
1068 h = (struct stlink_usb_handle_s *)handle;
1070 if (h->jtag_api == STLINK_JTAG_API_V1)
1071 return ERROR_OK;
1073 stlink_usb_init_buffer(handle, STLINK_RX_EP, 2);
1075 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1076 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_GETLASTRWSTATUS;
1078 res = stlink_usb_xfer(handle, h->databuf, 2);
1080 if (res != ERROR_OK)
1081 return res;
1083 return h->databuf[0] == STLINK_DEBUG_ERR_OK ? ERROR_OK : res;
1086 /** */
1087 static int stlink_usb_read_mem8(void *handle, uint32_t addr, uint16_t len,
1088 uint8_t *buffer)
1090 int res;
1091 uint16_t read_len = len;
1092 struct stlink_usb_handle_s *h;
1094 assert(handle != NULL);
1096 h = (struct stlink_usb_handle_s *)handle;
1098 stlink_usb_init_buffer(handle, STLINK_RX_EP, read_len);
1100 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1101 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_READMEM_8BIT;
1102 h_u32_to_le(h->cmdbuf+h->cmdidx, addr);
1103 h->cmdidx += 4;
1104 h_u16_to_le(h->cmdbuf+h->cmdidx, len);
1105 h->cmdidx += 2;
1107 /* we need to fix read length for single bytes */
1108 if (read_len == 1)
1109 read_len++;
1111 res = stlink_usb_xfer(handle, h->databuf, read_len);
1113 if (res != ERROR_OK)
1114 return res;
1116 memcpy(buffer, h->databuf, len);
1118 return stlink_usb_get_rw_status(handle);
1121 /** */
1122 static int stlink_usb_write_mem8(void *handle, uint32_t addr, uint16_t len,
1123 const uint8_t *buffer)
1125 int res;
1126 struct stlink_usb_handle_s *h;
1128 assert(handle != NULL);
1130 h = (struct stlink_usb_handle_s *)handle;
1132 stlink_usb_init_buffer(handle, STLINK_TX_EP, len);
1134 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1135 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_WRITEMEM_8BIT;
1136 h_u32_to_le(h->cmdbuf+h->cmdidx, addr);
1137 h->cmdidx += 4;
1138 h_u16_to_le(h->cmdbuf+h->cmdidx, len);
1139 h->cmdidx += 2;
1141 res = stlink_usb_xfer(handle, buffer, len);
1143 if (res != ERROR_OK)
1144 return res;
1146 return stlink_usb_get_rw_status(handle);
1149 /** */
1150 static int stlink_usb_read_mem32(void *handle, uint32_t addr, uint16_t len,
1151 uint8_t *buffer)
1153 int res;
1154 struct stlink_usb_handle_s *h;
1156 assert(handle != NULL);
1158 h = (struct stlink_usb_handle_s *)handle;
1160 len *= 4;
1162 stlink_usb_init_buffer(handle, STLINK_RX_EP, len);
1164 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1165 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_READMEM_32BIT;
1166 h_u32_to_le(h->cmdbuf+h->cmdidx, addr);
1167 h->cmdidx += 4;
1168 h_u16_to_le(h->cmdbuf+h->cmdidx, len);
1169 h->cmdidx += 2;
1171 res = stlink_usb_xfer(handle, h->databuf, len);
1173 if (res != ERROR_OK)
1174 return res;
1176 memcpy(buffer, h->databuf, len);
1178 return stlink_usb_get_rw_status(handle);
1181 /** */
1182 static int stlink_usb_write_mem32(void *handle, uint32_t addr, uint16_t len,
1183 const uint8_t *buffer)
1185 int res;
1186 struct stlink_usb_handle_s *h;
1188 assert(handle != NULL);
1190 h = (struct stlink_usb_handle_s *)handle;
1192 len *= 4;
1194 stlink_usb_init_buffer(handle, STLINK_TX_EP, len);
1196 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1197 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_WRITEMEM_32BIT;
1198 h_u32_to_le(h->cmdbuf+h->cmdidx, addr);
1199 h->cmdidx += 4;
1200 h_u16_to_le(h->cmdbuf+h->cmdidx, len);
1201 h->cmdidx += 2;
1203 res = stlink_usb_xfer(handle, buffer, len);
1205 if (res != ERROR_OK)
1206 return res;
1208 return stlink_usb_get_rw_status(handle);
1211 /** */
1212 static int stlink_usb_close(void *fd)
1214 struct stlink_usb_handle_s *h;
1216 h = (struct stlink_usb_handle_s *)fd;
1218 if (h->fd)
1219 jtag_libusb_close(h->fd);
1221 free(fd);
1223 return ERROR_OK;
1226 /** */
1227 static int stlink_usb_open(struct hl_interface_param_s *param, void **fd)
1229 int err;
1230 struct stlink_usb_handle_s *h;
1231 enum stlink_jtag_api_version api;
1233 LOG_DEBUG("stlink_usb_open");
1235 h = calloc(1, sizeof(struct stlink_usb_handle_s));
1237 if (h == 0) {
1238 LOG_DEBUG("malloc failed");
1239 return ERROR_FAIL;
1242 h->transport = param->transport;
1244 /* set max read/write buffer size in bytes */
1245 param->max_buffer = 512;
1247 const uint16_t vids[] = { param->vid, 0 };
1248 const uint16_t pids[] = { param->pid, 0 };
1250 LOG_DEBUG("transport: %d vid: 0x%04x pid: 0x%04x", param->transport,
1251 param->vid, param->pid);
1253 if (jtag_libusb_open(vids, pids, &h->fd) != ERROR_OK) {
1254 LOG_ERROR("open failed");
1255 goto error_open;
1258 jtag_libusb_set_configuration(h->fd, 0);
1260 if (jtag_libusb_claim_interface(h->fd, 0) != ERROR_OK) {
1261 LOG_DEBUG("claim interface failed");
1262 goto error_open;
1265 /* wrap version for first read */
1266 switch (param->pid) {
1267 case 0x3744:
1268 h->version.stlink = 1;
1269 break;
1270 default:
1271 h->version.stlink = 2;
1272 break;
1275 /* get the device version */
1276 err = stlink_usb_version(h);
1278 if (err != ERROR_OK) {
1279 LOG_ERROR("read version failed");
1280 goto error_open;
1283 /* compare usb vid/pid */
1284 if ((param->vid != h->vid) || (param->pid != h->pid))
1285 LOG_INFO("vid/pid are not identical: 0x%04X/0x%04X 0x%04X/0x%04X",
1286 param->vid, param->pid,
1287 h->vid, h->pid);
1289 /* check if mode is supported */
1290 err = ERROR_OK;
1292 switch (h->transport) {
1293 case HL_TRANSPORT_SWD:
1294 case HL_TRANSPORT_JTAG:
1295 if (h->version.jtag == 0)
1296 err = ERROR_FAIL;
1297 break;
1298 case HL_TRANSPORT_SWIM:
1299 if (h->version.swim == 0)
1300 err = ERROR_FAIL;
1301 break;
1302 default:
1303 err = ERROR_FAIL;
1304 break;
1307 if (err != ERROR_OK) {
1308 LOG_ERROR("mode (transport) not supported by device");
1309 goto error_open;
1312 api = h->version.jtag_api_max;
1314 /* check that user has not requested certain api version
1315 * and if they have check it is supported */
1316 if ((param->api != 0) && (param->api <= h->version.jtag_api_max)) {
1317 api = param->api;
1318 LOG_INFO("using stlink api v%d", api);
1321 /* set the used jtag api, this will default to the newest supported version */
1322 h->jtag_api = api;
1324 /* initialize the debug hardware */
1325 err = stlink_usb_init_mode(h, param->connect_under_reset);
1327 if (err != ERROR_OK) {
1328 LOG_ERROR("init mode failed");
1329 goto error_open;
1332 *fd = h;
1334 return ERROR_OK;
1336 error_open:
1337 stlink_usb_close(h);
1339 return ERROR_FAIL;
1342 /** */
1343 struct hl_layout_api_s stlink_usb_layout_api = {
1344 /** */
1345 .open = stlink_usb_open,
1346 /** */
1347 .close = stlink_usb_close,
1348 /** */
1349 .idcode = stlink_usb_idcode,
1350 /** */
1351 .state = stlink_usb_state,
1352 /** */
1353 .reset = stlink_usb_reset,
1354 /** */
1355 .assert_srst = stlink_usb_assert_srst,
1356 /** */
1357 .run = stlink_usb_run,
1358 /** */
1359 .halt = stlink_usb_halt,
1360 /** */
1361 .step = stlink_usb_step,
1362 /** */
1363 .read_regs = stlink_usb_read_regs,
1364 /** */
1365 .read_reg = stlink_usb_read_reg,
1366 /** */
1367 .write_reg = stlink_usb_write_reg,
1368 /** */
1369 .read_mem8 = stlink_usb_read_mem8,
1370 /** */
1371 .write_mem8 = stlink_usb_write_mem8,
1372 /** */
1373 .read_mem32 = stlink_usb_read_mem32,
1374 /** */
1375 .write_mem32 = stlink_usb_write_mem32,
1376 /** */
1377 .write_debug_reg = stlink_usb_write_debug_reg