Add bootloader mode.
[openocd.git] / src / jtag / drivers / stlink_usb.c
blobbf146444b21bf74f831854f1d1c6db7b21128be7
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_OK 0x80
88 #define STLINK_FALSE 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
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
143 /** */
144 enum stlink_mode {
145 STLINK_MODE_UNKNOWN = 0,
146 STLINK_MODE_DFU,
147 STLINK_MODE_MASS,
148 STLINK_MODE_DEBUG_JTAG,
149 STLINK_MODE_DEBUG_SWD,
150 STLINK_MODE_DEBUG_SWIM
153 /** */
154 static void stlink_usb_recv_v1_create_cmd(char *b, int s, uint32_t tag, uint32_t rxsize,
155 uint8_t flag, uint8_t lun, uint8_t length)
157 int i = 0;
159 memset(b, 0x00, s);
161 /* fill the send buffer */
162 strcpy(b, "USBC");
163 i += 4;
165 buf_set_u32(b+i, 0, 32, tag);
166 i += 4;
167 buf_set_u32(b+i, 0, 32, rxsize);
168 i += 4;
169 b[i++] = flag;
170 b[i++] = lun;
171 b[i++] = length;
174 /** */
175 static int stlink_usb_recv_v1_mass_storage_cmd(void *handle, const uint8_t *txbuf, int txsize, uint8_t *rxbuf,
176 int rxsize)
178 char sg_buffer[31];
179 struct stlink_usb_handle_s *h;
181 assert(handle != NULL);
183 h = (struct stlink_usb_handle_s *)handle;
184 h->sg_tag = (h->sg_tag + 1) & 1;
186 stlink_usb_recv_v1_create_cmd(sg_buffer, 31, h->sg_tag, rxsize, STLINK_TX_EP, 0x00, txsize);
188 memcpy(sg_buffer+15, txbuf, 10);
190 if (jtag_libusb_bulk_write(h->fd, STLINK_TX_EP, (char *)sg_buffer, 31,
191 1000) != 31) {
192 printf("send failed\n");
193 return ERROR_FAIL;
196 return ERROR_OK;
199 #define REQUEST_SENSE 0x03
200 #define REQUEST_SENSE_LENGTH 18
202 /** */
203 static int stlink_usb_recv_v1_get_status(void *handle, char *sg_buffer, int len)
205 struct stlink_usb_handle_s *h;
207 assert(handle != NULL);
209 h = (struct stlink_usb_handle_s *)handle;
211 /* read status */
212 memset(sg_buffer, 0x00, len);
214 if (jtag_libusb_bulk_read(h->fd, STLINK_RX_EP, (char *)sg_buffer,
215 len, 1000) != len)
216 return ERROR_FAIL;
218 uint32_t t1, t2;
220 t1 = buf_get_u32(sg_buffer+0, 0, 32);
221 t2 = buf_get_u32(sg_buffer+4, 0, 32);
223 /* check for USBS */
224 if (t1 != 0x53425355)
225 return ERROR_FAIL;
227 return ERROR_OK;
230 /** */
231 static int stlink_usb_recv_v1_get_sense(void *handle)
233 struct stlink_usb_handle_s *h;
234 char cdb[16];
235 char sg_buffer[31];
237 assert(handle != NULL);
239 h = (struct stlink_usb_handle_s *)handle;
240 h->sg_tag = (h->sg_tag + 1) & 1;
242 cdb[0] = REQUEST_SENSE;
243 cdb[4] = REQUEST_SENSE_LENGTH;
245 stlink_usb_recv_v1_create_cmd(sg_buffer, 31, h->sg_tag, REQUEST_SENSE_LENGTH, STLINK_TX_EP,
246 0x00, 16);
248 memcpy(sg_buffer+15, cdb, 16);
250 if (jtag_libusb_bulk_write(h->fd, STLINK_TX_EP, (char *)sg_buffer, 16,
251 1000) != 16)
252 return ERROR_FAIL;
254 if (jtag_libusb_bulk_read(h->fd, STLINK_RX_EP, (char *)cdb,
255 16, 1000) != 16)
256 return ERROR_FAIL;
258 if (stlink_usb_recv_v1_get_status(handle, sg_buffer, 13) != ERROR_OK)
259 return ERROR_FAIL;
260 /* check for sense */
261 if (sg_buffer[12] != 0)
262 return ERROR_FAIL;
264 /* if (sense[0] != 0x70 && sense[0] != 0x71) */
266 return ERROR_OK;
269 /** */
270 static int stlink_usb_recv_v1(void *handle, const uint8_t *txbuf, int txsize, uint8_t *rxbuf,
271 int rxsize)
273 int err;
274 char sg_buffer[31];
275 struct stlink_usb_handle_s *h;
277 assert(handle != NULL);
279 h = (struct stlink_usb_handle_s *)handle;
281 err = stlink_usb_recv_v1_mass_storage_cmd(handle, txbuf, txsize, rxbuf, rxsize);
283 if (err != ERROR_OK)
284 return err;
286 if (rxsize && rxbuf) {
287 if (jtag_libusb_bulk_read(h->fd, STLINK_RX_EP, (char *)rxbuf,
288 rxsize, 1000) != rxsize) {
289 LOG_DEBUG("jtag_libusb_bulk_read");
290 return ERROR_FAIL;
294 if (stlink_usb_recv_v1_get_status(handle, sg_buffer, 13) != ERROR_OK)
295 return ERROR_FAIL;
296 /* check for sense */
297 if (sg_buffer[12] == 1) {
298 LOG_DEBUG("get sense");
299 err = stlink_usb_recv_v1_get_sense(handle);
301 return err;
304 /** */
305 static int stlink_usb_recv_v2(void *handle, const uint8_t *txbuf, int txsize, uint8_t *rxbuf,
306 int rxsize)
308 struct stlink_usb_handle_s *h;
310 assert(handle != NULL);
312 h = (struct stlink_usb_handle_s *)handle;
314 if (jtag_libusb_bulk_write(h->fd, STLINK_TX_EP, (char *)txbuf, txsize,
315 1000) != txsize) {
316 return ERROR_FAIL;
318 if (rxsize && rxbuf) {
319 if (jtag_libusb_bulk_read(h->fd, STLINK_RX_EP, (char *)rxbuf,
320 rxsize, 1000) != rxsize) {
321 return ERROR_FAIL;
324 return ERROR_OK;
327 /** */
328 static int stlink_usb_recv(void *handle, const uint8_t *txbuf, int txsize, uint8_t *rxbuf,
329 int rxsize)
331 struct stlink_usb_handle_s *h;
333 assert(handle != NULL);
335 h = (struct stlink_usb_handle_s *)handle;
337 if (h->version.stlink == 1) {
338 return stlink_usb_recv_v1(handle, txbuf, txsize, rxbuf, rxsize);
339 } else {
340 if (txsize < STLINK_CMD_SIZE)
341 txsize = STLINK_CMD_SIZE;
342 return stlink_usb_recv_v2(handle, txbuf, txsize, rxbuf, rxsize);
346 /** */
347 static void stlink_usb_init_buffer(void *handle)
349 struct stlink_usb_handle_s *h;
351 assert(handle != NULL);
353 h = (struct stlink_usb_handle_s *)handle;
355 memset(h->txbuf, 0, STLINK_CMD_SIZE);
358 /** */
359 static int stlink_usb_version(void *handle)
361 int res;
362 uint16_t v;
363 struct stlink_usb_handle_s *h;
365 assert(handle != NULL);
367 h = (struct stlink_usb_handle_s *)handle;
369 stlink_usb_init_buffer(handle);
371 h->txbuf[0] = STLINK_GET_VERSION;
373 res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, 6);
375 if (res != ERROR_OK)
376 return res;
378 v = (h->rxbuf[0] << 8) | h->rxbuf[1];
380 h->version.stlink = (v >> 12) & 0x0f;
381 h->version.jtag = (v >> 6) & 0x3f;
382 h->version.swim = v & 0x3f;
383 h->vid = buf_get_u32(h->rxbuf, 16, 16);
384 h->pid = buf_get_u32(h->rxbuf, 32, 16);
386 /* set the supported jtag api version
387 * V1 doesn't support API V2 at all
388 * V2 support API V2 since JTAG V13
390 if ((h->version.stlink == 2) && (h->version.jtag > 12))
391 h->version.jtag_api_max = STLINK_JTAG_API_V2;
392 else
393 h->version.jtag_api_max = STLINK_JTAG_API_V1;
395 LOG_DEBUG("STLINK v%d JTAG v%d API v%d SWIM v%d VID %04X PID %04X",
396 h->version.stlink,
397 h->version.jtag,
398 (h->version.jtag_api_max == STLINK_JTAG_API_V1) ? 1 : 2,
399 h->version.swim,
400 h->vid,
401 h->pid);
403 return ERROR_OK;
406 /** */
407 static int stlink_usb_current_mode(void *handle, uint8_t *mode)
409 int res;
410 struct stlink_usb_handle_s *h;
412 assert(handle != NULL);
414 h = (struct stlink_usb_handle_s *)handle;
416 stlink_usb_init_buffer(handle);
418 h->txbuf[0] = STLINK_GET_CURRENT_MODE;
420 res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, 2);
422 if (res != ERROR_OK)
423 return res;
425 *mode = h->rxbuf[0];
427 return ERROR_OK;
430 /** */
431 static int stlink_usb_mode_enter(void *handle, enum stlink_mode type)
433 int res;
434 struct stlink_usb_handle_s *h;
436 assert(handle != NULL);
438 h = (struct stlink_usb_handle_s *)handle;
440 stlink_usb_init_buffer(handle);
442 switch (type) {
443 case STLINK_MODE_DEBUG_JTAG:
444 h->txbuf[0] = STLINK_DEBUG_COMMAND;
445 if (h->jtag_api == STLINK_JTAG_API_V1)
446 h->txbuf[1] = STLINK_DEBUG_APIV1_ENTER;
447 else
448 h->txbuf[1] = STLINK_DEBUG_APIV2_ENTER;
449 h->txbuf[2] = STLINK_DEBUG_ENTER_JTAG;
450 break;
451 case STLINK_MODE_DEBUG_SWD:
452 h->txbuf[0] = STLINK_DEBUG_COMMAND;
453 if (h->jtag_api == STLINK_JTAG_API_V1)
454 h->txbuf[1] = STLINK_DEBUG_APIV1_ENTER;
455 else
456 h->txbuf[1] = STLINK_DEBUG_APIV2_ENTER;
457 h->txbuf[2] = STLINK_DEBUG_ENTER_SWD;
458 break;
459 case STLINK_MODE_DEBUG_SWIM:
460 h->txbuf[0] = STLINK_SWIM_COMMAND;
461 h->txbuf[1] = STLINK_SWIM_ENTER;
462 break;
463 case STLINK_MODE_DFU:
464 case STLINK_MODE_MASS:
465 default:
466 return ERROR_FAIL;
469 res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, 0, 0);
470 if (res != ERROR_OK)
471 return res;
473 return ERROR_OK;
476 /** */
477 static int stlink_usb_mode_leave(void *handle, enum stlink_mode type)
479 int res;
480 struct stlink_usb_handle_s *h;
482 assert(handle != NULL);
484 h = (struct stlink_usb_handle_s *)handle;
486 stlink_usb_init_buffer(handle);
488 switch (type) {
489 case STLINK_MODE_DEBUG_JTAG:
490 case STLINK_MODE_DEBUG_SWD:
491 h->txbuf[0] = STLINK_DEBUG_COMMAND;
492 h->txbuf[1] = STLINK_DEBUG_EXIT;
493 break;
494 case STLINK_MODE_DEBUG_SWIM:
495 h->txbuf[0] = STLINK_SWIM_COMMAND;
496 h->txbuf[1] = STLINK_SWIM_EXIT;
497 break;
498 case STLINK_MODE_DFU:
499 h->txbuf[0] = STLINK_DFU_COMMAND;
500 h->txbuf[1] = STLINK_DFU_EXIT;
501 break;
502 case STLINK_MODE_MASS:
503 default:
504 return ERROR_FAIL;
507 res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, 0, 0);
508 if (res != ERROR_OK)
509 return res;
511 return ERROR_OK;
514 /** */
515 static int stlink_usb_init_mode(void *handle)
517 int res;
518 uint8_t mode;
519 enum stlink_mode emode;
520 struct stlink_usb_handle_s *h;
522 assert(handle != NULL);
524 h = (struct stlink_usb_handle_s *)handle;
526 res = stlink_usb_current_mode(handle, &mode);
528 if (res != ERROR_OK)
529 return res;
531 LOG_DEBUG("MODE: %02X", mode);
533 /* try to exit current mode */
534 switch (mode) {
535 case STLINK_DEV_DFU_MODE:
536 emode = STLINK_MODE_DFU;
537 break;
538 case STLINK_DEV_DEBUG_MODE:
539 emode = STLINK_MODE_DEBUG_SWD;
540 break;
541 case STLINK_DEV_SWIM_MODE:
542 emode = STLINK_MODE_DEBUG_SWIM;
543 break;
544 case STLINK_DEV_BOOTLOADER_MODE:
545 default:
546 emode = STLINK_MODE_UNKNOWN;
547 break;
550 if (emode != STLINK_MODE_UNKNOWN) {
551 res = stlink_usb_mode_leave(handle, emode);
553 if (res != ERROR_OK)
554 return res;
557 res = stlink_usb_current_mode(handle, &mode);
559 if (res != ERROR_OK)
560 return res;
562 LOG_DEBUG("MODE: %02X", mode);
564 /* set selected mode */
565 switch (h->transport) {
566 case STLINK_TRANSPORT_SWD:
567 emode = STLINK_MODE_DEBUG_SWD;
568 break;
569 case STLINK_TRANSPORT_JTAG:
570 emode = STLINK_MODE_DEBUG_JTAG;
571 break;
572 case STLINK_TRANSPORT_SWIM:
573 emode = STLINK_MODE_DEBUG_SWIM;
574 break;
575 default:
576 emode = STLINK_MODE_UNKNOWN;
577 break;
580 if (emode == STLINK_MODE_UNKNOWN) {
581 LOG_ERROR("selected mode (transport) not supported");
582 return ERROR_FAIL;
585 res = stlink_usb_mode_enter(handle, emode);
587 if (res != ERROR_OK)
588 return res;
590 res = stlink_usb_current_mode(handle, &mode);
592 if (res != ERROR_OK)
593 return res;
595 LOG_DEBUG("MODE: %02X", mode);
597 return ERROR_OK;
600 /** */
601 static int stlink_usb_idcode(void *handle, uint32_t *idcode)
603 int res;
604 struct stlink_usb_handle_s *h;
606 assert(handle != NULL);
608 h = (struct stlink_usb_handle_s *)handle;
610 stlink_usb_init_buffer(handle);
612 h->txbuf[0] = STLINK_DEBUG_COMMAND;
613 h->txbuf[1] = STLINK_DEBUG_READCOREID;
615 res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, 4);
617 if (res != ERROR_OK)
618 return res;
620 *idcode = le_to_h_u32(h->rxbuf);
622 LOG_DEBUG("IDCODE: %08X", *idcode);
624 return ERROR_OK;
627 /** */
628 static enum target_state stlink_usb_state(void *handle)
630 int res;
631 struct stlink_usb_handle_s *h;
633 assert(handle != NULL);
635 h = (struct stlink_usb_handle_s *)handle;
637 if (h->jtag_api == STLINK_JTAG_API_V2)
638 return TARGET_UNKNOWN;
640 stlink_usb_init_buffer(handle);
642 h->txbuf[0] = STLINK_DEBUG_COMMAND;
643 h->txbuf[1] = STLINK_DEBUG_GETSTATUS;
645 res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, 2);
647 if (res != ERROR_OK)
648 return TARGET_UNKNOWN;
650 if (h->rxbuf[0] == STLINK_CORE_RUNNING)
651 return TARGET_RUNNING;
652 if (h->rxbuf[0] == STLINK_CORE_HALTED)
653 return TARGET_HALTED;
655 return TARGET_UNKNOWN;
658 /** */
659 static int stlink_usb_reset(void *handle)
661 int res;
662 struct stlink_usb_handle_s *h;
664 assert(handle != NULL);
666 h = (struct stlink_usb_handle_s *)handle;
668 stlink_usb_init_buffer(handle);
670 h->txbuf[0] = STLINK_DEBUG_COMMAND;
672 if (h->jtag_api == STLINK_JTAG_API_V1)
673 h->txbuf[1] = STLINK_DEBUG_APIV1_RESETSYS;
674 else
675 h->txbuf[1] = STLINK_DEBUG_APIV2_RESETSYS;
677 res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, 2);
679 if (res != ERROR_OK)
680 return res;
682 LOG_DEBUG("RESET: %08X", h->rxbuf[0]);
684 return ERROR_OK;
687 /** */
688 static int stlink_usb_run(void *handle)
690 int res;
691 struct stlink_usb_handle_s *h;
693 assert(handle != NULL);
695 h = (struct stlink_usb_handle_s *)handle;
697 if (h->jtag_api == STLINK_JTAG_API_V2)
698 return ERROR_FAIL;
700 stlink_usb_init_buffer(handle);
702 h->txbuf[0] = STLINK_DEBUG_COMMAND;
703 h->txbuf[1] = STLINK_DEBUG_RUNCORE;
705 res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, 2);
707 if (res != ERROR_OK)
708 return res;
710 return ERROR_OK;
713 /** */
714 static int stlink_usb_halt(void *handle)
716 int res;
717 struct stlink_usb_handle_s *h;
719 assert(handle != NULL);
721 h = (struct stlink_usb_handle_s *)handle;
723 if (h->jtag_api == STLINK_JTAG_API_V2)
724 return ERROR_FAIL;
726 stlink_usb_init_buffer(handle);
728 h->txbuf[0] = STLINK_DEBUG_COMMAND;
729 h->txbuf[1] = STLINK_DEBUG_FORCEDEBUG;
731 res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, 2);
733 if (res != ERROR_OK)
734 return res;
736 return ERROR_OK;
739 /** */
740 static int stlink_usb_step(void *handle)
742 int res;
743 struct stlink_usb_handle_s *h;
745 assert(handle != NULL);
747 h = (struct stlink_usb_handle_s *)handle;
749 if (h->jtag_api == STLINK_JTAG_API_V2)
750 return ERROR_FAIL;
752 stlink_usb_init_buffer(handle);
754 h->txbuf[0] = STLINK_DEBUG_COMMAND;
755 h->txbuf[1] = STLINK_DEBUG_STEPCORE;
757 res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, 2);
759 if (res != ERROR_OK)
760 return res;
762 return ERROR_OK;
765 /** */
766 static int stlink_usb_read_regs(void *handle)
768 int res;
769 struct stlink_usb_handle_s *h;
771 assert(handle != NULL);
773 h = (struct stlink_usb_handle_s *)handle;
775 stlink_usb_init_buffer(handle);
777 h->txbuf[0] = STLINK_DEBUG_COMMAND;
778 if (h->jtag_api == STLINK_JTAG_API_V1)
779 h->txbuf[1] = STLINK_DEBUG_APIV1_READALLREGS;
780 else
781 h->txbuf[1] = STLINK_DEBUG_APIV2_READALLREGS;
783 res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, 84);
785 if (res != ERROR_OK)
786 return res;
788 return ERROR_OK;
791 /** */
792 static int stlink_usb_read_reg(void *handle, int num, uint32_t *val)
794 int res;
795 struct stlink_usb_handle_s *h;
797 assert(handle != NULL);
799 h = (struct stlink_usb_handle_s *)handle;
801 stlink_usb_init_buffer(handle);
803 h->txbuf[0] = STLINK_DEBUG_COMMAND;
804 if (h->jtag_api == STLINK_JTAG_API_V1)
805 h->txbuf[1] = STLINK_DEBUG_APIV1_READREG;
806 else
807 h->txbuf[1] = STLINK_DEBUG_APIV2_READREG;
808 h->txbuf[2] = num;
810 res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, 4);
812 if (res != ERROR_OK)
813 return res;
815 *val = le_to_h_u32(h->rxbuf);
817 return ERROR_OK;
820 /** */
821 static int stlink_usb_write_reg(void *handle, int num, uint32_t val)
823 int res;
824 struct stlink_usb_handle_s *h;
826 assert(handle != NULL);
828 h = (struct stlink_usb_handle_s *)handle;
830 stlink_usb_init_buffer(handle);
832 h->txbuf[0] = STLINK_DEBUG_COMMAND;
833 if (h->jtag_api == STLINK_JTAG_API_V1)
834 h->txbuf[1] = STLINK_DEBUG_APIV1_WRITEREG;
835 else
836 h->txbuf[1] = STLINK_DEBUG_APIV2_WRITEREG;
837 h->txbuf[2] = num;
838 h_u32_to_le(h->txbuf + 3, val);
840 res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, 2);
842 if (res != ERROR_OK)
843 return res;
845 return ERROR_OK;
848 /** */
849 static int stlink_usb_read_mem8(void *handle, uint32_t addr, uint16_t len,
850 uint8_t *buffer)
852 int res;
853 uint16_t read_len = len;
854 struct stlink_usb_handle_s *h;
856 assert(handle != NULL);
858 h = (struct stlink_usb_handle_s *)handle;
860 stlink_usb_init_buffer(handle);
862 h->txbuf[0] = STLINK_DEBUG_COMMAND;
863 h->txbuf[1] = STLINK_DEBUG_READMEM_8BIT;
864 h_u32_to_le(h->txbuf + 2, addr);
865 h_u16_to_le(h->txbuf + 2 + 4, len);
867 /* we need to fix read length for single bytes */
868 if (read_len == 1)
869 read_len++;
871 res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, read_len);
873 if (res != ERROR_OK)
874 return res;
876 memcpy(buffer, h->rxbuf, len);
878 return ERROR_OK;
881 /** */
882 static int stlink_usb_write_mem8(void *handle, uint32_t addr, uint16_t len,
883 const uint8_t *buffer)
885 int res;
886 struct stlink_usb_handle_s *h;
888 assert(handle != NULL);
890 h = (struct stlink_usb_handle_s *)handle;
892 stlink_usb_init_buffer(handle);
894 h->txbuf[0] = STLINK_DEBUG_COMMAND;
895 h->txbuf[1] = STLINK_DEBUG_WRITEMEM_8BIT;
896 h_u32_to_le(h->txbuf + 2, addr);
897 h_u16_to_le(h->txbuf + 2 + 4, len);
899 res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, 0, 0);
901 if (res != ERROR_OK)
902 return res;
904 res = stlink_usb_recv(handle, (uint8_t *) buffer, len, 0, 0);
906 if (res != ERROR_OK)
907 return res;
909 return ERROR_OK;
912 /** */
913 static int stlink_usb_read_mem32(void *handle, uint32_t addr, uint16_t len,
914 uint32_t *buffer)
916 int res;
917 struct stlink_usb_handle_s *h;
919 assert(handle != NULL);
921 h = (struct stlink_usb_handle_s *)handle;
923 stlink_usb_init_buffer(handle);
925 len *= 4;
927 h->txbuf[0] = STLINK_DEBUG_COMMAND;
928 h->txbuf[1] = STLINK_DEBUG_READMEM_32BIT;
929 h_u32_to_le(h->txbuf + 2, addr);
930 h_u16_to_le(h->txbuf + 2 + 4, len);
932 res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, len);
934 if (res != ERROR_OK)
935 return res;
937 memcpy(buffer, h->rxbuf, len);
939 return ERROR_OK;
942 /** */
943 static int stlink_usb_write_mem32(void *handle, uint32_t addr, uint16_t len,
944 const uint32_t *buffer)
946 int res;
947 struct stlink_usb_handle_s *h;
949 assert(handle != NULL);
951 h = (struct stlink_usb_handle_s *)handle;
953 stlink_usb_init_buffer(handle);
955 len *= 4;
957 h->txbuf[0] = STLINK_DEBUG_COMMAND;
958 h->txbuf[1] = STLINK_DEBUG_WRITEMEM_32BIT;
959 h_u32_to_le(h->txbuf + 2, addr);
960 h_u16_to_le(h->txbuf + 2 + 4, len);
962 res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, 0, 0);
964 if (res != ERROR_OK)
965 return res;
967 res = stlink_usb_recv(handle, (uint8_t *) buffer, len, 0, 0);
969 if (res != ERROR_OK)
970 return res;
972 return ERROR_OK;
975 /** */
976 static int stlink_usb_open(struct stlink_interface_param_s *param, void **fd)
978 int err;
979 struct stlink_usb_handle_s *h;
981 LOG_DEBUG("stlink_usb_open");
983 h = malloc(sizeof(struct stlink_usb_handle_s));
985 if (h == 0) {
986 LOG_DEBUG("malloc failed");
987 return ERROR_FAIL;
990 h->transport = param->transport;
992 const uint16_t vids[] = { param->vid, 0 };
993 const uint16_t pids[] = { param->pid, 0 };
995 LOG_DEBUG("transport: %d vid: %04x pid: %04x", param->transport,
996 param->vid, param->pid);
998 if (jtag_libusb_open(vids, pids, &h->fd) != ERROR_OK) {
999 LOG_ERROR("open failed");
1000 return ERROR_FAIL;
1003 jtag_libusb_set_configuration(h->fd, 0);
1005 if (jtag_libusb_claim_interface(h->fd, 0) != ERROR_OK) {
1006 LOG_DEBUG("claim interface failed");
1007 return ERROR_FAIL;
1010 /* wrap version for first read */
1011 switch (param->pid) {
1012 case 0x3744:
1013 h->version.stlink = 1;
1014 break;
1015 default:
1016 h->version.stlink = 2;
1017 break;
1020 /* get the device version */
1021 err = stlink_usb_version(h);
1023 if (err != ERROR_OK) {
1024 LOG_ERROR("read version failed");
1025 jtag_libusb_close(h->fd);
1026 free(h);
1027 return err;
1030 /* compare usb vid/pid */
1031 if ((param->vid != h->vid) || (param->pid != h->pid))
1032 LOG_INFO("vid/pid are not identical: %04X/%04X %04X/%04X",
1033 param->vid, param->pid,
1034 h->vid, h->pid);
1036 /* check if mode is supported */
1037 err = ERROR_OK;
1039 switch (h->transport) {
1040 case STLINK_TRANSPORT_SWD:
1041 case STLINK_TRANSPORT_JTAG:
1042 if (h->version.jtag == 0)
1043 err = ERROR_FAIL;
1044 break;
1045 case STLINK_TRANSPORT_SWIM:
1046 if (h->version.swim == 0)
1047 err = ERROR_FAIL;
1048 break;
1049 default:
1050 err = ERROR_FAIL;
1051 break;
1054 if (err != ERROR_OK) {
1055 LOG_ERROR("mode (transport) not supported by device");
1056 jtag_libusb_close(h->fd);
1057 free(h);
1058 return err;
1061 /* set the used jtag api */
1062 h->jtag_api = STLINK_JTAG_API_V1;
1064 /* initialize the debug hardware */
1065 err = stlink_usb_init_mode(h);
1067 if (err != ERROR_OK) {
1068 LOG_ERROR("init mode failed");
1069 jtag_libusb_close(h->fd);
1070 free(h);
1071 return err;
1074 *fd = h;
1076 return ERROR_OK;
1079 /** */
1080 static int stlink_usb_close(void *fd)
1082 return ERROR_OK;
1085 /** */
1086 struct stlink_layout_api_s stlink_usb_layout_api = {
1087 /** */
1088 .open = stlink_usb_open,
1089 /** */
1090 .close = stlink_usb_close,
1091 /** */
1092 .idcode = stlink_usb_idcode,
1093 /** */
1094 .state = stlink_usb_state,
1095 /** */
1096 .reset = stlink_usb_reset,
1097 /** */
1098 .run = stlink_usb_run,
1099 /** */
1100 .halt = stlink_usb_halt,
1101 /** */
1102 .step = stlink_usb_step,
1103 /** */
1104 .read_regs = stlink_usb_read_regs,
1105 /** */
1106 .read_reg = stlink_usb_read_reg,
1107 /** */
1108 .write_reg = stlink_usb_write_reg,
1109 /** */
1110 .read_mem8 = stlink_usb_read_mem8,
1111 /** */
1112 .write_mem8 = stlink_usb_write_mem8,
1113 /** */
1114 .read_mem32 = stlink_usb_read_mem32,
1115 /** */
1116 .write_mem32 = stlink_usb_write_mem32,