stlink: correctly format printed hex addresses
[openocd.git] / src / jtag / drivers / stlink_usb.c
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 ***************************************************************************/
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
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>
34
35 #include "libusb_common.h"
36
37 #define ENDPOINT_IN 0x80
38 #define ENDPOINT_OUT 0x00
39
40 #define STLINK_NULL_EP 0
41 #define STLINK_RX_EP (1|ENDPOINT_IN)
42 #define STLINK_TX_EP (2|ENDPOINT_OUT)
43 #define STLINK_SG_SIZE (31)
44 #define STLINK_DATA_SIZE (4*128)
45 #define STLINK_CMD_SIZE_V2 (16)
46 #define STLINK_CMD_SIZE_V1 (10)
47
48 enum stlink_jtag_api_version {
49 STLINK_JTAG_API_V1 = 0,
50 STLINK_JTAG_API_V2,
51 };
52
53 /** */
54 struct stlink_usb_version {
55 /** */
56 int stlink;
57 /** */
58 int jtag;
59 /** */
60 int swim;
61 /** highest supported jtag api version */
62 enum stlink_jtag_api_version jtag_api_max;
63 };
64
65 /** */
66 struct stlink_usb_handle_s {
67 /** */
68 struct jtag_libusb_device_handle *fd;
69 /** */
70 struct libusb_transfer *trans;
71 /** */
72 uint8_t cmdbuf[STLINK_SG_SIZE];
73 /** */
74 uint8_t cmdidx;
75 /** */
76 uint8_t direction;
77 /** */
78 uint8_t databuf[STLINK_DATA_SIZE];
79 /** */
80 enum stlink_transports transport;
81 /** */
82 struct stlink_usb_version version;
83 /** */
84 uint16_t vid;
85 /** */
86 uint16_t pid;
87 /** this is the currently used jtag api */
88 enum stlink_jtag_api_version jtag_api;
89 };
90
91 #define STLINK_DEBUG_ERR_OK 0x80
92 #define STLINK_DEBUG_ERR_FAULT 0x81
93 #define STLINK_CORE_RUNNING 0x80
94 #define STLINK_CORE_HALTED 0x81
95 #define STLINK_CORE_STAT_UNKNOWN -1
96
97 #define STLINK_GET_VERSION 0xF1
98 #define STLINK_DEBUG_COMMAND 0xF2
99 #define STLINK_DFU_COMMAND 0xF3
100 #define STLINK_SWIM_COMMAND 0xF4
101 #define STLINK_GET_CURRENT_MODE 0xF5
102
103 #define STLINK_DEV_DFU_MODE 0x00
104 #define STLINK_DEV_MASS_MODE 0x01
105 #define STLINK_DEV_DEBUG_MODE 0x02
106 #define STLINK_DEV_SWIM_MODE 0x03
107 #define STLINK_DEV_BOOTLOADER_MODE 0x04
108 #define STLINK_DEV_UNKNOWN_MODE -1
109
110 #define STLINK_DFU_EXIT 0x07
111
112 #define STLINK_SWIM_ENTER 0x00
113 #define STLINK_SWIM_EXIT 0x01
114
115 #define STLINK_DEBUG_ENTER_JTAG 0x00
116 #define STLINK_DEBUG_GETSTATUS 0x01
117 #define STLINK_DEBUG_FORCEDEBUG 0x02
118 #define STLINK_DEBUG_APIV1_RESETSYS 0x03
119 #define STLINK_DEBUG_APIV1_READALLREGS 0x04
120 #define STLINK_DEBUG_APIV1_READREG 0x05
121 #define STLINK_DEBUG_APIV1_WRITEREG 0x06
122 #define STLINK_DEBUG_READMEM_32BIT 0x07
123 #define STLINK_DEBUG_WRITEMEM_32BIT 0x08
124 #define STLINK_DEBUG_RUNCORE 0x09
125 #define STLINK_DEBUG_STEPCORE 0x0a
126 #define STLINK_DEBUG_APIV1_SETFP 0x0b
127 #define STLINK_DEBUG_READMEM_8BIT 0x0c
128 #define STLINK_DEBUG_WRITEMEM_8BIT 0x0d
129 #define STLINK_DEBUG_APIV1_CLEARFP 0x0e
130 #define STLINK_DEBUG_APIV1_WRITEDEBUGREG 0x0f
131 #define STLINK_DEBUG_APIV1_SETWATCHPOINT 0x10
132
133 #define STLINK_DEBUG_ENTER_JTAG 0x00
134 #define STLINK_DEBUG_ENTER_SWD 0xa3
135
136 #define STLINK_DEBUG_APIV1_ENTER 0x20
137 #define STLINK_DEBUG_EXIT 0x21
138 #define STLINK_DEBUG_READCOREID 0x22
139
140 #define STLINK_DEBUG_APIV2_ENTER 0x30
141 #define STLINK_DEBUG_APIV2_READ_IDCODES 0x31
142 #define STLINK_DEBUG_APIV2_RESETSYS 0x32
143 #define STLINK_DEBUG_APIV2_READREG 0x33
144 #define STLINK_DEBUG_APIV2_WRITEREG 0x34
145
146 #define STLINK_DEBUG_APIV2_READALLREGS 0x3A
147
148 #define STLINK_DEBUG_APIV2_DRIVE_NRST 0x3C
149
150 #define STLINK_DEBUG_APIV2_DRIVE_NRST_LOW 0x00
151 #define STLINK_DEBUG_APIV2_DRIVE_NRST_HIGH 0x01
152 #define STLINK_DEBUG_APIV2_DRIVE_NRST_PULSE 0x02
153
154 /** */
155 enum stlink_mode {
156 STLINK_MODE_UNKNOWN = 0,
157 STLINK_MODE_DFU,
158 STLINK_MODE_MASS,
159 STLINK_MODE_DEBUG_JTAG,
160 STLINK_MODE_DEBUG_SWD,
161 STLINK_MODE_DEBUG_SWIM
162 };
163
164 #define REQUEST_SENSE 0x03
165 #define REQUEST_SENSE_LENGTH 18
166
167 static void stlink_usb_init_buffer(void *handle, uint8_t direction, uint32_t size);
168
169 /** */
170 static int stlink_usb_xfer_v1_get_status(void *handle)
171 {
172 struct stlink_usb_handle_s *h;
173
174 assert(handle != NULL);
175
176 h = (struct stlink_usb_handle_s *)handle;
177
178 /* read status */
179 memset(h->cmdbuf, 0, STLINK_SG_SIZE);
180
181 if (jtag_libusb_bulk_read(h->fd, STLINK_RX_EP, (char *)h->cmdbuf,
182 13, 1000) != 13)
183 return ERROR_FAIL;
184
185 uint32_t t1;
186
187 t1 = buf_get_u32(h->cmdbuf, 0, 32);
188
189 /* check for USBS */
190 if (t1 != 0x53425355)
191 return ERROR_FAIL;
192 /*
193 * CSW status:
194 * 0 success
195 * 1 command failure
196 * 2 phase error
197 */
198 if (h->cmdbuf[12] != 0)
199 return ERROR_FAIL;
200
201 return ERROR_OK;
202 }
203
204 /** */
205 static int stlink_usb_xfer_rw(void *handle, int cmdsize, const uint8_t *buf, int size)
206 {
207 struct stlink_usb_handle_s *h;
208
209 assert(handle != NULL);
210
211 h = (struct stlink_usb_handle_s *)handle;
212
213 if (jtag_libusb_bulk_write(h->fd, STLINK_TX_EP, (char *)h->cmdbuf, cmdsize,
214 1000) != cmdsize) {
215 return ERROR_FAIL;
216 }
217
218 if (h->direction == STLINK_TX_EP && size) {
219 if (jtag_libusb_bulk_write(h->fd, STLINK_TX_EP, (char *)buf,
220 size, 1000) != size) {
221 LOG_DEBUG("bulk write failed");
222 return ERROR_FAIL;
223 }
224 } else if (h->direction == STLINK_RX_EP && size) {
225 if (jtag_libusb_bulk_read(h->fd, STLINK_RX_EP, (char *)buf,
226 size, 1000) != size) {
227 LOG_DEBUG("bulk read failed");
228 return ERROR_FAIL;
229 }
230 }
231
232 return ERROR_OK;
233 }
234
235 /** */
236 static int stlink_usb_xfer_v1_get_sense(void *handle)
237 {
238 int res;
239 struct stlink_usb_handle_s *h;
240
241 assert(handle != NULL);
242
243 h = (struct stlink_usb_handle_s *)handle;
244
245 stlink_usb_init_buffer(handle, STLINK_RX_EP, 16);
246
247 h->cmdbuf[h->cmdidx++] = REQUEST_SENSE;
248 h->cmdbuf[h->cmdidx++] = 0;
249 h->cmdbuf[h->cmdidx++] = 0;
250 h->cmdbuf[h->cmdidx++] = 0;
251 h->cmdbuf[h->cmdidx++] = REQUEST_SENSE_LENGTH;
252
253 res = stlink_usb_xfer_rw(handle, REQUEST_SENSE_LENGTH, h->databuf, 16);
254
255 if (res != ERROR_OK)
256 return res;
257
258 if (stlink_usb_xfer_v1_get_status(handle) != ERROR_OK)
259 return ERROR_FAIL;
260
261 return ERROR_OK;
262 }
263
264 /** */
265 static int stlink_usb_xfer(void *handle, const uint8_t *buf, int size)
266 {
267 int err, cmdsize = STLINK_CMD_SIZE_V2;
268 struct stlink_usb_handle_s *h;
269
270 assert(handle != NULL);
271
272 h = (struct stlink_usb_handle_s *)handle;
273
274 if (h->version.stlink == 1)
275 cmdsize = STLINK_SG_SIZE;
276
277 err = stlink_usb_xfer_rw(handle, cmdsize, buf, size);
278
279 if (err != ERROR_OK)
280 return err;
281
282 if (h->version.stlink == 1) {
283 if (stlink_usb_xfer_v1_get_status(handle) != ERROR_OK) {
284 /* check csw status */
285 if (h->cmdbuf[12] == 1) {
286 LOG_DEBUG("get sense");
287 if (stlink_usb_xfer_v1_get_sense(handle) != ERROR_OK)
288 return ERROR_FAIL;
289 }
290 return ERROR_FAIL;
291 }
292 }
293
294 return ERROR_OK;
295 }
296
297 /** */
298 static void stlink_usb_xfer_v1_create_cmd(void *handle, uint8_t direction, uint32_t size)
299 {
300 struct stlink_usb_handle_s *h;
301
302 h = (struct stlink_usb_handle_s *)handle;
303
304 /* fill the send buffer */
305 strcpy((char *)h->cmdbuf, "USBC");
306 h->cmdidx += 4;
307 /* csw tag not used */
308 h->cmdidx += 4;
309 buf_set_u32(h->cmdbuf+h->cmdidx, 0, 32, size);
310 h->cmdidx += 4;
311 h->cmdbuf[h->cmdidx++] = (direction == STLINK_RX_EP ? ENDPOINT_IN : ENDPOINT_OUT);
312 h->cmdbuf[h->cmdidx++] = 0; /* lun */
313 h->cmdbuf[h->cmdidx++] = STLINK_CMD_SIZE_V1;
314 }
315
316 /** */
317 static void stlink_usb_init_buffer(void *handle, uint8_t direction, uint32_t size)
318 {
319 struct stlink_usb_handle_s *h;
320
321 h = (struct stlink_usb_handle_s *)handle;
322
323 h->direction = direction;
324
325 h->cmdidx = 0;
326
327 memset(h->cmdbuf, 0, STLINK_SG_SIZE);
328 memset(h->databuf, 0, STLINK_DATA_SIZE);
329
330 if (h->version.stlink == 1)
331 stlink_usb_xfer_v1_create_cmd(handle, direction, size);
332 }
333
334 static const char * const stlink_usb_error_msg[] = {
335 "unknown"
336 };
337
338 /** */
339 static int stlink_usb_error_check(void *handle)
340 {
341 int res;
342 const char *err_msg = 0;
343 struct stlink_usb_handle_s *h;
344
345 assert(handle != NULL);
346
347 h = (struct stlink_usb_handle_s *)handle;
348
349 /* TODO: no error checking yet on api V1 */
350 if (h->jtag_api == STLINK_JTAG_API_V1)
351 h->databuf[0] = STLINK_DEBUG_ERR_OK;
352
353 switch (h->databuf[0]) {
354 case STLINK_DEBUG_ERR_OK:
355 res = ERROR_OK;
356 break;
357 case STLINK_DEBUG_ERR_FAULT:
358 default:
359 err_msg = stlink_usb_error_msg[0];
360 res = ERROR_FAIL;
361 break;
362 }
363
364 if (res != ERROR_OK)
365 LOG_DEBUG("status error: %d ('%s')", h->databuf[0], err_msg);
366
367 return res;
368 }
369
370 /** */
371 static int stlink_usb_version(void *handle)
372 {
373 int res;
374 uint16_t v;
375 struct stlink_usb_handle_s *h;
376
377 assert(handle != NULL);
378
379 h = (struct stlink_usb_handle_s *)handle;
380
381 stlink_usb_init_buffer(handle, STLINK_RX_EP, 6);
382
383 h->cmdbuf[h->cmdidx++] = STLINK_GET_VERSION;
384
385 res = stlink_usb_xfer(handle, h->databuf, 6);
386
387 if (res != ERROR_OK)
388 return res;
389
390 v = (h->databuf[0] << 8) | h->databuf[1];
391
392 h->version.stlink = (v >> 12) & 0x0f;
393 h->version.jtag = (v >> 6) & 0x3f;
394 h->version.swim = v & 0x3f;
395 h->vid = buf_get_u32(h->databuf, 16, 16);
396 h->pid = buf_get_u32(h->databuf, 32, 16);
397
398 /* set the supported jtag api version
399 * V1 doesn't support API V2 at all
400 * V2 support API V2 since JTAG V13
401 */
402 if ((h->version.stlink == 2) && (h->version.jtag > 12))
403 h->version.jtag_api_max = STLINK_JTAG_API_V2;
404 else
405 h->version.jtag_api_max = STLINK_JTAG_API_V1;
406
407 LOG_DEBUG("STLINK v%d JTAG v%d API v%d SWIM v%d VID 0x%04X PID 0x%04X",
408 h->version.stlink,
409 h->version.jtag,
410 (h->version.jtag_api_max == STLINK_JTAG_API_V1) ? 1 : 2,
411 h->version.swim,
412 h->vid,
413 h->pid);
414
415 return ERROR_OK;
416 }
417
418 /** */
419 static int stlink_usb_current_mode(void *handle, uint8_t *mode)
420 {
421 int res;
422 struct stlink_usb_handle_s *h;
423
424 assert(handle != NULL);
425
426 h = (struct stlink_usb_handle_s *)handle;
427
428 stlink_usb_init_buffer(handle, STLINK_RX_EP, 2);
429
430 h->cmdbuf[h->cmdidx++] = STLINK_GET_CURRENT_MODE;
431
432 res = stlink_usb_xfer(handle, h->databuf, 2);
433
434 if (res != ERROR_OK)
435 return res;
436
437 *mode = h->databuf[0];
438
439 return ERROR_OK;
440 }
441
442 /** */
443 static int stlink_usb_mode_enter(void *handle, enum stlink_mode type)
444 {
445 int res;
446 int rx_size = 0;
447 struct stlink_usb_handle_s *h;
448
449 assert(handle != NULL);
450
451 h = (struct stlink_usb_handle_s *)handle;
452
453 /* on api V2 we are able the read the latest command
454 * status
455 * TODO: we need the test on api V1 too
456 */
457 if (h->jtag_api == STLINK_JTAG_API_V2)
458 rx_size = 2;
459
460 stlink_usb_init_buffer(handle, STLINK_RX_EP, rx_size);
461
462 switch (type) {
463 case STLINK_MODE_DEBUG_JTAG:
464 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
465 if (h->jtag_api == STLINK_JTAG_API_V1)
466 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_ENTER;
467 else
468 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_ENTER;
469 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_ENTER_JTAG;
470 break;
471 case STLINK_MODE_DEBUG_SWD:
472 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
473 if (h->jtag_api == STLINK_JTAG_API_V1)
474 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_ENTER;
475 else
476 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_ENTER;
477 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_ENTER_SWD;
478 break;
479 case STLINK_MODE_DEBUG_SWIM:
480 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_COMMAND;
481 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_ENTER;
482 break;
483 case STLINK_MODE_DFU:
484 case STLINK_MODE_MASS:
485 default:
486 return ERROR_FAIL;
487 }
488
489 res = stlink_usb_xfer(handle, h->databuf, rx_size);
490
491 if (res != ERROR_OK)
492 return res;
493
494 res = stlink_usb_error_check(h);
495
496 return res;
497 }
498
499 /** */
500 static int stlink_usb_mode_leave(void *handle, enum stlink_mode type)
501 {
502 int res;
503 struct stlink_usb_handle_s *h;
504
505 assert(handle != NULL);
506
507 h = (struct stlink_usb_handle_s *)handle;
508
509 stlink_usb_init_buffer(handle, STLINK_NULL_EP, 0);
510
511 switch (type) {
512 case STLINK_MODE_DEBUG_JTAG:
513 case STLINK_MODE_DEBUG_SWD:
514 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
515 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_EXIT;
516 break;
517 case STLINK_MODE_DEBUG_SWIM:
518 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_COMMAND;
519 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_EXIT;
520 break;
521 case STLINK_MODE_DFU:
522 h->cmdbuf[h->cmdidx++] = STLINK_DFU_COMMAND;
523 h->cmdbuf[h->cmdidx++] = STLINK_DFU_EXIT;
524 break;
525 case STLINK_MODE_MASS:
526 default:
527 return ERROR_FAIL;
528 }
529
530 res = stlink_usb_xfer(handle, 0, 0);
531
532 if (res != ERROR_OK)
533 return res;
534
535 return ERROR_OK;
536 }
537
538 /** */
539 static int stlink_usb_init_mode(void *handle)
540 {
541 int res;
542 uint8_t mode;
543 enum stlink_mode emode;
544 struct stlink_usb_handle_s *h;
545
546 assert(handle != NULL);
547
548 h = (struct stlink_usb_handle_s *)handle;
549
550 res = stlink_usb_current_mode(handle, &mode);
551
552 if (res != ERROR_OK)
553 return res;
554
555 LOG_DEBUG("MODE: 0x%02X", mode);
556
557 /* try to exit current mode */
558 switch (mode) {
559 case STLINK_DEV_DFU_MODE:
560 emode = STLINK_MODE_DFU;
561 break;
562 case STLINK_DEV_DEBUG_MODE:
563 emode = STLINK_MODE_DEBUG_SWD;
564 break;
565 case STLINK_DEV_SWIM_MODE:
566 emode = STLINK_MODE_DEBUG_SWIM;
567 break;
568 case STLINK_DEV_BOOTLOADER_MODE:
569 case STLINK_DEV_MASS_MODE:
570 default:
571 emode = STLINK_MODE_UNKNOWN;
572 break;
573 }
574
575 if (emode != STLINK_MODE_UNKNOWN) {
576 res = stlink_usb_mode_leave(handle, emode);
577
578 if (res != ERROR_OK)
579 return res;
580 }
581
582 res = stlink_usb_current_mode(handle, &mode);
583
584 if (res != ERROR_OK)
585 return res;
586
587 LOG_DEBUG("MODE: 0x%02X", mode);
588
589 /* set selected mode */
590 switch (h->transport) {
591 case STLINK_TRANSPORT_SWD:
592 emode = STLINK_MODE_DEBUG_SWD;
593 break;
594 case STLINK_TRANSPORT_JTAG:
595 emode = STLINK_MODE_DEBUG_JTAG;
596 break;
597 case STLINK_TRANSPORT_SWIM:
598 emode = STLINK_MODE_DEBUG_SWIM;
599 break;
600 default:
601 emode = STLINK_MODE_UNKNOWN;
602 break;
603 }
604
605 if (emode == STLINK_MODE_UNKNOWN) {
606 LOG_ERROR("selected mode (transport) not supported");
607 return ERROR_FAIL;
608 }
609
610 res = stlink_usb_mode_enter(handle, emode);
611
612 if (res != ERROR_OK)
613 return res;
614
615 res = stlink_usb_current_mode(handle, &mode);
616
617 if (res != ERROR_OK)
618 return res;
619
620 LOG_DEBUG("MODE: 0x%02X", mode);
621
622 return ERROR_OK;
623 }
624
625 /** */
626 static int stlink_usb_idcode(void *handle, uint32_t *idcode)
627 {
628 int res;
629 struct stlink_usb_handle_s *h;
630
631 assert(handle != NULL);
632
633 h = (struct stlink_usb_handle_s *)handle;
634
635 stlink_usb_init_buffer(handle, STLINK_RX_EP, 4);
636
637 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
638 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_READCOREID;
639
640 res = stlink_usb_xfer(handle, h->databuf, 4);
641
642 if (res != ERROR_OK)
643 return res;
644
645 *idcode = le_to_h_u32(h->databuf);
646
647 LOG_DEBUG("IDCODE: 0x%08X", *idcode);
648
649 return ERROR_OK;
650 }
651
652 /** */
653 static enum target_state stlink_usb_state(void *handle)
654 {
655 int res;
656 struct stlink_usb_handle_s *h;
657
658 assert(handle != NULL);
659
660 h = (struct stlink_usb_handle_s *)handle;
661
662 if (h->jtag_api == STLINK_JTAG_API_V2)
663 return TARGET_UNKNOWN;
664
665 stlink_usb_init_buffer(handle, STLINK_RX_EP, 2);
666
667 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
668 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_GETSTATUS;
669
670 res = stlink_usb_xfer(handle, h->databuf, 2);
671
672 if (res != ERROR_OK)
673 return TARGET_UNKNOWN;
674
675 if (h->databuf[0] == STLINK_CORE_RUNNING)
676 return TARGET_RUNNING;
677 if (h->databuf[0] == STLINK_CORE_HALTED)
678 return TARGET_HALTED;
679
680 return TARGET_UNKNOWN;
681 }
682
683 /** */
684 static int stlink_usb_reset(void *handle)
685 {
686 int res;
687 struct stlink_usb_handle_s *h;
688
689 assert(handle != NULL);
690
691 h = (struct stlink_usb_handle_s *)handle;
692
693 stlink_usb_init_buffer(handle, STLINK_RX_EP, 2);
694
695 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
696
697 if (h->jtag_api == STLINK_JTAG_API_V1)
698 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_RESETSYS;
699 else
700 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_RESETSYS;
701
702 res = stlink_usb_xfer(handle, h->databuf, 2);
703
704 if (res != ERROR_OK)
705 return res;
706
707 LOG_DEBUG("RESET: 0x%08X", h->databuf[0]);
708
709 return ERROR_OK;
710 }
711
712 /** */
713 static int stlink_usb_run(void *handle)
714 {
715 int res;
716 struct stlink_usb_handle_s *h;
717
718 assert(handle != NULL);
719
720 h = (struct stlink_usb_handle_s *)handle;
721
722 if (h->jtag_api == STLINK_JTAG_API_V2)
723 return ERROR_FAIL;
724
725 stlink_usb_init_buffer(handle, STLINK_RX_EP, 2);
726
727 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
728 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_RUNCORE;
729
730 res = stlink_usb_xfer(handle, h->databuf, 2);
731
732 if (res != ERROR_OK)
733 return res;
734
735 return ERROR_OK;
736 }
737
738 /** */
739 static int stlink_usb_halt(void *handle)
740 {
741 int res;
742 struct stlink_usb_handle_s *h;
743
744 assert(handle != NULL);
745
746 h = (struct stlink_usb_handle_s *)handle;
747
748 if (h->jtag_api == STLINK_JTAG_API_V2)
749 return ERROR_FAIL;
750
751 stlink_usb_init_buffer(handle, STLINK_RX_EP, 2);
752
753 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
754 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_FORCEDEBUG;
755
756 res = stlink_usb_xfer(handle, h->databuf, 2);
757
758 if (res != ERROR_OK)
759 return res;
760
761 return ERROR_OK;
762 }
763
764 /** */
765 static int stlink_usb_step(void *handle)
766 {
767 int res;
768 struct stlink_usb_handle_s *h;
769
770 assert(handle != NULL);
771
772 h = (struct stlink_usb_handle_s *)handle;
773
774 if (h->jtag_api == STLINK_JTAG_API_V2)
775 return ERROR_FAIL;
776
777 stlink_usb_init_buffer(handle, STLINK_RX_EP, 2);
778
779 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
780 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_STEPCORE;
781
782 res = stlink_usb_xfer(handle, h->databuf, 2);
783
784 if (res != ERROR_OK)
785 return res;
786
787 return ERROR_OK;
788 }
789
790 /** */
791 static int stlink_usb_read_regs(void *handle)
792 {
793 int res;
794 struct stlink_usb_handle_s *h;
795
796 assert(handle != NULL);
797
798 h = (struct stlink_usb_handle_s *)handle;
799
800 stlink_usb_init_buffer(handle, STLINK_RX_EP, 84);
801
802 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
803 if (h->jtag_api == STLINK_JTAG_API_V1)
804 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_READALLREGS;
805 else
806 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_READALLREGS;
807
808 res = stlink_usb_xfer(handle, h->databuf, 84);
809
810 if (res != ERROR_OK)
811 return res;
812
813 return ERROR_OK;
814 }
815
816 /** */
817 static int stlink_usb_read_reg(void *handle, int num, uint32_t *val)
818 {
819 int res;
820 struct stlink_usb_handle_s *h;
821
822 assert(handle != NULL);
823
824 h = (struct stlink_usb_handle_s *)handle;
825
826 stlink_usb_init_buffer(handle, STLINK_RX_EP, 4);
827
828 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
829 if (h->jtag_api == STLINK_JTAG_API_V1)
830 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_READREG;
831 else
832 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_READREG;
833 h->cmdbuf[h->cmdidx++] = num;
834
835 res = stlink_usb_xfer(handle, h->databuf, 4);
836
837 if (res != ERROR_OK)
838 return res;
839
840 *val = le_to_h_u32(h->databuf);
841
842 return ERROR_OK;
843 }
844
845 /** */
846 static int stlink_usb_write_reg(void *handle, int num, uint32_t val)
847 {
848 int res;
849 struct stlink_usb_handle_s *h;
850
851 assert(handle != NULL);
852
853 h = (struct stlink_usb_handle_s *)handle;
854
855 stlink_usb_init_buffer(handle, STLINK_RX_EP, 2);
856
857 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
858 if (h->jtag_api == STLINK_JTAG_API_V1)
859 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_WRITEREG;
860 else
861 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_WRITEREG;
862 h->cmdbuf[h->cmdidx++] = num;
863 h_u32_to_le(h->cmdbuf+h->cmdidx, val);
864 h->cmdidx += 4;
865
866 res = stlink_usb_xfer(handle, h->databuf, 2);
867
868 if (res != ERROR_OK)
869 return res;
870
871 return ERROR_OK;
872 }
873
874 /** */
875 static int stlink_usb_read_mem8(void *handle, uint32_t addr, uint16_t len,
876 uint8_t *buffer)
877 {
878 int res;
879 uint16_t read_len = len;
880 struct stlink_usb_handle_s *h;
881
882 assert(handle != NULL);
883
884 h = (struct stlink_usb_handle_s *)handle;
885
886 stlink_usb_init_buffer(handle, STLINK_RX_EP, read_len);
887
888 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
889 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_READMEM_8BIT;
890 h_u32_to_le(h->cmdbuf+h->cmdidx, addr);
891 h->cmdidx += 4;
892 h_u16_to_le(h->cmdbuf+h->cmdidx, len);
893 h->cmdidx += 2;
894
895 /* we need to fix read length for single bytes */
896 if (read_len == 1)
897 read_len++;
898
899 res = stlink_usb_xfer(handle, h->databuf, read_len);
900
901 if (res != ERROR_OK)
902 return res;
903
904 memcpy(buffer, h->databuf, len);
905
906 return ERROR_OK;
907 }
908
909 /** */
910 static int stlink_usb_write_mem8(void *handle, uint32_t addr, uint16_t len,
911 const uint8_t *buffer)
912 {
913 int res;
914 struct stlink_usb_handle_s *h;
915
916 assert(handle != NULL);
917
918 h = (struct stlink_usb_handle_s *)handle;
919
920 stlink_usb_init_buffer(handle, STLINK_TX_EP, len);
921
922 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
923 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_WRITEMEM_8BIT;
924 h_u32_to_le(h->cmdbuf+h->cmdidx, addr);
925 h->cmdidx += 4;
926 h_u16_to_le(h->cmdbuf+h->cmdidx, len);
927 h->cmdidx += 2;
928
929 res = stlink_usb_xfer(handle, buffer, len);
930
931 if (res != ERROR_OK)
932 return res;
933
934 return ERROR_OK;
935 }
936
937 /** */
938 static int stlink_usb_read_mem32(void *handle, uint32_t addr, uint16_t len,
939 uint8_t *buffer)
940 {
941 int res;
942 struct stlink_usb_handle_s *h;
943
944 assert(handle != NULL);
945
946 h = (struct stlink_usb_handle_s *)handle;
947
948 len *= 4;
949
950 stlink_usb_init_buffer(handle, STLINK_RX_EP, len);
951
952 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
953 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_READMEM_32BIT;
954 h_u32_to_le(h->cmdbuf+h->cmdidx, addr);
955 h->cmdidx += 4;
956 h_u16_to_le(h->cmdbuf+h->cmdidx, len);
957 h->cmdidx += 2;
958
959 res = stlink_usb_xfer(handle, h->databuf, len);
960
961 if (res != ERROR_OK)
962 return res;
963
964 memcpy(buffer, h->databuf, len);
965
966 return ERROR_OK;
967 }
968
969 /** */
970 static int stlink_usb_write_mem32(void *handle, uint32_t addr, uint16_t len,
971 const uint8_t *buffer)
972 {
973 int res;
974 struct stlink_usb_handle_s *h;
975
976 assert(handle != NULL);
977
978 h = (struct stlink_usb_handle_s *)handle;
979
980 len *= 4;
981
982 stlink_usb_init_buffer(handle, STLINK_TX_EP, len);
983
984 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
985 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_WRITEMEM_32BIT;
986 h_u32_to_le(h->cmdbuf+h->cmdidx, addr);
987 h->cmdidx += 4;
988 h_u16_to_le(h->cmdbuf+h->cmdidx, len);
989 h->cmdidx += 2;
990
991 res = stlink_usb_xfer(handle, buffer, len);
992
993 if (res != ERROR_OK)
994 return res;
995
996 return ERROR_OK;
997 }
998
999 /** */
1000 static int stlink_usb_open(struct stlink_interface_param_s *param, void **fd)
1001 {
1002 int err;
1003 struct stlink_usb_handle_s *h;
1004
1005 LOG_DEBUG("stlink_usb_open");
1006
1007 h = malloc(sizeof(struct stlink_usb_handle_s));
1008
1009 if (h == 0) {
1010 LOG_DEBUG("malloc failed");
1011 return ERROR_FAIL;
1012 }
1013
1014 h->transport = param->transport;
1015
1016 const uint16_t vids[] = { param->vid, 0 };
1017 const uint16_t pids[] = { param->pid, 0 };
1018
1019 LOG_DEBUG("transport: %d vid: 0x%04x pid: 0x%04x", param->transport,
1020 param->vid, param->pid);
1021
1022 if (jtag_libusb_open(vids, pids, &h->fd) != ERROR_OK) {
1023 LOG_ERROR("open failed");
1024 return ERROR_FAIL;
1025 }
1026
1027 jtag_libusb_set_configuration(h->fd, 0);
1028
1029 if (jtag_libusb_claim_interface(h->fd, 0) != ERROR_OK) {
1030 LOG_DEBUG("claim interface failed");
1031 return ERROR_FAIL;
1032 }
1033
1034 /* wrap version for first read */
1035 switch (param->pid) {
1036 case 0x3744:
1037 h->version.stlink = 1;
1038 break;
1039 default:
1040 h->version.stlink = 2;
1041 break;
1042 }
1043
1044 /* get the device version */
1045 err = stlink_usb_version(h);
1046
1047 if (err != ERROR_OK) {
1048 LOG_ERROR("read version failed");
1049 jtag_libusb_close(h->fd);
1050 free(h);
1051 return err;
1052 }
1053
1054 /* compare usb vid/pid */
1055 if ((param->vid != h->vid) || (param->pid != h->pid))
1056 LOG_INFO("vid/pid are not identical: 0x%04X/0x%04X 0x%04X/0x%04X",
1057 param->vid, param->pid,
1058 h->vid, h->pid);
1059
1060 /* check if mode is supported */
1061 err = ERROR_OK;
1062
1063 switch (h->transport) {
1064 case STLINK_TRANSPORT_SWD:
1065 case STLINK_TRANSPORT_JTAG:
1066 if (h->version.jtag == 0)
1067 err = ERROR_FAIL;
1068 break;
1069 case STLINK_TRANSPORT_SWIM:
1070 if (h->version.swim == 0)
1071 err = ERROR_FAIL;
1072 break;
1073 default:
1074 err = ERROR_FAIL;
1075 break;
1076 }
1077
1078 if (err != ERROR_OK) {
1079 LOG_ERROR("mode (transport) not supported by device");
1080 jtag_libusb_close(h->fd);
1081 free(h);
1082 return err;
1083 }
1084
1085 /* set the used jtag api */
1086 h->jtag_api = STLINK_JTAG_API_V1;
1087
1088 /* initialize the debug hardware */
1089 err = stlink_usb_init_mode(h);
1090
1091 if (err != ERROR_OK) {
1092 LOG_ERROR("init mode failed");
1093 jtag_libusb_close(h->fd);
1094 free(h);
1095 return err;
1096 }
1097
1098 *fd = h;
1099
1100 return ERROR_OK;
1101 }
1102
1103 /** */
1104 static int stlink_usb_close(void *fd)
1105 {
1106 return ERROR_OK;
1107 }
1108
1109 /** */
1110 struct stlink_layout_api_s stlink_usb_layout_api = {
1111 /** */
1112 .open = stlink_usb_open,
1113 /** */
1114 .close = stlink_usb_close,
1115 /** */
1116 .idcode = stlink_usb_idcode,
1117 /** */
1118 .state = stlink_usb_state,
1119 /** */
1120 .reset = stlink_usb_reset,
1121 /** */
1122 .run = stlink_usb_run,
1123 /** */
1124 .halt = stlink_usb_halt,
1125 /** */
1126 .step = stlink_usb_step,
1127 /** */
1128 .read_regs = stlink_usb_read_regs,
1129 /** */
1130 .read_reg = stlink_usb_read_reg,
1131 /** */
1132 .write_reg = stlink_usb_write_reg,
1133 /** */
1134 .read_mem8 = stlink_usb_read_mem8,
1135 /** */
1136 .write_mem8 = stlink_usb_write_mem8,
1137 /** */
1138 .read_mem32 = stlink_usb_read_mem32,
1139 /** */
1140 .write_mem32 = stlink_usb_write_mem32,
1141 };

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)