stlink: add error status check
[openocd.git] / src / jtag / drivers / stlink_usb.c
blob3cb24348884f7bca8be6a65737cff20380266507
1 /***************************************************************************
2 * Copyright (C) 2011-2012 by Mathias Kuester *
3 * Mathias Kuester <kesmtp@freenet.de> *
4 * *
5 * This code is based on https://github.com/texane/stlink *
6 * *
7 * This program is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; either version 2 of the License, or *
10 * (at your option) any later version. *
11 * *
12 * This program is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU General Public License *
18 * along with this program; if not, write to the *
19 * Free Software Foundation, Inc., *
20 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
21 ***************************************************************************/
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
27 /* project specific includes */
28 #include <helper/binarybuffer.h>
29 #include <jtag/interface.h>
30 #include <jtag/stlink/stlink_layout.h>
31 #include <jtag/stlink/stlink_transport.h>
32 #include <jtag/stlink/stlink_interface.h>
33 #include <target/target.h>
35 #include "libusb_common.h"
37 #define ENDPOINT_IN 0x80
38 #define ENDPOINT_OUT 0x00
40 #define STLINK_RX_EP (1|ENDPOINT_IN)
41 #define STLINK_TX_EP (2|ENDPOINT_OUT)
42 #define STLINK_CMD_SIZE (16)
43 #define STLINK_TX_SIZE (4*128)
44 #define STLINK_RX_SIZE (4*128)
46 enum stlink_jtag_api_version {
47 STLINK_JTAG_API_V1 = 0,
48 STLINK_JTAG_API_V2,
51 /** */
52 struct stlink_usb_version {
53 /** */
54 int stlink;
55 /** */
56 int jtag;
57 /** */
58 int swim;
59 /** highest supported jtag api version */
60 enum stlink_jtag_api_version jtag_api_max;
63 /** */
64 struct stlink_usb_handle_s {
65 /** */
66 struct jtag_libusb_device_handle *fd;
67 /** */
68 struct libusb_transfer *trans;
69 /** */
70 uint8_t txbuf[STLINK_TX_SIZE];
71 /** */
72 uint8_t rxbuf[STLINK_RX_SIZE];
73 /** */
74 enum stlink_transports transport;
75 /** */
76 struct stlink_usb_version version;
77 /** */
78 uint16_t vid;
79 /** */
80 uint16_t pid;
81 /** */
82 uint32_t sg_tag;
83 /** this is the currently used jtag api */
84 enum stlink_jtag_api_version jtag_api;
87 #define STLINK_DEBUG_ERR_OK 0x80
88 #define STLINK_DEBUG_ERR_FAULT 0x81
89 #define STLINK_CORE_RUNNING 0x80
90 #define STLINK_CORE_HALTED 0x81
91 #define STLINK_CORE_STAT_UNKNOWN -1
93 #define STLINK_GET_VERSION 0xF1
94 #define STLINK_DEBUG_COMMAND 0xF2
95 #define STLINK_DFU_COMMAND 0xF3
96 #define STLINK_SWIM_COMMAND 0xF4
97 #define STLINK_GET_CURRENT_MODE 0xF5
99 #define STLINK_DEV_DFU_MODE 0x00
100 #define STLINK_DEV_MASS_MODE 0x01
101 #define STLINK_DEV_DEBUG_MODE 0x02
102 #define STLINK_DEV_SWIM_MODE 0x03
103 #define STLINK_DEV_BOOTLOADER_MODE 0x04
104 #define STLINK_DEV_UNKNOWN_MODE -1
106 #define STLINK_DFU_EXIT 0x07
108 #define STLINK_SWIM_ENTER 0x00
109 #define STLINK_SWIM_EXIT 0x01
111 #define STLINK_DEBUG_ENTER_JTAG 0x00
112 #define STLINK_DEBUG_GETSTATUS 0x01
113 #define STLINK_DEBUG_FORCEDEBUG 0x02
114 #define STLINK_DEBUG_APIV1_RESETSYS 0x03
115 #define STLINK_DEBUG_APIV1_READALLREGS 0x04
116 #define STLINK_DEBUG_APIV1_READREG 0x05
117 #define STLINK_DEBUG_APIV1_WRITEREG 0x06
118 #define STLINK_DEBUG_READMEM_32BIT 0x07
119 #define STLINK_DEBUG_WRITEMEM_32BIT 0x08
120 #define STLINK_DEBUG_RUNCORE 0x09
121 #define STLINK_DEBUG_STEPCORE 0x0a
122 #define STLINK_DEBUG_APIV1_SETFP 0x0b
123 #define STLINK_DEBUG_READMEM_8BIT 0x0c
124 #define STLINK_DEBUG_WRITEMEM_8BIT 0x0d
125 #define STLINK_DEBUG_APIV1_CLEARFP 0x0e
126 #define STLINK_DEBUG_APIV1_WRITEDEBUGREG 0x0f
127 #define STLINK_DEBUG_APIV1_SETWATCHPOINT 0x10
129 #define STLINK_DEBUG_ENTER_JTAG 0x00
130 #define STLINK_DEBUG_ENTER_SWD 0xa3
132 #define STLINK_DEBUG_APIV1_ENTER 0x20
133 #define STLINK_DEBUG_EXIT 0x21
134 #define STLINK_DEBUG_READCOREID 0x22
136 #define STLINK_DEBUG_APIV2_ENTER 0x30
137 #define STLINK_DEBUG_APIV2_READ_IDCODES 0x31
138 #define STLINK_DEBUG_APIV2_RESETSYS 0x32
139 #define STLINK_DEBUG_APIV2_READREG 0x33
140 #define STLINK_DEBUG_APIV2_WRITEREG 0x34
142 #define STLINK_DEBUG_APIV2_READALLREGS 0x3A
144 #define STLINK_DEBUG_APIV2_DRIVE_NRST 0x3C
146 #define STLINK_DEBUG_APIV2_DRIVE_NRST_LOW 0x00
147 #define STLINK_DEBUG_APIV2_DRIVE_NRST_HIGH 0x01
148 #define STLINK_DEBUG_APIV2_DRIVE_NRST_PULSE 0x02
150 /** */
151 enum stlink_mode {
152 STLINK_MODE_UNKNOWN = 0,
153 STLINK_MODE_DFU,
154 STLINK_MODE_MASS,
155 STLINK_MODE_DEBUG_JTAG,
156 STLINK_MODE_DEBUG_SWD,
157 STLINK_MODE_DEBUG_SWIM
160 /** */
161 static void stlink_usb_recv_v1_create_cmd(char *b, int s, uint32_t tag, uint32_t rxsize,
162 uint8_t flag, uint8_t lun, uint8_t length)
164 int i = 0;
166 memset(b, 0x00, s);
168 /* fill the send buffer */
169 strcpy(b, "USBC");
170 i += 4;
172 buf_set_u32(b+i, 0, 32, tag);
173 i += 4;
174 buf_set_u32(b+i, 0, 32, rxsize);
175 i += 4;
176 b[i++] = flag;
177 b[i++] = lun;
178 b[i++] = length;
181 /** */
182 static int stlink_usb_recv_v1_mass_storage_cmd(void *handle, const uint8_t *txbuf, int txsize, uint8_t *rxbuf,
183 int rxsize)
185 char sg_buffer[31];
186 struct stlink_usb_handle_s *h;
188 assert(handle != NULL);
190 h = (struct stlink_usb_handle_s *)handle;
191 h->sg_tag = (h->sg_tag + 1) & 1;
193 stlink_usb_recv_v1_create_cmd(sg_buffer, 31, h->sg_tag, rxsize, STLINK_TX_EP, 0x00, txsize);
195 memcpy(sg_buffer+15, txbuf, 10);
197 if (jtag_libusb_bulk_write(h->fd, STLINK_TX_EP, (char *)sg_buffer, 31,
198 1000) != 31) {
199 printf("send failed\n");
200 return ERROR_FAIL;
203 return ERROR_OK;
206 #define REQUEST_SENSE 0x03
207 #define REQUEST_SENSE_LENGTH 18
209 /** */
210 static int stlink_usb_recv_v1_get_status(void *handle, char *sg_buffer, int len)
212 struct stlink_usb_handle_s *h;
214 assert(handle != NULL);
216 h = (struct stlink_usb_handle_s *)handle;
218 /* read status */
219 memset(sg_buffer, 0x00, len);
221 if (jtag_libusb_bulk_read(h->fd, STLINK_RX_EP, (char *)sg_buffer,
222 len, 1000) != len)
223 return ERROR_FAIL;
225 uint32_t t1, t2;
227 t1 = buf_get_u32(sg_buffer+0, 0, 32);
228 t2 = buf_get_u32(sg_buffer+4, 0, 32);
230 /* check for USBS */
231 if (t1 != 0x53425355)
232 return ERROR_FAIL;
234 return ERROR_OK;
237 /** */
238 static int stlink_usb_recv_v1_get_sense(void *handle)
240 struct stlink_usb_handle_s *h;
241 char cdb[16];
242 char sg_buffer[31];
244 assert(handle != NULL);
246 h = (struct stlink_usb_handle_s *)handle;
247 h->sg_tag = (h->sg_tag + 1) & 1;
249 cdb[0] = REQUEST_SENSE;
250 cdb[4] = REQUEST_SENSE_LENGTH;
252 stlink_usb_recv_v1_create_cmd(sg_buffer, 31, h->sg_tag, REQUEST_SENSE_LENGTH, STLINK_TX_EP,
253 0x00, 16);
255 memcpy(sg_buffer+15, cdb, 16);
257 if (jtag_libusb_bulk_write(h->fd, STLINK_TX_EP, (char *)sg_buffer, 16,
258 1000) != 16)
259 return ERROR_FAIL;
261 if (jtag_libusb_bulk_read(h->fd, STLINK_RX_EP, (char *)cdb,
262 16, 1000) != 16)
263 return ERROR_FAIL;
265 if (stlink_usb_recv_v1_get_status(handle, sg_buffer, 13) != ERROR_OK)
266 return ERROR_FAIL;
267 /* check for sense */
268 if (sg_buffer[12] != 0)
269 return ERROR_FAIL;
271 /* if (sense[0] != 0x70 && sense[0] != 0x71) */
273 return ERROR_OK;
276 /** */
277 static int stlink_usb_recv_v1(void *handle, const uint8_t *txbuf, int txsize, uint8_t *rxbuf,
278 int rxsize)
280 int err;
281 char sg_buffer[31];
282 struct stlink_usb_handle_s *h;
284 assert(handle != NULL);
286 h = (struct stlink_usb_handle_s *)handle;
288 err = stlink_usb_recv_v1_mass_storage_cmd(handle, txbuf, txsize, rxbuf, rxsize);
290 if (err != ERROR_OK)
291 return err;
293 if (rxsize && rxbuf) {
294 if (jtag_libusb_bulk_read(h->fd, STLINK_RX_EP, (char *)rxbuf,
295 rxsize, 1000) != rxsize) {
296 LOG_DEBUG("jtag_libusb_bulk_read");
297 return ERROR_FAIL;
301 if (stlink_usb_recv_v1_get_status(handle, sg_buffer, 13) != ERROR_OK)
302 return ERROR_FAIL;
303 /* check for sense */
304 if (sg_buffer[12] == 1) {
305 LOG_DEBUG("get sense");
306 err = stlink_usb_recv_v1_get_sense(handle);
308 return err;
311 /** */
312 static int stlink_usb_recv_v2(void *handle, const uint8_t *txbuf, int txsize, uint8_t *rxbuf,
313 int rxsize)
315 struct stlink_usb_handle_s *h;
317 assert(handle != NULL);
319 h = (struct stlink_usb_handle_s *)handle;
321 if (jtag_libusb_bulk_write(h->fd, STLINK_TX_EP, (char *)txbuf, txsize,
322 1000) != txsize) {
323 return ERROR_FAIL;
325 if (rxsize && rxbuf) {
326 if (jtag_libusb_bulk_read(h->fd, STLINK_RX_EP, (char *)rxbuf,
327 rxsize, 1000) != rxsize) {
328 return ERROR_FAIL;
331 return ERROR_OK;
334 /** */
335 static int stlink_usb_recv(void *handle, const uint8_t *txbuf, int txsize, uint8_t *rxbuf,
336 int rxsize)
338 struct stlink_usb_handle_s *h;
340 assert(handle != NULL);
342 h = (struct stlink_usb_handle_s *)handle;
344 if (h->version.stlink == 1) {
345 return stlink_usb_recv_v1(handle, txbuf, txsize, rxbuf, rxsize);
346 } else {
347 if (txsize < STLINK_CMD_SIZE)
348 txsize = STLINK_CMD_SIZE;
349 return stlink_usb_recv_v2(handle, txbuf, txsize, rxbuf, rxsize);
353 /** */
354 static void stlink_usb_init_buffer(void *handle)
356 struct stlink_usb_handle_s *h;
358 assert(handle != NULL);
360 h = (struct stlink_usb_handle_s *)handle;
362 memset(h->txbuf, 0, STLINK_TX_SIZE);
363 memset(h->rxbuf, 0, STLINK_RX_SIZE);
366 static const char * const stlink_usb_error_msg[] = {
367 "unknown"
370 /** */
371 static int stlink_usb_error_check(void *handle)
373 int res;
374 const char *err_msg = 0;
375 struct stlink_usb_handle_s *h;
377 assert(handle != NULL);
379 h = (struct stlink_usb_handle_s *)handle;
381 /* TODO: no error checking yet on api V1 */
382 if (h->jtag_api == STLINK_JTAG_API_V1)
383 h->rxbuf[0] = STLINK_DEBUG_ERR_OK;
385 switch (h->rxbuf[0]) {
386 case STLINK_DEBUG_ERR_OK:
387 res = ERROR_OK;
388 break;
389 case STLINK_DEBUG_ERR_FAULT:
390 default:
391 err_msg = stlink_usb_error_msg[0];
392 res = ERROR_FAIL;
393 break;
396 if (res != ERROR_OK)
397 LOG_DEBUG("status error: %d ('%s')", h->rxbuf[0], err_msg);
399 return res;
402 /** */
403 static int stlink_usb_version(void *handle)
405 int res;
406 uint16_t v;
407 struct stlink_usb_handle_s *h;
409 assert(handle != NULL);
411 h = (struct stlink_usb_handle_s *)handle;
413 stlink_usb_init_buffer(handle);
415 h->txbuf[0] = STLINK_GET_VERSION;
417 res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, 6);
419 if (res != ERROR_OK)
420 return res;
422 v = (h->rxbuf[0] << 8) | h->rxbuf[1];
424 h->version.stlink = (v >> 12) & 0x0f;
425 h->version.jtag = (v >> 6) & 0x3f;
426 h->version.swim = v & 0x3f;
427 h->vid = buf_get_u32(h->rxbuf, 16, 16);
428 h->pid = buf_get_u32(h->rxbuf, 32, 16);
430 /* set the supported jtag api version
431 * V1 doesn't support API V2 at all
432 * V2 support API V2 since JTAG V13
434 if ((h->version.stlink == 2) && (h->version.jtag > 12))
435 h->version.jtag_api_max = STLINK_JTAG_API_V2;
436 else
437 h->version.jtag_api_max = STLINK_JTAG_API_V1;
439 LOG_DEBUG("STLINK v%d JTAG v%d API v%d SWIM v%d VID %04X PID %04X",
440 h->version.stlink,
441 h->version.jtag,
442 (h->version.jtag_api_max == STLINK_JTAG_API_V1) ? 1 : 2,
443 h->version.swim,
444 h->vid,
445 h->pid);
447 return ERROR_OK;
450 /** */
451 static int stlink_usb_current_mode(void *handle, uint8_t *mode)
453 int res;
454 struct stlink_usb_handle_s *h;
456 assert(handle != NULL);
458 h = (struct stlink_usb_handle_s *)handle;
460 stlink_usb_init_buffer(handle);
462 h->txbuf[0] = STLINK_GET_CURRENT_MODE;
464 res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, 2);
466 if (res != ERROR_OK)
467 return res;
469 *mode = h->rxbuf[0];
471 return ERROR_OK;
474 /** */
475 static int stlink_usb_mode_enter(void *handle, enum stlink_mode type)
477 int res;
478 int rx_size = 0;
479 struct stlink_usb_handle_s *h;
481 assert(handle != NULL);
483 h = (struct stlink_usb_handle_s *)handle;
485 stlink_usb_init_buffer(handle);
487 switch (type) {
488 case STLINK_MODE_DEBUG_JTAG:
489 h->txbuf[0] = STLINK_DEBUG_COMMAND;
490 if (h->jtag_api == STLINK_JTAG_API_V1)
491 h->txbuf[1] = STLINK_DEBUG_APIV1_ENTER;
492 else
493 h->txbuf[1] = STLINK_DEBUG_APIV2_ENTER;
494 h->txbuf[2] = STLINK_DEBUG_ENTER_JTAG;
495 break;
496 case STLINK_MODE_DEBUG_SWD:
497 h->txbuf[0] = STLINK_DEBUG_COMMAND;
498 if (h->jtag_api == STLINK_JTAG_API_V1)
499 h->txbuf[1] = STLINK_DEBUG_APIV1_ENTER;
500 else
501 h->txbuf[1] = STLINK_DEBUG_APIV2_ENTER;
502 h->txbuf[2] = STLINK_DEBUG_ENTER_SWD;
503 break;
504 case STLINK_MODE_DEBUG_SWIM:
505 h->txbuf[0] = STLINK_SWIM_COMMAND;
506 h->txbuf[1] = STLINK_SWIM_ENTER;
507 break;
508 case STLINK_MODE_DFU:
509 case STLINK_MODE_MASS:
510 default:
511 return ERROR_FAIL;
514 /* on api V2 we are able the read the latest command
515 * status
516 * TODO: we need the test on api V1 too
518 if (h->jtag_api == STLINK_JTAG_API_V2)
519 rx_size = 2;
521 res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, rx_size);
523 if (res != ERROR_OK)
524 return res;
526 res = stlink_usb_error_check(h);
528 return res;
531 /** */
532 static int stlink_usb_mode_leave(void *handle, enum stlink_mode type)
534 int res;
535 struct stlink_usb_handle_s *h;
537 assert(handle != NULL);
539 h = (struct stlink_usb_handle_s *)handle;
541 stlink_usb_init_buffer(handle);
543 switch (type) {
544 case STLINK_MODE_DEBUG_JTAG:
545 case STLINK_MODE_DEBUG_SWD:
546 h->txbuf[0] = STLINK_DEBUG_COMMAND;
547 h->txbuf[1] = STLINK_DEBUG_EXIT;
548 break;
549 case STLINK_MODE_DEBUG_SWIM:
550 h->txbuf[0] = STLINK_SWIM_COMMAND;
551 h->txbuf[1] = STLINK_SWIM_EXIT;
552 break;
553 case STLINK_MODE_DFU:
554 h->txbuf[0] = STLINK_DFU_COMMAND;
555 h->txbuf[1] = STLINK_DFU_EXIT;
556 break;
557 case STLINK_MODE_MASS:
558 default:
559 return ERROR_FAIL;
562 res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, 0, 0);
564 if (res != ERROR_OK)
565 return res;
567 return ERROR_OK;
570 /** */
571 static int stlink_usb_init_mode(void *handle)
573 int res;
574 uint8_t mode;
575 enum stlink_mode emode;
576 struct stlink_usb_handle_s *h;
578 assert(handle != NULL);
580 h = (struct stlink_usb_handle_s *)handle;
582 res = stlink_usb_current_mode(handle, &mode);
584 if (res != ERROR_OK)
585 return res;
587 LOG_DEBUG("MODE: %02X", mode);
589 /* try to exit current mode */
590 switch (mode) {
591 case STLINK_DEV_DFU_MODE:
592 emode = STLINK_MODE_DFU;
593 break;
594 case STLINK_DEV_DEBUG_MODE:
595 emode = STLINK_MODE_DEBUG_SWD;
596 break;
597 case STLINK_DEV_SWIM_MODE:
598 emode = STLINK_MODE_DEBUG_SWIM;
599 break;
600 case STLINK_DEV_BOOTLOADER_MODE:
601 case STLINK_DEV_MASS_MODE:
602 default:
603 emode = STLINK_MODE_UNKNOWN;
604 break;
607 if (emode != STLINK_MODE_UNKNOWN) {
608 res = stlink_usb_mode_leave(handle, emode);
610 if (res != ERROR_OK)
611 return res;
614 res = stlink_usb_current_mode(handle, &mode);
616 if (res != ERROR_OK)
617 return res;
619 LOG_DEBUG("MODE: %02X", mode);
621 /* set selected mode */
622 switch (h->transport) {
623 case STLINK_TRANSPORT_SWD:
624 emode = STLINK_MODE_DEBUG_SWD;
625 break;
626 case STLINK_TRANSPORT_JTAG:
627 emode = STLINK_MODE_DEBUG_JTAG;
628 break;
629 case STLINK_TRANSPORT_SWIM:
630 emode = STLINK_MODE_DEBUG_SWIM;
631 break;
632 default:
633 emode = STLINK_MODE_UNKNOWN;
634 break;
637 if (emode == STLINK_MODE_UNKNOWN) {
638 LOG_ERROR("selected mode (transport) not supported");
639 return ERROR_FAIL;
642 res = stlink_usb_mode_enter(handle, emode);
644 if (res != ERROR_OK)
645 return res;
647 res = stlink_usb_current_mode(handle, &mode);
649 if (res != ERROR_OK)
650 return res;
652 LOG_DEBUG("MODE: %02X", mode);
654 return ERROR_OK;
657 /** */
658 static int stlink_usb_idcode(void *handle, uint32_t *idcode)
660 int res;
661 struct stlink_usb_handle_s *h;
663 assert(handle != NULL);
665 h = (struct stlink_usb_handle_s *)handle;
667 stlink_usb_init_buffer(handle);
669 h->txbuf[0] = STLINK_DEBUG_COMMAND;
670 h->txbuf[1] = STLINK_DEBUG_READCOREID;
672 res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, 4);
674 if (res != ERROR_OK)
675 return res;
677 *idcode = le_to_h_u32(h->rxbuf);
679 LOG_DEBUG("IDCODE: %08X", *idcode);
681 return ERROR_OK;
684 /** */
685 static enum target_state stlink_usb_state(void *handle)
687 int res;
688 struct stlink_usb_handle_s *h;
690 assert(handle != NULL);
692 h = (struct stlink_usb_handle_s *)handle;
694 if (h->jtag_api == STLINK_JTAG_API_V2)
695 return TARGET_UNKNOWN;
697 stlink_usb_init_buffer(handle);
699 h->txbuf[0] = STLINK_DEBUG_COMMAND;
700 h->txbuf[1] = STLINK_DEBUG_GETSTATUS;
702 res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, 2);
704 if (res != ERROR_OK)
705 return TARGET_UNKNOWN;
707 if (h->rxbuf[0] == STLINK_CORE_RUNNING)
708 return TARGET_RUNNING;
709 if (h->rxbuf[0] == STLINK_CORE_HALTED)
710 return TARGET_HALTED;
712 return TARGET_UNKNOWN;
715 /** */
716 static int stlink_usb_reset(void *handle)
718 int res;
719 struct stlink_usb_handle_s *h;
721 assert(handle != NULL);
723 h = (struct stlink_usb_handle_s *)handle;
725 stlink_usb_init_buffer(handle);
727 h->txbuf[0] = STLINK_DEBUG_COMMAND;
729 if (h->jtag_api == STLINK_JTAG_API_V1)
730 h->txbuf[1] = STLINK_DEBUG_APIV1_RESETSYS;
731 else
732 h->txbuf[1] = STLINK_DEBUG_APIV2_RESETSYS;
734 res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, 2);
736 if (res != ERROR_OK)
737 return res;
739 LOG_DEBUG("RESET: %08X", h->rxbuf[0]);
741 return ERROR_OK;
744 /** */
745 static int stlink_usb_run(void *handle)
747 int res;
748 struct stlink_usb_handle_s *h;
750 assert(handle != NULL);
752 h = (struct stlink_usb_handle_s *)handle;
754 if (h->jtag_api == STLINK_JTAG_API_V2)
755 return ERROR_FAIL;
757 stlink_usb_init_buffer(handle);
759 h->txbuf[0] = STLINK_DEBUG_COMMAND;
760 h->txbuf[1] = STLINK_DEBUG_RUNCORE;
762 res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, 2);
764 if (res != ERROR_OK)
765 return res;
767 return ERROR_OK;
770 /** */
771 static int stlink_usb_halt(void *handle)
773 int res;
774 struct stlink_usb_handle_s *h;
776 assert(handle != NULL);
778 h = (struct stlink_usb_handle_s *)handle;
780 if (h->jtag_api == STLINK_JTAG_API_V2)
781 return ERROR_FAIL;
783 stlink_usb_init_buffer(handle);
785 h->txbuf[0] = STLINK_DEBUG_COMMAND;
786 h->txbuf[1] = STLINK_DEBUG_FORCEDEBUG;
788 res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, 2);
790 if (res != ERROR_OK)
791 return res;
793 return ERROR_OK;
796 /** */
797 static int stlink_usb_step(void *handle)
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_V2)
807 return ERROR_FAIL;
809 stlink_usb_init_buffer(handle);
811 h->txbuf[0] = STLINK_DEBUG_COMMAND;
812 h->txbuf[1] = STLINK_DEBUG_STEPCORE;
814 res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, 2);
816 if (res != ERROR_OK)
817 return res;
819 return ERROR_OK;
822 /** */
823 static int stlink_usb_read_regs(void *handle)
825 int res;
826 struct stlink_usb_handle_s *h;
828 assert(handle != NULL);
830 h = (struct stlink_usb_handle_s *)handle;
832 stlink_usb_init_buffer(handle);
834 h->txbuf[0] = STLINK_DEBUG_COMMAND;
835 if (h->jtag_api == STLINK_JTAG_API_V1)
836 h->txbuf[1] = STLINK_DEBUG_APIV1_READALLREGS;
837 else
838 h->txbuf[1] = STLINK_DEBUG_APIV2_READALLREGS;
840 res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, 84);
842 if (res != ERROR_OK)
843 return res;
845 return ERROR_OK;
848 /** */
849 static int stlink_usb_read_reg(void *handle, int num, uint32_t *val)
851 int res;
852 struct stlink_usb_handle_s *h;
854 assert(handle != NULL);
856 h = (struct stlink_usb_handle_s *)handle;
858 stlink_usb_init_buffer(handle);
860 h->txbuf[0] = STLINK_DEBUG_COMMAND;
861 if (h->jtag_api == STLINK_JTAG_API_V1)
862 h->txbuf[1] = STLINK_DEBUG_APIV1_READREG;
863 else
864 h->txbuf[1] = STLINK_DEBUG_APIV2_READREG;
865 h->txbuf[2] = num;
867 res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, 4);
869 if (res != ERROR_OK)
870 return res;
872 *val = le_to_h_u32(h->rxbuf);
874 return ERROR_OK;
877 /** */
878 static int stlink_usb_write_reg(void *handle, int num, uint32_t val)
880 int res;
881 struct stlink_usb_handle_s *h;
883 assert(handle != NULL);
885 h = (struct stlink_usb_handle_s *)handle;
887 stlink_usb_init_buffer(handle);
889 h->txbuf[0] = STLINK_DEBUG_COMMAND;
890 if (h->jtag_api == STLINK_JTAG_API_V1)
891 h->txbuf[1] = STLINK_DEBUG_APIV1_WRITEREG;
892 else
893 h->txbuf[1] = STLINK_DEBUG_APIV2_WRITEREG;
894 h->txbuf[2] = num;
895 h_u32_to_le(h->txbuf + 3, val);
897 res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, 2);
899 if (res != ERROR_OK)
900 return res;
902 return ERROR_OK;
905 /** */
906 static int stlink_usb_read_mem8(void *handle, uint32_t addr, uint16_t len,
907 uint8_t *buffer)
909 int res;
910 uint16_t read_len = len;
911 struct stlink_usb_handle_s *h;
913 assert(handle != NULL);
915 h = (struct stlink_usb_handle_s *)handle;
917 stlink_usb_init_buffer(handle);
919 h->txbuf[0] = STLINK_DEBUG_COMMAND;
920 h->txbuf[1] = STLINK_DEBUG_READMEM_8BIT;
921 h_u32_to_le(h->txbuf + 2, addr);
922 h_u16_to_le(h->txbuf + 2 + 4, len);
924 /* we need to fix read length for single bytes */
925 if (read_len == 1)
926 read_len++;
928 res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, read_len);
930 if (res != ERROR_OK)
931 return res;
933 memcpy(buffer, h->rxbuf, len);
935 return ERROR_OK;
938 /** */
939 static int stlink_usb_write_mem8(void *handle, uint32_t addr, uint16_t len,
940 const uint8_t *buffer)
942 int res;
943 struct stlink_usb_handle_s *h;
945 assert(handle != NULL);
947 h = (struct stlink_usb_handle_s *)handle;
949 stlink_usb_init_buffer(handle);
951 h->txbuf[0] = STLINK_DEBUG_COMMAND;
952 h->txbuf[1] = STLINK_DEBUG_WRITEMEM_8BIT;
953 h_u32_to_le(h->txbuf + 2, addr);
954 h_u16_to_le(h->txbuf + 2 + 4, len);
956 res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, 0, 0);
958 if (res != ERROR_OK)
959 return res;
961 res = stlink_usb_recv(handle, (uint8_t *) buffer, len, 0, 0);
963 if (res != ERROR_OK)
964 return res;
966 return ERROR_OK;
969 /** */
970 static int stlink_usb_read_mem32(void *handle, uint32_t addr, uint16_t len,
971 uint32_t *buffer)
973 int res;
974 struct stlink_usb_handle_s *h;
976 assert(handle != NULL);
978 h = (struct stlink_usb_handle_s *)handle;
980 stlink_usb_init_buffer(handle);
982 len *= 4;
984 h->txbuf[0] = STLINK_DEBUG_COMMAND;
985 h->txbuf[1] = STLINK_DEBUG_READMEM_32BIT;
986 h_u32_to_le(h->txbuf + 2, addr);
987 h_u16_to_le(h->txbuf + 2 + 4, len);
989 res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, len);
991 if (res != ERROR_OK)
992 return res;
994 memcpy(buffer, h->rxbuf, len);
996 return ERROR_OK;
999 /** */
1000 static int stlink_usb_write_mem32(void *handle, uint32_t addr, uint16_t len,
1001 const uint32_t *buffer)
1003 int res;
1004 struct stlink_usb_handle_s *h;
1006 assert(handle != NULL);
1008 h = (struct stlink_usb_handle_s *)handle;
1010 stlink_usb_init_buffer(handle);
1012 len *= 4;
1014 h->txbuf[0] = STLINK_DEBUG_COMMAND;
1015 h->txbuf[1] = STLINK_DEBUG_WRITEMEM_32BIT;
1016 h_u32_to_le(h->txbuf + 2, addr);
1017 h_u16_to_le(h->txbuf + 2 + 4, len);
1019 res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, 0, 0);
1021 if (res != ERROR_OK)
1022 return res;
1024 res = stlink_usb_recv(handle, (uint8_t *) buffer, len, 0, 0);
1026 if (res != ERROR_OK)
1027 return res;
1029 return ERROR_OK;
1032 /** */
1033 static int stlink_usb_open(struct stlink_interface_param_s *param, void **fd)
1035 int err;
1036 struct stlink_usb_handle_s *h;
1038 LOG_DEBUG("stlink_usb_open");
1040 h = malloc(sizeof(struct stlink_usb_handle_s));
1042 if (h == 0) {
1043 LOG_DEBUG("malloc failed");
1044 return ERROR_FAIL;
1047 h->transport = param->transport;
1049 const uint16_t vids[] = { param->vid, 0 };
1050 const uint16_t pids[] = { param->pid, 0 };
1052 LOG_DEBUG("transport: %d vid: %04x pid: %04x", param->transport,
1053 param->vid, param->pid);
1055 if (jtag_libusb_open(vids, pids, &h->fd) != ERROR_OK) {
1056 LOG_ERROR("open failed");
1057 return ERROR_FAIL;
1060 jtag_libusb_set_configuration(h->fd, 0);
1062 if (jtag_libusb_claim_interface(h->fd, 0) != ERROR_OK) {
1063 LOG_DEBUG("claim interface failed");
1064 return ERROR_FAIL;
1067 /* wrap version for first read */
1068 switch (param->pid) {
1069 case 0x3744:
1070 h->version.stlink = 1;
1071 break;
1072 default:
1073 h->version.stlink = 2;
1074 break;
1077 /* get the device version */
1078 err = stlink_usb_version(h);
1080 if (err != ERROR_OK) {
1081 LOG_ERROR("read version failed");
1082 jtag_libusb_close(h->fd);
1083 free(h);
1084 return err;
1087 /* compare usb vid/pid */
1088 if ((param->vid != h->vid) || (param->pid != h->pid))
1089 LOG_INFO("vid/pid are not identical: %04X/%04X %04X/%04X",
1090 param->vid, param->pid,
1091 h->vid, h->pid);
1093 /* check if mode is supported */
1094 err = ERROR_OK;
1096 switch (h->transport) {
1097 case STLINK_TRANSPORT_SWD:
1098 case STLINK_TRANSPORT_JTAG:
1099 if (h->version.jtag == 0)
1100 err = ERROR_FAIL;
1101 break;
1102 case STLINK_TRANSPORT_SWIM:
1103 if (h->version.swim == 0)
1104 err = ERROR_FAIL;
1105 break;
1106 default:
1107 err = ERROR_FAIL;
1108 break;
1111 if (err != ERROR_OK) {
1112 LOG_ERROR("mode (transport) not supported by device");
1113 jtag_libusb_close(h->fd);
1114 free(h);
1115 return err;
1118 /* set the used jtag api */
1119 h->jtag_api = STLINK_JTAG_API_V1;
1121 /* initialize the debug hardware */
1122 err = stlink_usb_init_mode(h);
1124 if (err != ERROR_OK) {
1125 LOG_ERROR("init mode failed");
1126 jtag_libusb_close(h->fd);
1127 free(h);
1128 return err;
1131 *fd = h;
1133 return ERROR_OK;
1136 /** */
1137 static int stlink_usb_close(void *fd)
1139 return ERROR_OK;
1142 /** */
1143 struct stlink_layout_api_s stlink_usb_layout_api = {
1144 /** */
1145 .open = stlink_usb_open,
1146 /** */
1147 .close = stlink_usb_close,
1148 /** */
1149 .idcode = stlink_usb_idcode,
1150 /** */
1151 .state = stlink_usb_state,
1152 /** */
1153 .reset = stlink_usb_reset,
1154 /** */
1155 .run = stlink_usb_run,
1156 /** */
1157 .halt = stlink_usb_halt,
1158 /** */
1159 .step = stlink_usb_step,
1160 /** */
1161 .read_regs = stlink_usb_read_regs,
1162 /** */
1163 .read_reg = stlink_usb_read_reg,
1164 /** */
1165 .write_reg = stlink_usb_write_reg,
1166 /** */
1167 .read_mem8 = stlink_usb_read_mem8,
1168 /** */
1169 .write_mem8 = stlink_usb_write_mem8,
1170 /** */
1171 .read_mem32 = stlink_usb_read_mem32,
1172 /** */
1173 .write_mem32 = stlink_usb_write_mem32,