stlink: add STLINK_F_HAS_SWD_SET_FREQ
[openocd.git] / src / jtag / drivers / stlink_usb.c
1 /***************************************************************************
2 * SWIM contributions by Ake Rehnman *
3 * Copyright (C) 2017 Ake Rehnman *
4 * ake.rehnman(at)gmail.com *
5 * *
6 * Copyright (C) 2011-2012 by Mathias Kuester *
7 * Mathias Kuester <kesmtp@freenet.de> *
8 * *
9 * Copyright (C) 2012 by Spencer Oliver *
10 * spen@spen-soft.co.uk *
11 * *
12 * This code is based on https://github.com/texane/stlink *
13 * *
14 * This program is free software; you can redistribute it and/or modify *
15 * it under the terms of the GNU General Public License as published by *
16 * the Free Software Foundation; either version 2 of the License, or *
17 * (at your option) any later version. *
18 * *
19 * This program is distributed in the hope that it will be useful, *
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
22 * GNU General Public License for more details. *
23 * *
24 * You should have received a copy of the GNU General Public License *
25 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
26 ***************************************************************************/
27
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31
32 /* project specific includes */
33 #include <helper/binarybuffer.h>
34 #include <jtag/interface.h>
35 #include <jtag/hla/hla_layout.h>
36 #include <jtag/hla/hla_transport.h>
37 #include <jtag/hla/hla_interface.h>
38 #include <target/target.h>
39
40 #include <target/cortex_m.h>
41
42 #include "libusb_common.h"
43
44 #define ENDPOINT_IN 0x80
45 #define ENDPOINT_OUT 0x00
46
47 #define STLINK_WRITE_TIMEOUT 1000
48 #define STLINK_READ_TIMEOUT 1000
49
50 #define STLINK_NULL_EP 0
51 #define STLINK_RX_EP (1|ENDPOINT_IN)
52 #define STLINK_TX_EP (2|ENDPOINT_OUT)
53 #define STLINK_TRACE_EP (3|ENDPOINT_IN)
54
55 #define STLINK_V2_1_TX_EP (1|ENDPOINT_OUT)
56 #define STLINK_V2_1_TRACE_EP (2|ENDPOINT_IN)
57
58 #define STLINK_SG_SIZE (31)
59 #define STLINK_DATA_SIZE (4096)
60 #define STLINK_CMD_SIZE_V2 (16)
61 #define STLINK_CMD_SIZE_V1 (10)
62
63 #define STLINK_V1_PID (0x3744)
64 #define STLINK_V2_PID (0x3748)
65 #define STLINK_V2_1_PID (0x374B)
66 #define STLINK_V2_1_NO_MSD_PID (0x3752)
67
68 /* the current implementation of the stlink limits
69 * 8bit read/writes to max 64 bytes. */
70 #define STLINK_MAX_RW8 (64)
71
72 /* "WAIT" responses will be retried (with exponential backoff) at
73 * most this many times before failing to caller.
74 */
75 #define MAX_WAIT_RETRIES 8
76
77 enum stlink_jtag_api_version {
78 STLINK_JTAG_API_V1 = 1,
79 STLINK_JTAG_API_V2,
80 };
81
82 /** */
83 struct stlink_usb_version {
84 /** */
85 int stlink;
86 /** */
87 int jtag;
88 /** */
89 int swim;
90 /** highest supported jtag api version */
91 enum stlink_jtag_api_version jtag_api_max;
92 /** one bit for each feature supported. See macros STLINK_F_* */
93 uint32_t flags;
94 };
95
96 /** */
97 struct stlink_usb_handle_s {
98 /** */
99 struct jtag_libusb_device_handle *fd;
100 /** */
101 struct libusb_transfer *trans;
102 /** */
103 uint8_t rx_ep;
104 /** */
105 uint8_t tx_ep;
106 /** */
107 uint8_t trace_ep;
108 /** */
109 uint8_t cmdbuf[STLINK_SG_SIZE];
110 /** */
111 uint8_t cmdidx;
112 /** */
113 uint8_t direction;
114 /** */
115 uint8_t databuf[STLINK_DATA_SIZE];
116 /** */
117 uint32_t max_mem_packet;
118 /** */
119 enum hl_transports transport;
120 /** */
121 struct stlink_usb_version version;
122 /** */
123 uint16_t vid;
124 /** */
125 uint16_t pid;
126 /** this is the currently used jtag api */
127 enum stlink_jtag_api_version jtag_api;
128 /** */
129 struct {
130 /** whether SWO tracing is enabled or not */
131 bool enabled;
132 /** trace module source clock */
133 uint32_t source_hz;
134 } trace;
135 /** reconnect is needed next time we try to query the
136 * status */
137 bool reconnect_pending;
138 };
139
140 #define STLINK_SWIM_ERR_OK 0x00
141 #define STLINK_SWIM_BUSY 0x01
142 #define STLINK_DEBUG_ERR_OK 0x80
143 #define STLINK_DEBUG_ERR_FAULT 0x81
144 #define STLINK_SWD_AP_WAIT 0x10
145 #define STLINK_SWD_AP_FAULT 0x11
146 #define STLINK_SWD_AP_ERROR 0x12
147 #define STLINK_SWD_AP_PARITY_ERROR 0x13
148 #define STLINK_JTAG_WRITE_ERROR 0x0c
149 #define STLINK_JTAG_WRITE_VERIF_ERROR 0x0d
150 #define STLINK_SWD_DP_WAIT 0x14
151 #define STLINK_SWD_DP_FAULT 0x15
152 #define STLINK_SWD_DP_ERROR 0x16
153 #define STLINK_SWD_DP_PARITY_ERROR 0x17
154
155 #define STLINK_SWD_AP_WDATA_ERROR 0x18
156 #define STLINK_SWD_AP_STICKY_ERROR 0x19
157 #define STLINK_SWD_AP_STICKYORUN_ERROR 0x1a
158
159 #define STLINK_CORE_RUNNING 0x80
160 #define STLINK_CORE_HALTED 0x81
161 #define STLINK_CORE_STAT_UNKNOWN -1
162
163 #define STLINK_GET_VERSION 0xF1
164 #define STLINK_DEBUG_COMMAND 0xF2
165 #define STLINK_DFU_COMMAND 0xF3
166 #define STLINK_SWIM_COMMAND 0xF4
167 #define STLINK_GET_CURRENT_MODE 0xF5
168 #define STLINK_GET_TARGET_VOLTAGE 0xF7
169
170 #define STLINK_DEV_DFU_MODE 0x00
171 #define STLINK_DEV_MASS_MODE 0x01
172 #define STLINK_DEV_DEBUG_MODE 0x02
173 #define STLINK_DEV_SWIM_MODE 0x03
174 #define STLINK_DEV_BOOTLOADER_MODE 0x04
175 #define STLINK_DEV_UNKNOWN_MODE -1
176
177 #define STLINK_DFU_EXIT 0x07
178
179 /*
180 STLINK_SWIM_ENTER_SEQ
181 1.3ms low then 750Hz then 1.5kHz
182
183 STLINK_SWIM_GEN_RST
184 STM8 DM pulls reset pin low 50us
185
186 STLINK_SWIM_SPEED
187 uint8_t (0=low|1=high)
188
189 STLINK_SWIM_WRITEMEM
190 uint16_t length
191 uint32_t address
192
193 STLINK_SWIM_RESET
194 send syncronization seq (16us low, response 64 clocks low)
195 */
196 #define STLINK_SWIM_ENTER 0x00
197 #define STLINK_SWIM_EXIT 0x01
198 #define STLINK_SWIM_READ_CAP 0x02
199 #define STLINK_SWIM_SPEED 0x03
200 #define STLINK_SWIM_ENTER_SEQ 0x04
201 #define STLINK_SWIM_GEN_RST 0x05
202 #define STLINK_SWIM_RESET 0x06
203 #define STLINK_SWIM_ASSERT_RESET 0x07
204 #define STLINK_SWIM_DEASSERT_RESET 0x08
205 #define STLINK_SWIM_READSTATUS 0x09
206 #define STLINK_SWIM_WRITEMEM 0x0a
207 #define STLINK_SWIM_READMEM 0x0b
208 #define STLINK_SWIM_READBUF 0x0c
209
210 #define STLINK_DEBUG_ENTER_JTAG 0x00
211 #define STLINK_DEBUG_GETSTATUS 0x01
212 #define STLINK_DEBUG_FORCEDEBUG 0x02
213 #define STLINK_DEBUG_APIV1_RESETSYS 0x03
214 #define STLINK_DEBUG_APIV1_READALLREGS 0x04
215 #define STLINK_DEBUG_APIV1_READREG 0x05
216 #define STLINK_DEBUG_APIV1_WRITEREG 0x06
217 #define STLINK_DEBUG_READMEM_32BIT 0x07
218 #define STLINK_DEBUG_WRITEMEM_32BIT 0x08
219 #define STLINK_DEBUG_RUNCORE 0x09
220 #define STLINK_DEBUG_STEPCORE 0x0a
221 #define STLINK_DEBUG_APIV1_SETFP 0x0b
222 #define STLINK_DEBUG_READMEM_8BIT 0x0c
223 #define STLINK_DEBUG_WRITEMEM_8BIT 0x0d
224 #define STLINK_DEBUG_APIV1_CLEARFP 0x0e
225 #define STLINK_DEBUG_APIV1_WRITEDEBUGREG 0x0f
226 #define STLINK_DEBUG_APIV1_SETWATCHPOINT 0x10
227
228 #define STLINK_DEBUG_ENTER_JTAG 0x00
229 #define STLINK_DEBUG_ENTER_SWD 0xa3
230
231 #define STLINK_DEBUG_APIV1_ENTER 0x20
232 #define STLINK_DEBUG_EXIT 0x21
233 #define STLINK_DEBUG_READCOREID 0x22
234
235 #define STLINK_DEBUG_APIV2_ENTER 0x30
236 #define STLINK_DEBUG_APIV2_READ_IDCODES 0x31
237 #define STLINK_DEBUG_APIV2_RESETSYS 0x32
238 #define STLINK_DEBUG_APIV2_READREG 0x33
239 #define STLINK_DEBUG_APIV2_WRITEREG 0x34
240 #define STLINK_DEBUG_APIV2_WRITEDEBUGREG 0x35
241 #define STLINK_DEBUG_APIV2_READDEBUGREG 0x36
242
243 #define STLINK_DEBUG_APIV2_READALLREGS 0x3A
244 #define STLINK_DEBUG_APIV2_GETLASTRWSTATUS 0x3B
245 #define STLINK_DEBUG_APIV2_DRIVE_NRST 0x3C
246
247 #define STLINK_DEBUG_APIV2_START_TRACE_RX 0x40
248 #define STLINK_DEBUG_APIV2_STOP_TRACE_RX 0x41
249 #define STLINK_DEBUG_APIV2_GET_TRACE_NB 0x42
250 #define STLINK_DEBUG_APIV2_SWD_SET_FREQ 0x43
251 #define STLINK_DEBUG_APIV2_JTAG_SET_FREQ 0x44
252
253 #define STLINK_DEBUG_APIV2_READMEM_16BIT 0x47
254 #define STLINK_DEBUG_APIV2_WRITEMEM_16BIT 0x48
255
256 #define STLINK_DEBUG_APIV2_DRIVE_NRST_LOW 0x00
257 #define STLINK_DEBUG_APIV2_DRIVE_NRST_HIGH 0x01
258 #define STLINK_DEBUG_APIV2_DRIVE_NRST_PULSE 0x02
259
260 #define STLINK_TRACE_SIZE 4096
261 #define STLINK_TRACE_MAX_HZ 2000000
262
263 /** */
264 enum stlink_mode {
265 STLINK_MODE_UNKNOWN = 0,
266 STLINK_MODE_DFU,
267 STLINK_MODE_MASS,
268 STLINK_MODE_DEBUG_JTAG,
269 STLINK_MODE_DEBUG_SWD,
270 STLINK_MODE_DEBUG_SWIM
271 };
272
273 #define REQUEST_SENSE 0x03
274 #define REQUEST_SENSE_LENGTH 18
275
276 /*
277 * Map the relevant features, quirks and workaround for specific firmware
278 * version of stlink
279 */
280 #define STLINK_F_HAS_TRACE (1UL << 0)
281 #define STLINK_F_HAS_SWD_SET_FREQ (1UL << 1)
282
283 /* aliases */
284 #define STLINK_F_HAS_TARGET_VOLT STLINK_F_HAS_TRACE
285
286 struct speed_map {
287 int speed;
288 int speed_divisor;
289 };
290
291 /* SWD clock speed */
292 static const struct speed_map stlink_khz_to_speed_map_swd[] = {
293 {4000, 0},
294 {1800, 1}, /* default */
295 {1200, 2},
296 {950, 3},
297 {480, 7},
298 {240, 15},
299 {125, 31},
300 {100, 40},
301 {50, 79},
302 {25, 158},
303 {15, 265},
304 {5, 798}
305 };
306
307 /* JTAG clock speed */
308 static const struct speed_map stlink_khz_to_speed_map_jtag[] = {
309 {18000, 2},
310 {9000, 4},
311 {4500, 8},
312 {2250, 16},
313 {1125, 32}, /* default */
314 {562, 64},
315 {281, 128},
316 {140, 256}
317 };
318
319 static void stlink_usb_init_buffer(void *handle, uint8_t direction, uint32_t size);
320 static int stlink_swim_status(void *handle);
321
322 /** */
323 static int stlink_usb_xfer_v1_get_status(void *handle)
324 {
325 struct stlink_usb_handle_s *h = handle;
326
327 assert(handle != NULL);
328
329 /* read status */
330 memset(h->cmdbuf, 0, STLINK_SG_SIZE);
331
332 if (jtag_libusb_bulk_read(h->fd, h->rx_ep, (char *)h->cmdbuf,
333 13, STLINK_READ_TIMEOUT) != 13)
334 return ERROR_FAIL;
335
336 uint32_t t1;
337
338 t1 = buf_get_u32(h->cmdbuf, 0, 32);
339
340 /* check for USBS */
341 if (t1 != 0x53425355)
342 return ERROR_FAIL;
343 /*
344 * CSW status:
345 * 0 success
346 * 1 command failure
347 * 2 phase error
348 */
349 if (h->cmdbuf[12] != 0)
350 return ERROR_FAIL;
351
352 return ERROR_OK;
353 }
354
355 /** */
356 static int stlink_usb_xfer_rw(void *handle, int cmdsize, const uint8_t *buf, int size)
357 {
358 struct stlink_usb_handle_s *h = handle;
359
360 assert(handle != NULL);
361
362 if (jtag_libusb_bulk_write(h->fd, h->tx_ep, (char *)h->cmdbuf, cmdsize,
363 STLINK_WRITE_TIMEOUT) != cmdsize) {
364 return ERROR_FAIL;
365 }
366
367 if (h->direction == h->tx_ep && size) {
368 if (jtag_libusb_bulk_write(h->fd, h->tx_ep, (char *)buf,
369 size, STLINK_WRITE_TIMEOUT) != size) {
370 LOG_DEBUG("bulk write failed");
371 return ERROR_FAIL;
372 }
373 } else if (h->direction == h->rx_ep && size) {
374 if (jtag_libusb_bulk_read(h->fd, h->rx_ep, (char *)buf,
375 size, STLINK_READ_TIMEOUT) != size) {
376 LOG_DEBUG("bulk read failed");
377 return ERROR_FAIL;
378 }
379 }
380
381 return ERROR_OK;
382 }
383
384 /** */
385 static int stlink_usb_xfer_v1_get_sense(void *handle)
386 {
387 int res;
388 struct stlink_usb_handle_s *h = handle;
389
390 assert(handle != NULL);
391
392 stlink_usb_init_buffer(handle, h->rx_ep, 16);
393
394 h->cmdbuf[h->cmdidx++] = REQUEST_SENSE;
395 h->cmdbuf[h->cmdidx++] = 0;
396 h->cmdbuf[h->cmdidx++] = 0;
397 h->cmdbuf[h->cmdidx++] = 0;
398 h->cmdbuf[h->cmdidx++] = REQUEST_SENSE_LENGTH;
399
400 res = stlink_usb_xfer_rw(handle, REQUEST_SENSE_LENGTH, h->databuf, 16);
401
402 if (res != ERROR_OK)
403 return res;
404
405 if (stlink_usb_xfer_v1_get_status(handle) != ERROR_OK)
406 return ERROR_FAIL;
407
408 return ERROR_OK;
409 }
410
411 /*
412 transfers block in cmdbuf
413 <size> indicates number of bytes in the following
414 data phase.
415 */
416 static int stlink_usb_xfer(void *handle, const uint8_t *buf, int size)
417 {
418 int err, cmdsize = STLINK_CMD_SIZE_V2;
419 struct stlink_usb_handle_s *h = handle;
420
421 assert(handle != NULL);
422
423 if (h->version.stlink == 1) {
424 cmdsize = STLINK_SG_SIZE;
425 /* put length in bCBWCBLength */
426 h->cmdbuf[14] = h->cmdidx-15;
427 }
428
429 err = stlink_usb_xfer_rw(handle, cmdsize, buf, size);
430
431 if (err != ERROR_OK)
432 return err;
433
434 if (h->version.stlink == 1) {
435 if (stlink_usb_xfer_v1_get_status(handle) != ERROR_OK) {
436 /* check csw status */
437 if (h->cmdbuf[12] == 1) {
438 LOG_DEBUG("get sense");
439 if (stlink_usb_xfer_v1_get_sense(handle) != ERROR_OK)
440 return ERROR_FAIL;
441 }
442 return ERROR_FAIL;
443 }
444 }
445
446 return ERROR_OK;
447 }
448
449 /**
450 Converts an STLINK status code held in the first byte of a response
451 to an openocd error, logs any error/wait status as debug output.
452 */
453 static int stlink_usb_error_check(void *handle)
454 {
455 struct stlink_usb_handle_s *h = handle;
456
457 assert(handle != NULL);
458
459 if (h->transport == HL_TRANSPORT_SWIM) {
460 switch (h->databuf[0]) {
461 case STLINK_SWIM_ERR_OK:
462 return ERROR_OK;
463 case STLINK_SWIM_BUSY:
464 return ERROR_WAIT;
465 default:
466 LOG_DEBUG("unknown/unexpected STLINK status code 0x%x", h->databuf[0]);
467 return ERROR_FAIL;
468 }
469 }
470
471 /* TODO: no error checking yet on api V1 */
472 if (h->jtag_api == STLINK_JTAG_API_V1)
473 h->databuf[0] = STLINK_DEBUG_ERR_OK;
474
475 switch (h->databuf[0]) {
476 case STLINK_DEBUG_ERR_OK:
477 return ERROR_OK;
478 case STLINK_DEBUG_ERR_FAULT:
479 LOG_DEBUG("SWD fault response (0x%x)", STLINK_DEBUG_ERR_FAULT);
480 return ERROR_FAIL;
481 case STLINK_SWD_AP_WAIT:
482 LOG_DEBUG("wait status SWD_AP_WAIT (0x%x)", STLINK_SWD_AP_WAIT);
483 return ERROR_WAIT;
484 case STLINK_SWD_DP_WAIT:
485 LOG_DEBUG("wait status SWD_DP_WAIT (0x%x)", STLINK_SWD_DP_WAIT);
486 return ERROR_WAIT;
487 case STLINK_JTAG_WRITE_ERROR:
488 LOG_DEBUG("Write error");
489 return ERROR_FAIL;
490 case STLINK_JTAG_WRITE_VERIF_ERROR:
491 LOG_DEBUG("Write verify error, ignoring");
492 return ERROR_OK;
493 case STLINK_SWD_AP_FAULT:
494 /* git://git.ac6.fr/openocd commit 657e3e885b9ee10
495 * returns ERROR_OK with the comment:
496 * Change in error status when reading outside RAM.
497 * This fix allows CDT plugin to visualize memory.
498 */
499 LOG_DEBUG("STLINK_SWD_AP_FAULT");
500 return ERROR_FAIL;
501 case STLINK_SWD_AP_ERROR:
502 LOG_DEBUG("STLINK_SWD_AP_ERROR");
503 return ERROR_FAIL;
504 case STLINK_SWD_AP_PARITY_ERROR:
505 LOG_DEBUG("STLINK_SWD_AP_PARITY_ERROR");
506 return ERROR_FAIL;
507 case STLINK_SWD_DP_FAULT:
508 LOG_DEBUG("STLINK_SWD_DP_FAULT");
509 return ERROR_FAIL;
510 case STLINK_SWD_DP_ERROR:
511 LOG_DEBUG("STLINK_SWD_DP_ERROR");
512 return ERROR_FAIL;
513 case STLINK_SWD_DP_PARITY_ERROR:
514 LOG_DEBUG("STLINK_SWD_DP_PARITY_ERROR");
515 return ERROR_FAIL;
516 case STLINK_SWD_AP_WDATA_ERROR:
517 LOG_DEBUG("STLINK_SWD_AP_WDATA_ERROR");
518 return ERROR_FAIL;
519 case STLINK_SWD_AP_STICKY_ERROR:
520 LOG_DEBUG("STLINK_SWD_AP_STICKY_ERROR");
521 return ERROR_FAIL;
522 case STLINK_SWD_AP_STICKYORUN_ERROR:
523 LOG_DEBUG("STLINK_SWD_AP_STICKYORUN_ERROR");
524 return ERROR_FAIL;
525 default:
526 LOG_DEBUG("unknown/unexpected STLINK status code 0x%x", h->databuf[0]);
527 return ERROR_FAIL;
528 }
529 }
530
531
532 /** Issue an STLINK command via USB transfer, with retries on any wait status responses.
533
534 Works for commands where the STLINK_DEBUG status is returned in the first
535 byte of the response packet. For SWIM a SWIM_READSTATUS is requested instead.
536
537 Returns an openocd result code.
538 */
539 static int stlink_cmd_allow_retry(void *handle, const uint8_t *buf, int size)
540 {
541 int retries = 0;
542 int res;
543 struct stlink_usb_handle_s *h = handle;
544
545 while (1) {
546 if ((h->transport != HL_TRANSPORT_SWIM) || !retries) {
547 res = stlink_usb_xfer(handle, buf, size);
548 if (res != ERROR_OK)
549 return res;
550 }
551
552 if (h->transport == HL_TRANSPORT_SWIM) {
553 res = stlink_swim_status(handle);
554 if (res != ERROR_OK)
555 return res;
556 }
557
558 res = stlink_usb_error_check(handle);
559 if (res == ERROR_WAIT && retries < MAX_WAIT_RETRIES) {
560 usleep((1<<retries++) * 1000);
561 continue;
562 }
563 return res;
564 }
565 }
566
567 /** */
568 static int stlink_usb_read_trace(void *handle, const uint8_t *buf, int size)
569 {
570 struct stlink_usb_handle_s *h = handle;
571
572 assert(handle != NULL);
573
574 assert(h->version.flags & STLINK_F_HAS_TRACE);
575
576 if (jtag_libusb_bulk_read(h->fd, h->trace_ep, (char *)buf,
577 size, STLINK_READ_TIMEOUT) != size) {
578 LOG_ERROR("bulk trace read failed");
579 return ERROR_FAIL;
580 }
581
582 return ERROR_OK;
583 }
584
585 /*
586 this function writes transfer length in
587 the right place in the cb
588 */
589 static void stlink_usb_set_cbw_transfer_datalength(void *handle, uint32_t size)
590 {
591 struct stlink_usb_handle_s *h = handle;
592
593 buf_set_u32(h->cmdbuf+8, 0, 32, size);
594 }
595
596 static void stlink_usb_xfer_v1_create_cmd(void *handle, uint8_t direction, uint32_t size)
597 {
598 struct stlink_usb_handle_s *h = handle;
599
600 /* fill the send buffer */
601 strcpy((char *)h->cmdbuf, "USBC");
602 h->cmdidx += 4;
603 /* csw tag not used */
604 buf_set_u32(h->cmdbuf+h->cmdidx, 0, 32, 0);
605 h->cmdidx += 4;
606 /* cbw data transfer length (in the following data phase in or out) */
607 buf_set_u32(h->cmdbuf+h->cmdidx, 0, 32, size);
608 h->cmdidx += 4;
609 /* cbw flags */
610 h->cmdbuf[h->cmdidx++] = (direction == h->rx_ep ? ENDPOINT_IN : ENDPOINT_OUT);
611 h->cmdbuf[h->cmdidx++] = 0; /* lun */
612 /* cdb clength (is filled in at xfer) */
613 h->cmdbuf[h->cmdidx++] = 0;
614 }
615
616 /** */
617 static void stlink_usb_init_buffer(void *handle, uint8_t direction, uint32_t size)
618 {
619 struct stlink_usb_handle_s *h = handle;
620
621 h->direction = direction;
622
623 h->cmdidx = 0;
624
625 memset(h->cmdbuf, 0, STLINK_SG_SIZE);
626 memset(h->databuf, 0, STLINK_DATA_SIZE);
627
628 if (h->version.stlink == 1)
629 stlink_usb_xfer_v1_create_cmd(handle, direction, size);
630 }
631
632 /** */
633 static int stlink_usb_version(void *handle)
634 {
635 int res;
636 uint32_t flags;
637 uint16_t v;
638 struct stlink_usb_handle_s *h = handle;
639
640 assert(handle != NULL);
641
642 stlink_usb_init_buffer(handle, h->rx_ep, 6);
643
644 h->cmdbuf[h->cmdidx++] = STLINK_GET_VERSION;
645
646 res = stlink_usb_xfer(handle, h->databuf, 6);
647
648 if (res != ERROR_OK)
649 return res;
650
651 v = (h->databuf[0] << 8) | h->databuf[1];
652
653 h->version.stlink = (v >> 12) & 0x0f;
654 h->version.jtag = (v >> 6) & 0x3f;
655 h->version.swim = v & 0x3f;
656 h->vid = buf_get_u32(h->databuf, 16, 16);
657 h->pid = buf_get_u32(h->databuf, 32, 16);
658
659 flags = 0;
660 switch (h->version.stlink) {
661 case 1:
662 /* ST-LINK/V1 from J11 switch to api-v2 (and support SWD) */
663 if (h->version.jtag >= 11)
664 h->version.jtag_api_max = STLINK_JTAG_API_V2;
665 else
666 h->version.jtag_api_max = STLINK_JTAG_API_V1;
667
668 break;
669 case 2:
670 /* all ST-LINK/V2 and ST-Link/V2.1 use api-v2 */
671 h->version.jtag_api_max = STLINK_JTAG_API_V2;
672
673 /* API for trace from J13 */
674 /* API for target voltage from J13 */
675 if (h->version.jtag >= 13)
676 flags |= STLINK_F_HAS_TRACE;
677
678 /* API to set SWD frequency from J22 */
679 if (h->version.jtag >= 22)
680 flags |= STLINK_F_HAS_SWD_SET_FREQ;
681
682 break;
683 default:
684 break;
685 }
686 h->version.flags = flags;
687
688 LOG_INFO("STLINK v%d JTAG v%d API v%d SWIM v%d VID 0x%04X PID 0x%04X",
689 h->version.stlink,
690 h->version.jtag,
691 (h->version.jtag_api_max == STLINK_JTAG_API_V1) ? 1 : 2,
692 h->version.swim,
693 h->vid,
694 h->pid);
695
696 return ERROR_OK;
697 }
698
699 static int stlink_usb_check_voltage(void *handle, float *target_voltage)
700 {
701 struct stlink_usb_handle_s *h = handle;
702 uint32_t adc_results[2];
703
704 /* no error message, simply quit with error */
705 if (!(h->version.flags & STLINK_F_HAS_TARGET_VOLT))
706 return ERROR_COMMAND_NOTFOUND;
707
708 stlink_usb_init_buffer(handle, h->rx_ep, 8);
709
710 h->cmdbuf[h->cmdidx++] = STLINK_GET_TARGET_VOLTAGE;
711
712 int result = stlink_usb_xfer(handle, h->databuf, 8);
713
714 if (result != ERROR_OK)
715 return result;
716
717 /* convert result */
718 adc_results[0] = le_to_h_u32(h->databuf);
719 adc_results[1] = le_to_h_u32(h->databuf + 4);
720
721 *target_voltage = 0;
722
723 if (adc_results[0])
724 *target_voltage = 2 * ((float)adc_results[1]) * (float)(1.2 / adc_results[0]);
725
726 LOG_INFO("Target voltage: %f", (double)*target_voltage);
727
728 return ERROR_OK;
729 }
730
731 static int stlink_usb_set_swdclk(void *handle, uint16_t clk_divisor)
732 {
733 struct stlink_usb_handle_s *h = handle;
734
735 assert(handle != NULL);
736
737 if (!(h->version.flags & STLINK_F_HAS_SWD_SET_FREQ))
738 return ERROR_COMMAND_NOTFOUND;
739
740 stlink_usb_init_buffer(handle, h->rx_ep, 2);
741
742 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
743 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_SWD_SET_FREQ;
744 h_u16_to_le(h->cmdbuf+h->cmdidx, clk_divisor);
745 h->cmdidx += 2;
746
747 int result = stlink_cmd_allow_retry(handle, h->databuf, 2);
748
749 if (result != ERROR_OK)
750 return result;
751
752 return ERROR_OK;
753 }
754
755 static int stlink_usb_set_jtagclk(void *handle, uint16_t clk_divisor)
756 {
757 struct stlink_usb_handle_s *h = handle;
758
759 assert(handle != NULL);
760
761 /* only supported by stlink/v2 and for firmware >= 24 */
762 if (h->version.stlink == 1 || h->version.jtag < 24)
763 return ERROR_COMMAND_NOTFOUND;
764
765 stlink_usb_init_buffer(handle, h->rx_ep, 2);
766
767 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
768 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_JTAG_SET_FREQ;
769 h_u16_to_le(h->cmdbuf+h->cmdidx, clk_divisor);
770 h->cmdidx += 2;
771
772 int result = stlink_cmd_allow_retry(handle, h->databuf, 2);
773
774 if (result != ERROR_OK)
775 return result;
776
777 return ERROR_OK;
778 }
779
780 /** */
781 static int stlink_usb_current_mode(void *handle, uint8_t *mode)
782 {
783 int res;
784 struct stlink_usb_handle_s *h = handle;
785
786 assert(handle != NULL);
787
788 stlink_usb_init_buffer(handle, h->rx_ep, 2);
789
790 h->cmdbuf[h->cmdidx++] = STLINK_GET_CURRENT_MODE;
791
792 res = stlink_usb_xfer(handle, h->databuf, 2);
793
794 if (res != ERROR_OK)
795 return res;
796
797 *mode = h->databuf[0];
798
799 return ERROR_OK;
800 }
801
802 /** */
803 static int stlink_usb_mode_enter(void *handle, enum stlink_mode type)
804 {
805 int rx_size = 0;
806 struct stlink_usb_handle_s *h = handle;
807
808 assert(handle != NULL);
809
810 /* on api V2 we are able the read the latest command
811 * status
812 * TODO: we need the test on api V1 too
813 */
814 if (h->jtag_api == STLINK_JTAG_API_V2)
815 rx_size = 2;
816
817 stlink_usb_init_buffer(handle, h->rx_ep, rx_size);
818
819 switch (type) {
820 case STLINK_MODE_DEBUG_JTAG:
821 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
822 if (h->jtag_api == STLINK_JTAG_API_V1)
823 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_ENTER;
824 else
825 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_ENTER;
826 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_ENTER_JTAG;
827 break;
828 case STLINK_MODE_DEBUG_SWD:
829 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
830 if (h->jtag_api == STLINK_JTAG_API_V1)
831 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_ENTER;
832 else
833 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_ENTER;
834 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_ENTER_SWD;
835 break;
836 case STLINK_MODE_DEBUG_SWIM:
837 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_COMMAND;
838 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_ENTER;
839 /* no answer for this function... */
840 rx_size = 0;
841 break;
842 case STLINK_MODE_DFU:
843 case STLINK_MODE_MASS:
844 default:
845 return ERROR_FAIL;
846 }
847
848 return stlink_cmd_allow_retry(handle, h->databuf, rx_size);
849 }
850
851 /** */
852 static int stlink_usb_mode_leave(void *handle, enum stlink_mode type)
853 {
854 int res;
855 struct stlink_usb_handle_s *h = handle;
856
857 assert(handle != NULL);
858
859 stlink_usb_init_buffer(handle, STLINK_NULL_EP, 0);
860
861 switch (type) {
862 case STLINK_MODE_DEBUG_JTAG:
863 case STLINK_MODE_DEBUG_SWD:
864 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
865 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_EXIT;
866 break;
867 case STLINK_MODE_DEBUG_SWIM:
868 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_COMMAND;
869 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_EXIT;
870 break;
871 case STLINK_MODE_DFU:
872 h->cmdbuf[h->cmdidx++] = STLINK_DFU_COMMAND;
873 h->cmdbuf[h->cmdidx++] = STLINK_DFU_EXIT;
874 break;
875 case STLINK_MODE_MASS:
876 default:
877 return ERROR_FAIL;
878 }
879
880 res = stlink_usb_xfer(handle, 0, 0);
881
882 if (res != ERROR_OK)
883 return res;
884
885 return ERROR_OK;
886 }
887
888 static int stlink_usb_assert_srst(void *handle, int srst);
889
890 static enum stlink_mode stlink_get_mode(enum hl_transports t)
891 {
892 switch (t) {
893 case HL_TRANSPORT_SWD:
894 return STLINK_MODE_DEBUG_SWD;
895 case HL_TRANSPORT_JTAG:
896 return STLINK_MODE_DEBUG_JTAG;
897 case HL_TRANSPORT_SWIM:
898 return STLINK_MODE_DEBUG_SWIM;
899 default:
900 return STLINK_MODE_UNKNOWN;
901 }
902 }
903
904 /** */
905 static int stlink_usb_init_mode(void *handle, bool connect_under_reset)
906 {
907 int res;
908 uint8_t mode;
909 enum stlink_mode emode;
910 struct stlink_usb_handle_s *h = handle;
911
912 assert(handle != NULL);
913
914 res = stlink_usb_current_mode(handle, &mode);
915
916 if (res != ERROR_OK)
917 return res;
918
919 LOG_DEBUG("MODE: 0x%02X", mode);
920
921 /* try to exit current mode */
922 switch (mode) {
923 case STLINK_DEV_DFU_MODE:
924 emode = STLINK_MODE_DFU;
925 break;
926 case STLINK_DEV_DEBUG_MODE:
927 emode = STLINK_MODE_DEBUG_SWD;
928 break;
929 case STLINK_DEV_SWIM_MODE:
930 emode = STLINK_MODE_DEBUG_SWIM;
931 break;
932 case STLINK_DEV_BOOTLOADER_MODE:
933 case STLINK_DEV_MASS_MODE:
934 default:
935 emode = STLINK_MODE_UNKNOWN;
936 break;
937 }
938
939 if (emode != STLINK_MODE_UNKNOWN) {
940 res = stlink_usb_mode_leave(handle, emode);
941
942 if (res != ERROR_OK)
943 return res;
944 }
945
946 res = stlink_usb_current_mode(handle, &mode);
947
948 if (res != ERROR_OK)
949 return res;
950
951 /* we check the target voltage here as an aid to debugging connection problems.
952 * the stlink requires the target Vdd to be connected for reliable debugging.
953 * this cmd is supported in all modes except DFU
954 */
955 if (mode != STLINK_DEV_DFU_MODE) {
956
957 float target_voltage;
958
959 /* check target voltage (if supported) */
960 res = stlink_usb_check_voltage(h, &target_voltage);
961
962 if (res != ERROR_OK) {
963 if (res != ERROR_COMMAND_NOTFOUND)
964 LOG_ERROR("voltage check failed");
965 /* attempt to continue as it is not a catastrophic failure */
966 } else {
967 /* check for a sensible target voltage, operating range is 1.65-5.5v
968 * according to datasheet */
969 if (target_voltage < 1.5)
970 LOG_ERROR("target voltage may be too low for reliable debugging");
971 }
972 }
973
974 LOG_DEBUG("MODE: 0x%02X", mode);
975
976 /* set selected mode */
977 emode = stlink_get_mode(h->transport);
978
979 if (emode == STLINK_MODE_UNKNOWN) {
980 LOG_ERROR("selected mode (transport) not supported");
981 return ERROR_FAIL;
982 }
983
984 /* preliminary SRST assert:
985 * We want SRST is asserted before activating debug signals (mode_enter).
986 * As the required mode has not been set, the adapter may not know what pin to use.
987 * Tested firmware STLINK v2 JTAG v29 API v2 SWIM v0 uses T_NRST pin by default
988 * Tested firmware STLINK v2 JTAG v27 API v2 SWIM v6 uses T_NRST pin by default
989 * after power on, SWIM_RST stays unchanged */
990 if (connect_under_reset && emode != STLINK_MODE_DEBUG_SWIM)
991 stlink_usb_assert_srst(handle, 0);
992 /* do not check the return status here, we will
993 proceed and enter the desired mode below
994 and try asserting srst again. */
995
996 res = stlink_usb_mode_enter(handle, emode);
997 if (res != ERROR_OK)
998 return res;
999
1000 /* assert SRST again: a little bit late but now the adapter knows for sure what pin to use */
1001 if (connect_under_reset) {
1002 res = stlink_usb_assert_srst(handle, 0);
1003 if (res != ERROR_OK)
1004 return res;
1005 }
1006
1007 res = stlink_usb_current_mode(handle, &mode);
1008
1009 if (res != ERROR_OK)
1010 return res;
1011
1012 LOG_DEBUG("MODE: 0x%02X", mode);
1013
1014 return ERROR_OK;
1015 }
1016
1017 /* request status from last swim request */
1018 static int stlink_swim_status(void *handle)
1019 {
1020 struct stlink_usb_handle_s *h = handle;
1021 int res;
1022
1023 stlink_usb_init_buffer(handle, h->rx_ep, 4);
1024 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_COMMAND;
1025 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_READSTATUS;
1026 res = stlink_usb_xfer(handle, h->databuf, 4);
1027 if (res != ERROR_OK)
1028 return res;
1029 return ERROR_OK;
1030 }
1031 /*
1032 the purpose of this function is unknown...
1033 capabilites? anyway for swim v6 it returns
1034 0001020600000000
1035 */
1036 __attribute__((unused))
1037 static int stlink_swim_cap(void *handle, uint8_t *cap)
1038 {
1039 struct stlink_usb_handle_s *h = handle;
1040 int res;
1041
1042 stlink_usb_init_buffer(handle, h->rx_ep, 8);
1043 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_COMMAND;
1044 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_READ_CAP;
1045 h->cmdbuf[h->cmdidx++] = 0x01;
1046 res = stlink_usb_xfer(handle, h->databuf, 8);
1047 if (res != ERROR_OK)
1048 return res;
1049 memcpy(cap, h->databuf, 8);
1050 return ERROR_OK;
1051 }
1052
1053 /* debug dongle assert/deassert sreset line */
1054 static int stlink_swim_assert_reset(void *handle, int reset)
1055 {
1056 struct stlink_usb_handle_s *h = handle;
1057 int res;
1058
1059 stlink_usb_init_buffer(handle, h->rx_ep, 0);
1060 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_COMMAND;
1061 if (!reset)
1062 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_ASSERT_RESET;
1063 else
1064 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_DEASSERT_RESET;
1065 res = stlink_cmd_allow_retry(handle, h->databuf, 0);
1066 if (res != ERROR_OK)
1067 return res;
1068 return ERROR_OK;
1069 }
1070
1071 /*
1072 send swim enter seq
1073 1.3ms low then 750Hz then 1.5kHz
1074 */
1075 static int stlink_swim_enter(void *handle)
1076 {
1077 struct stlink_usb_handle_s *h = handle;
1078 int res;
1079
1080 stlink_usb_init_buffer(handle, h->rx_ep, 0);
1081 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_COMMAND;
1082 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_ENTER_SEQ;
1083 res = stlink_cmd_allow_retry(handle, h->databuf, 0);
1084 if (res != ERROR_OK)
1085 return res;
1086 return ERROR_OK;
1087 }
1088
1089 /* switch high/low speed swim */
1090 static int stlink_swim_speed(void *handle, int speed)
1091 {
1092 struct stlink_usb_handle_s *h = handle;
1093 int res;
1094
1095 stlink_usb_init_buffer(handle, h->rx_ep, 0);
1096 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_COMMAND;
1097 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_SPEED;
1098 if (speed)
1099 h->cmdbuf[h->cmdidx++] = 1;
1100 else
1101 h->cmdbuf[h->cmdidx++] = 0;
1102 res = stlink_cmd_allow_retry(handle, h->databuf, 0);
1103 if (res != ERROR_OK)
1104 return res;
1105 return ERROR_OK;
1106 }
1107
1108 /*
1109 initiate srst from swim.
1110 nrst is pulled low for 50us.
1111 */
1112 static int stlink_swim_generate_rst(void *handle)
1113 {
1114 struct stlink_usb_handle_s *h = handle;
1115 int res;
1116
1117 stlink_usb_init_buffer(handle, h->rx_ep, 0);
1118 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_COMMAND;
1119 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_GEN_RST;
1120 res = stlink_cmd_allow_retry(handle, h->databuf, 0);
1121 if (res != ERROR_OK)
1122 return res;
1123 return ERROR_OK;
1124 }
1125
1126 /*
1127 send resyncronize sequence
1128 swim is pulled low for 16us
1129 reply is 64 clks low
1130 */
1131 static int stlink_swim_resync(void *handle)
1132 {
1133 struct stlink_usb_handle_s *h = handle;
1134 int res;
1135
1136 stlink_usb_init_buffer(handle, h->rx_ep, 0);
1137 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_COMMAND;
1138 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_RESET;
1139 res = stlink_cmd_allow_retry(handle, h->databuf, 0);
1140 if (res != ERROR_OK)
1141 return res;
1142 return ERROR_OK;
1143 }
1144
1145 static int stlink_swim_writebytes(void *handle, uint32_t addr, uint32_t len, const uint8_t *data)
1146 {
1147 struct stlink_usb_handle_s *h = handle;
1148 int res;
1149 unsigned int i;
1150 unsigned int datalen = 0;
1151 int cmdsize = STLINK_CMD_SIZE_V2;
1152
1153 if (len > STLINK_DATA_SIZE)
1154 return ERROR_FAIL;
1155
1156 if (h->version.stlink == 1)
1157 cmdsize = STLINK_SG_SIZE;
1158
1159 stlink_usb_init_buffer(handle, h->tx_ep, 0);
1160 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_COMMAND;
1161 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_WRITEMEM;
1162 h_u16_to_be(h->cmdbuf+h->cmdidx, len);
1163 h->cmdidx += 2;
1164 h_u32_to_be(h->cmdbuf+h->cmdidx, addr);
1165 h->cmdidx += 4;
1166 for (i = 0; i < len; i++) {
1167 if (h->cmdidx == cmdsize)
1168 h->databuf[datalen++] = *(data++);
1169 else
1170 h->cmdbuf[h->cmdidx++] = *(data++);
1171 }
1172 if (h->version.stlink == 1)
1173 stlink_usb_set_cbw_transfer_datalength(handle, datalen);
1174
1175 res = stlink_cmd_allow_retry(handle, h->databuf, datalen);
1176 if (res != ERROR_OK)
1177 return res;
1178 return ERROR_OK;
1179 }
1180
1181 static int stlink_swim_readbytes(void *handle, uint32_t addr, uint32_t len, uint8_t *data)
1182 {
1183 struct stlink_usb_handle_s *h = handle;
1184 int res;
1185
1186 if (len > STLINK_DATA_SIZE)
1187 return ERROR_FAIL;
1188
1189 stlink_usb_init_buffer(handle, h->rx_ep, 0);
1190 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_COMMAND;
1191 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_READMEM;
1192 h_u16_to_be(h->cmdbuf+h->cmdidx, len);
1193 h->cmdidx += 2;
1194 h_u32_to_be(h->cmdbuf+h->cmdidx, addr);
1195 h->cmdidx += 4;
1196 res = stlink_cmd_allow_retry(handle, h->databuf, 0);
1197 if (res != ERROR_OK)
1198 return res;
1199
1200 stlink_usb_init_buffer(handle, h->rx_ep, len);
1201 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_COMMAND;
1202 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_READBUF;
1203 res = stlink_usb_xfer(handle, data, len);
1204 if (res != ERROR_OK)
1205 return res;
1206
1207 return ERROR_OK;
1208 }
1209
1210 /** */
1211 static int stlink_usb_idcode(void *handle, uint32_t *idcode)
1212 {
1213 int res;
1214 struct stlink_usb_handle_s *h = handle;
1215
1216 assert(handle != NULL);
1217
1218 /* there is no swim read core id cmd */
1219 if (h->transport == HL_TRANSPORT_SWIM) {
1220 *idcode = 0;
1221 return ERROR_OK;
1222 }
1223
1224 stlink_usb_init_buffer(handle, h->rx_ep, 4);
1225
1226 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1227 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_READCOREID;
1228
1229 res = stlink_usb_xfer(handle, h->databuf, 4);
1230
1231 if (res != ERROR_OK)
1232 return res;
1233
1234 *idcode = le_to_h_u32(h->databuf);
1235
1236 LOG_DEBUG("IDCODE: 0x%08" PRIX32, *idcode);
1237
1238 return ERROR_OK;
1239 }
1240
1241 static int stlink_usb_v2_read_debug_reg(void *handle, uint32_t addr, uint32_t *val)
1242 {
1243 struct stlink_usb_handle_s *h = handle;
1244 int res;
1245
1246 assert(handle != NULL);
1247
1248 stlink_usb_init_buffer(handle, h->rx_ep, 8);
1249
1250 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1251 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_READDEBUGREG;
1252 h_u32_to_le(h->cmdbuf+h->cmdidx, addr);
1253 h->cmdidx += 4;
1254
1255 res = stlink_cmd_allow_retry(handle, h->databuf, 8);
1256 if (res != ERROR_OK)
1257 return res;
1258
1259 *val = le_to_h_u32(h->databuf + 4);
1260 return ERROR_OK;
1261 }
1262
1263 static int stlink_usb_write_debug_reg(void *handle, uint32_t addr, uint32_t val)
1264 {
1265 struct stlink_usb_handle_s *h = handle;
1266
1267 assert(handle != NULL);
1268
1269 stlink_usb_init_buffer(handle, h->rx_ep, 2);
1270
1271 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1272 if (h->jtag_api == STLINK_JTAG_API_V1)
1273 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_WRITEDEBUGREG;
1274 else
1275 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_WRITEDEBUGREG;
1276 h_u32_to_le(h->cmdbuf+h->cmdidx, addr);
1277 h->cmdidx += 4;
1278 h_u32_to_le(h->cmdbuf+h->cmdidx, val);
1279 h->cmdidx += 4;
1280
1281 return stlink_cmd_allow_retry(handle, h->databuf, 2);
1282 }
1283
1284 /** */
1285 static int stlink_usb_trace_read(void *handle, uint8_t *buf, size_t *size)
1286 {
1287 struct stlink_usb_handle_s *h = handle;
1288
1289 assert(handle != NULL);
1290
1291 if (h->trace.enabled && (h->version.flags & STLINK_F_HAS_TRACE)) {
1292 int res;
1293
1294 stlink_usb_init_buffer(handle, h->rx_ep, 10);
1295
1296 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1297 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_GET_TRACE_NB;
1298
1299 res = stlink_usb_xfer(handle, h->databuf, 2);
1300 if (res != ERROR_OK)
1301 return res;
1302
1303 size_t bytes_avail = le_to_h_u16(h->databuf);
1304 *size = bytes_avail < *size ? bytes_avail : *size - 1;
1305
1306 if (*size > 0) {
1307 res = stlink_usb_read_trace(handle, buf, *size);
1308 if (res != ERROR_OK)
1309 return res;
1310 return ERROR_OK;
1311 }
1312 }
1313 *size = 0;
1314 return ERROR_OK;
1315 }
1316
1317 static enum target_state stlink_usb_v2_get_status(void *handle)
1318 {
1319 int result;
1320 uint32_t status;
1321
1322 result = stlink_usb_v2_read_debug_reg(handle, DCB_DHCSR, &status);
1323 if (result != ERROR_OK)
1324 return TARGET_UNKNOWN;
1325
1326 if (status & S_HALT)
1327 return TARGET_HALTED;
1328 else if (status & S_RESET_ST)
1329 return TARGET_RESET;
1330
1331 return TARGET_RUNNING;
1332 }
1333
1334 /** */
1335 static enum target_state stlink_usb_state(void *handle)
1336 {
1337 int res;
1338 struct stlink_usb_handle_s *h = handle;
1339
1340 assert(handle != NULL);
1341
1342 if (h->transport == HL_TRANSPORT_SWIM) {
1343 res = stlink_usb_mode_enter(handle, stlink_get_mode(h->transport));
1344 if (res != ERROR_OK)
1345 return TARGET_UNKNOWN;
1346
1347 res = stlink_swim_resync(handle);
1348 if (res != ERROR_OK)
1349 return TARGET_UNKNOWN;
1350
1351 return ERROR_OK;
1352 }
1353
1354 if (h->reconnect_pending) {
1355 LOG_INFO("Previous state query failed, trying to reconnect");
1356 res = stlink_usb_mode_enter(handle, stlink_get_mode(h->transport));
1357
1358 if (res != ERROR_OK)
1359 return TARGET_UNKNOWN;
1360
1361 h->reconnect_pending = false;
1362 }
1363
1364 if (h->jtag_api == STLINK_JTAG_API_V2) {
1365 res = stlink_usb_v2_get_status(handle);
1366 if (res == TARGET_UNKNOWN)
1367 h->reconnect_pending = true;
1368 return res;
1369 }
1370
1371 stlink_usb_init_buffer(handle, h->rx_ep, 2);
1372
1373 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1374 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_GETSTATUS;
1375
1376 res = stlink_usb_xfer(handle, h->databuf, 2);
1377
1378 if (res != ERROR_OK)
1379 return TARGET_UNKNOWN;
1380
1381 if (h->databuf[0] == STLINK_CORE_RUNNING)
1382 return TARGET_RUNNING;
1383 if (h->databuf[0] == STLINK_CORE_HALTED)
1384 return TARGET_HALTED;
1385
1386 h->reconnect_pending = true;
1387
1388 return TARGET_UNKNOWN;
1389 }
1390
1391 static int stlink_usb_assert_srst(void *handle, int srst)
1392 {
1393 struct stlink_usb_handle_s *h = handle;
1394
1395 assert(handle != NULL);
1396
1397 if (h->transport == HL_TRANSPORT_SWIM)
1398 return stlink_swim_assert_reset(handle, srst);
1399
1400 if (h->version.stlink == 1)
1401 return ERROR_COMMAND_NOTFOUND;
1402
1403 stlink_usb_init_buffer(handle, h->rx_ep, 2);
1404
1405 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1406 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_DRIVE_NRST;
1407 h->cmdbuf[h->cmdidx++] = srst;
1408
1409 return stlink_cmd_allow_retry(handle, h->databuf, 2);
1410 }
1411
1412 /** */
1413 static void stlink_usb_trace_disable(void *handle)
1414 {
1415 int res = ERROR_OK;
1416 struct stlink_usb_handle_s *h = handle;
1417
1418 assert(handle != NULL);
1419
1420 assert(h->version.flags & STLINK_F_HAS_TRACE);
1421
1422 LOG_DEBUG("Tracing: disable");
1423
1424 stlink_usb_init_buffer(handle, h->rx_ep, 2);
1425 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1426 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_STOP_TRACE_RX;
1427 res = stlink_usb_xfer(handle, h->databuf, 2);
1428
1429 if (res == ERROR_OK)
1430 h->trace.enabled = false;
1431 }
1432
1433
1434 /** */
1435 static int stlink_usb_trace_enable(void *handle)
1436 {
1437 int res;
1438 struct stlink_usb_handle_s *h = handle;
1439
1440 assert(handle != NULL);
1441
1442 if (h->version.flags & STLINK_F_HAS_TRACE) {
1443 stlink_usb_init_buffer(handle, h->rx_ep, 10);
1444
1445 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1446 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_START_TRACE_RX;
1447 h_u16_to_le(h->cmdbuf+h->cmdidx, (uint16_t)STLINK_TRACE_SIZE);
1448 h->cmdidx += 2;
1449 h_u32_to_le(h->cmdbuf+h->cmdidx, h->trace.source_hz);
1450 h->cmdidx += 4;
1451
1452 res = stlink_usb_xfer(handle, h->databuf, 2);
1453
1454 if (res == ERROR_OK) {
1455 h->trace.enabled = true;
1456 LOG_DEBUG("Tracing: recording at %" PRIu32 "Hz", h->trace.source_hz);
1457 }
1458 } else {
1459 LOG_ERROR("Tracing is not supported by this version.");
1460 res = ERROR_FAIL;
1461 }
1462
1463 return res;
1464 }
1465
1466 /** */
1467 static int stlink_usb_reset(void *handle)
1468 {
1469 struct stlink_usb_handle_s *h = handle;
1470 int retval;
1471
1472 assert(handle != NULL);
1473
1474 if (h->transport == HL_TRANSPORT_SWIM)
1475 return stlink_swim_generate_rst(handle);
1476
1477 stlink_usb_init_buffer(handle, h->rx_ep, 2);
1478
1479 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1480
1481 if (h->jtag_api == STLINK_JTAG_API_V1)
1482 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_RESETSYS;
1483 else
1484 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_RESETSYS;
1485
1486 retval = stlink_cmd_allow_retry(handle, h->databuf, 2);
1487 if (retval != ERROR_OK)
1488 return retval;
1489
1490 if (h->trace.enabled) {
1491 stlink_usb_trace_disable(h);
1492 return stlink_usb_trace_enable(h);
1493 }
1494
1495 return ERROR_OK;
1496 }
1497
1498 /** */
1499 static int stlink_usb_run(void *handle)
1500 {
1501 int res;
1502 struct stlink_usb_handle_s *h = handle;
1503
1504 assert(handle != NULL);
1505
1506 if (h->jtag_api == STLINK_JTAG_API_V2) {
1507 res = stlink_usb_write_debug_reg(handle, DCB_DHCSR, DBGKEY|C_DEBUGEN);
1508
1509 return res;
1510 }
1511
1512 stlink_usb_init_buffer(handle, h->rx_ep, 2);
1513
1514 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1515 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_RUNCORE;
1516
1517 return stlink_cmd_allow_retry(handle, h->databuf, 2);
1518 }
1519
1520 /** */
1521 static int stlink_usb_halt(void *handle)
1522 {
1523 int res;
1524 struct stlink_usb_handle_s *h = handle;
1525
1526 assert(handle != NULL);
1527
1528 if (h->jtag_api == STLINK_JTAG_API_V2) {
1529 res = stlink_usb_write_debug_reg(handle, DCB_DHCSR, DBGKEY|C_HALT|C_DEBUGEN);
1530
1531 return res;
1532 }
1533
1534 stlink_usb_init_buffer(handle, h->rx_ep, 2);
1535
1536 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1537 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_FORCEDEBUG;
1538
1539 return stlink_cmd_allow_retry(handle, h->databuf, 2);
1540 }
1541
1542 /** */
1543 static int stlink_usb_step(void *handle)
1544 {
1545 struct stlink_usb_handle_s *h = handle;
1546
1547 assert(handle != NULL);
1548
1549 if (h->jtag_api == STLINK_JTAG_API_V2) {
1550 /* TODO: this emulates the v1 api, it should really use a similar auto mask isr
1551 * that the Cortex-M3 currently does. */
1552 stlink_usb_write_debug_reg(handle, DCB_DHCSR, DBGKEY|C_HALT|C_MASKINTS|C_DEBUGEN);
1553 stlink_usb_write_debug_reg(handle, DCB_DHCSR, DBGKEY|C_STEP|C_MASKINTS|C_DEBUGEN);
1554 return stlink_usb_write_debug_reg(handle, DCB_DHCSR, DBGKEY|C_HALT|C_DEBUGEN);
1555 }
1556
1557 stlink_usb_init_buffer(handle, h->rx_ep, 2);
1558
1559 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1560 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_STEPCORE;
1561
1562 return stlink_cmd_allow_retry(handle, h->databuf, 2);
1563 }
1564
1565 /** */
1566 static int stlink_usb_read_regs(void *handle)
1567 {
1568 int res;
1569 struct stlink_usb_handle_s *h = handle;
1570
1571 assert(handle != NULL);
1572
1573 stlink_usb_init_buffer(handle, h->rx_ep, 84);
1574
1575 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1576 if (h->jtag_api == STLINK_JTAG_API_V1)
1577 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_READALLREGS;
1578 else
1579 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_READALLREGS;
1580
1581 res = stlink_usb_xfer(handle, h->databuf, 84);
1582
1583 if (res != ERROR_OK)
1584 return res;
1585
1586 return ERROR_OK;
1587 }
1588
1589 /** */
1590 static int stlink_usb_read_reg(void *handle, int num, uint32_t *val)
1591 {
1592 int res;
1593 struct stlink_usb_handle_s *h = handle;
1594
1595 assert(handle != NULL);
1596
1597 stlink_usb_init_buffer(handle, h->rx_ep, h->jtag_api == STLINK_JTAG_API_V1 ? 4 : 8);
1598
1599 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1600 if (h->jtag_api == STLINK_JTAG_API_V1)
1601 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_READREG;
1602 else
1603 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_READREG;
1604 h->cmdbuf[h->cmdidx++] = num;
1605
1606 if (h->jtag_api == STLINK_JTAG_API_V1) {
1607 res = stlink_usb_xfer(handle, h->databuf, 4);
1608 if (res != ERROR_OK)
1609 return res;
1610 *val = le_to_h_u32(h->databuf);
1611 return ERROR_OK;
1612 } else {
1613 res = stlink_cmd_allow_retry(handle, h->databuf, 8);
1614 if (res != ERROR_OK)
1615 return res;
1616 *val = le_to_h_u32(h->databuf + 4);
1617 return ERROR_OK;
1618 }
1619 }
1620
1621 /** */
1622 static int stlink_usb_write_reg(void *handle, int num, uint32_t val)
1623 {
1624 struct stlink_usb_handle_s *h = handle;
1625
1626 assert(handle != NULL);
1627
1628 stlink_usb_init_buffer(handle, h->rx_ep, 2);
1629
1630 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1631 if (h->jtag_api == STLINK_JTAG_API_V1)
1632 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_WRITEREG;
1633 else
1634 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_WRITEREG;
1635 h->cmdbuf[h->cmdidx++] = num;
1636 h_u32_to_le(h->cmdbuf+h->cmdidx, val);
1637 h->cmdidx += 4;
1638
1639 return stlink_cmd_allow_retry(handle, h->databuf, 2);
1640 }
1641
1642 static int stlink_usb_get_rw_status(void *handle)
1643 {
1644 int res;
1645 struct stlink_usb_handle_s *h = handle;
1646
1647 assert(handle != NULL);
1648
1649 if (h->jtag_api == STLINK_JTAG_API_V1)
1650 return ERROR_OK;
1651
1652 stlink_usb_init_buffer(handle, h->rx_ep, 2);
1653
1654 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1655 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_GETLASTRWSTATUS;
1656
1657 res = stlink_usb_xfer(handle, h->databuf, 2);
1658
1659 if (res != ERROR_OK)
1660 return res;
1661
1662 return stlink_usb_error_check(h);
1663 }
1664
1665 /** */
1666 static int stlink_usb_read_mem8(void *handle, uint32_t addr, uint16_t len,
1667 uint8_t *buffer)
1668 {
1669 int res;
1670 uint16_t read_len = len;
1671 struct stlink_usb_handle_s *h = handle;
1672
1673 assert(handle != NULL);
1674
1675 /* max 8bit read/write is 64bytes */
1676 if (len > STLINK_MAX_RW8) {
1677 LOG_DEBUG("max buffer length exceeded");
1678 return ERROR_FAIL;
1679 }
1680
1681 stlink_usb_init_buffer(handle, h->rx_ep, read_len);
1682
1683 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1684 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_READMEM_8BIT;
1685 h_u32_to_le(h->cmdbuf+h->cmdidx, addr);
1686 h->cmdidx += 4;
1687 h_u16_to_le(h->cmdbuf+h->cmdidx, len);
1688 h->cmdidx += 2;
1689
1690 /* we need to fix read length for single bytes */
1691 if (read_len == 1)
1692 read_len++;
1693
1694 res = stlink_usb_xfer(handle, h->databuf, read_len);
1695
1696 if (res != ERROR_OK)
1697 return res;
1698
1699 memcpy(buffer, h->databuf, len);
1700
1701 return stlink_usb_get_rw_status(handle);
1702 }
1703
1704 /** */
1705 static int stlink_usb_write_mem8(void *handle, uint32_t addr, uint16_t len,
1706 const uint8_t *buffer)
1707 {
1708 int res;
1709 struct stlink_usb_handle_s *h = handle;
1710
1711 assert(handle != NULL);
1712
1713 /* max 8bit read/write is 64bytes */
1714 if (len > STLINK_MAX_RW8) {
1715 LOG_DEBUG("max buffer length exceeded");
1716 return ERROR_FAIL;
1717 }
1718
1719 stlink_usb_init_buffer(handle, h->tx_ep, len);
1720
1721 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1722 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_WRITEMEM_8BIT;
1723 h_u32_to_le(h->cmdbuf+h->cmdidx, addr);
1724 h->cmdidx += 4;
1725 h_u16_to_le(h->cmdbuf+h->cmdidx, len);
1726 h->cmdidx += 2;
1727
1728 res = stlink_usb_xfer(handle, buffer, len);
1729
1730 if (res != ERROR_OK)
1731 return res;
1732
1733 return stlink_usb_get_rw_status(handle);
1734 }
1735
1736 /** */
1737 static int stlink_usb_read_mem16(void *handle, uint32_t addr, uint16_t len,
1738 uint8_t *buffer)
1739 {
1740 int res;
1741 struct stlink_usb_handle_s *h = handle;
1742
1743 assert(handle != NULL);
1744
1745 /* only supported by stlink/v2 and for firmware >= 26 */
1746 if (h->jtag_api == STLINK_JTAG_API_V1 ||
1747 (h->jtag_api == STLINK_JTAG_API_V2 && h->version.jtag < 26))
1748 return ERROR_COMMAND_NOTFOUND;
1749
1750 /* data must be a multiple of 2 and half-word aligned */
1751 if (len % 2 || addr % 2) {
1752 LOG_DEBUG("Invalid data alignment");
1753 return ERROR_TARGET_UNALIGNED_ACCESS;
1754 }
1755
1756 stlink_usb_init_buffer(handle, h->rx_ep, len);
1757
1758 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1759 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_READMEM_16BIT;
1760 h_u32_to_le(h->cmdbuf+h->cmdidx, addr);
1761 h->cmdidx += 4;
1762 h_u16_to_le(h->cmdbuf+h->cmdidx, len);
1763 h->cmdidx += 2;
1764
1765 res = stlink_usb_xfer(handle, h->databuf, len);
1766
1767 if (res != ERROR_OK)
1768 return res;
1769
1770 memcpy(buffer, h->databuf, len);
1771
1772 return stlink_usb_get_rw_status(handle);
1773 }
1774
1775 /** */
1776 static int stlink_usb_write_mem16(void *handle, uint32_t addr, uint16_t len,
1777 const uint8_t *buffer)
1778 {
1779 int res;
1780 struct stlink_usb_handle_s *h = handle;
1781
1782 assert(handle != NULL);
1783
1784 /* only supported by stlink/v2 and for firmware >= 26 */
1785 if (h->jtag_api == STLINK_JTAG_API_V1 ||
1786 (h->jtag_api == STLINK_JTAG_API_V2 && h->version.jtag < 26))
1787 return ERROR_COMMAND_NOTFOUND;
1788
1789 /* data must be a multiple of 2 and half-word aligned */
1790 if (len % 2 || addr % 2) {
1791 LOG_DEBUG("Invalid data alignment");
1792 return ERROR_TARGET_UNALIGNED_ACCESS;
1793 }
1794
1795 stlink_usb_init_buffer(handle, h->tx_ep, len);
1796
1797 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1798 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_WRITEMEM_16BIT;
1799 h_u32_to_le(h->cmdbuf+h->cmdidx, addr);
1800 h->cmdidx += 4;
1801 h_u16_to_le(h->cmdbuf+h->cmdidx, len);
1802 h->cmdidx += 2;
1803
1804 res = stlink_usb_xfer(handle, buffer, len);
1805
1806 if (res != ERROR_OK)
1807 return res;
1808
1809 return stlink_usb_get_rw_status(handle);
1810 }
1811
1812 /** */
1813 static int stlink_usb_read_mem32(void *handle, uint32_t addr, uint16_t len,
1814 uint8_t *buffer)
1815 {
1816 int res;
1817 struct stlink_usb_handle_s *h = handle;
1818
1819 assert(handle != NULL);
1820
1821 /* data must be a multiple of 4 and word aligned */
1822 if (len % 4 || addr % 4) {
1823 LOG_DEBUG("Invalid data alignment");
1824 return ERROR_TARGET_UNALIGNED_ACCESS;
1825 }
1826
1827 stlink_usb_init_buffer(handle, h->rx_ep, len);
1828
1829 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1830 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_READMEM_32BIT;
1831 h_u32_to_le(h->cmdbuf+h->cmdidx, addr);
1832 h->cmdidx += 4;
1833 h_u16_to_le(h->cmdbuf+h->cmdidx, len);
1834 h->cmdidx += 2;
1835
1836 res = stlink_usb_xfer(handle, h->databuf, len);
1837
1838 if (res != ERROR_OK)
1839 return res;
1840
1841 memcpy(buffer, h->databuf, len);
1842
1843 return stlink_usb_get_rw_status(handle);
1844 }
1845
1846 /** */
1847 static int stlink_usb_write_mem32(void *handle, uint32_t addr, uint16_t len,
1848 const uint8_t *buffer)
1849 {
1850 int res;
1851 struct stlink_usb_handle_s *h = handle;
1852
1853 assert(handle != NULL);
1854
1855 /* data must be a multiple of 4 and word aligned */
1856 if (len % 4 || addr % 4) {
1857 LOG_DEBUG("Invalid data alignment");
1858 return ERROR_TARGET_UNALIGNED_ACCESS;
1859 }
1860
1861 stlink_usb_init_buffer(handle, h->tx_ep, len);
1862
1863 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1864 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_WRITEMEM_32BIT;
1865 h_u32_to_le(h->cmdbuf+h->cmdidx, addr);
1866 h->cmdidx += 4;
1867 h_u16_to_le(h->cmdbuf+h->cmdidx, len);
1868 h->cmdidx += 2;
1869
1870 res = stlink_usb_xfer(handle, buffer, len);
1871
1872 if (res != ERROR_OK)
1873 return res;
1874
1875 return stlink_usb_get_rw_status(handle);
1876 }
1877
1878 static uint32_t stlink_max_block_size(uint32_t tar_autoincr_block, uint32_t address)
1879 {
1880 uint32_t max_tar_block = (tar_autoincr_block - ((tar_autoincr_block - 1) & address));
1881 if (max_tar_block == 0)
1882 max_tar_block = 4;
1883 return max_tar_block;
1884 }
1885
1886 static int stlink_usb_read_mem(void *handle, uint32_t addr, uint32_t size,
1887 uint32_t count, uint8_t *buffer)
1888 {
1889 int retval = ERROR_OK;
1890 uint32_t bytes_remaining;
1891 int retries = 0;
1892 struct stlink_usb_handle_s *h = handle;
1893
1894 /* calculate byte count */
1895 count *= size;
1896
1897 /* switch to 8 bit if stlink does not support 16 bit memory read */
1898 if (size == 2 && (h->jtag_api == STLINK_JTAG_API_V1 ||
1899 (h->jtag_api == STLINK_JTAG_API_V2 && h->version.jtag < 26)))
1900 size = 1;
1901
1902 while (count) {
1903
1904 bytes_remaining = (size != 1) ? \
1905 stlink_max_block_size(h->max_mem_packet, addr) : STLINK_MAX_RW8;
1906
1907 if (count < bytes_remaining)
1908 bytes_remaining = count;
1909
1910 if (h->transport == HL_TRANSPORT_SWIM) {
1911 retval = stlink_swim_readbytes(handle, addr, bytes_remaining, buffer);
1912 if (retval != ERROR_OK)
1913 return retval;
1914 } else
1915 /*
1916 * all stlink support 8/32bit memory read/writes and only from
1917 * stlink V2J26 there is support for 16 bit memory read/write.
1918 * Honour 32 bit and, if possible, 16 bit too. Otherwise, handle
1919 * as 8bit access.
1920 */
1921 if (size != 1) {
1922
1923 /* When in jtag mode the stlink uses the auto-increment functionality.
1924 * However it expects us to pass the data correctly, this includes
1925 * alignment and any page boundaries. We already do this as part of the
1926 * adi_v5 implementation, but the stlink is a hla adapter and so this
1927 * needs implementing manually.
1928 * currently this only affects jtag mode, according to ST they do single
1929 * access in SWD mode - but this may change and so we do it for both modes */
1930
1931 /* we first need to check for any unaligned bytes */
1932 if (addr & (size - 1)) {
1933
1934 uint32_t head_bytes = size - (addr & (size - 1));
1935 retval = stlink_usb_read_mem8(handle, addr, head_bytes, buffer);
1936 if (retval == ERROR_WAIT && retries < MAX_WAIT_RETRIES) {
1937 usleep((1<<retries++) * 1000);
1938 continue;
1939 }
1940 if (retval != ERROR_OK)
1941 return retval;
1942 buffer += head_bytes;
1943 addr += head_bytes;
1944 count -= head_bytes;
1945 bytes_remaining -= head_bytes;
1946 }
1947
1948 if (bytes_remaining & (size - 1))
1949 retval = stlink_usb_read_mem(handle, addr, 1, bytes_remaining, buffer);
1950 else if (size == 2)
1951 retval = stlink_usb_read_mem16(handle, addr, bytes_remaining, buffer);
1952 else
1953 retval = stlink_usb_read_mem32(handle, addr, bytes_remaining, buffer);
1954 } else
1955 retval = stlink_usb_read_mem8(handle, addr, bytes_remaining, buffer);
1956
1957 if (retval == ERROR_WAIT && retries < MAX_WAIT_RETRIES) {
1958 usleep((1<<retries++) * 1000);
1959 continue;
1960 }
1961 if (retval != ERROR_OK)
1962 return retval;
1963
1964 buffer += bytes_remaining;
1965 addr += bytes_remaining;
1966 count -= bytes_remaining;
1967 }
1968
1969 return retval;
1970 }
1971
1972 static int stlink_usb_write_mem(void *handle, uint32_t addr, uint32_t size,
1973 uint32_t count, const uint8_t *buffer)
1974 {
1975 int retval = ERROR_OK;
1976 uint32_t bytes_remaining;
1977 int retries = 0;
1978 struct stlink_usb_handle_s *h = handle;
1979
1980 /* calculate byte count */
1981 count *= size;
1982
1983 /* switch to 8 bit if stlink does not support 16 bit memory read */
1984 if (size == 2 && (h->jtag_api == STLINK_JTAG_API_V1 ||
1985 (h->jtag_api == STLINK_JTAG_API_V2 && h->version.jtag < 26)))
1986 size = 1;
1987
1988 while (count) {
1989
1990 bytes_remaining = (size != 1) ? \
1991 stlink_max_block_size(h->max_mem_packet, addr) : STLINK_MAX_RW8;
1992
1993 if (count < bytes_remaining)
1994 bytes_remaining = count;
1995
1996 if (h->transport == HL_TRANSPORT_SWIM) {
1997 retval = stlink_swim_writebytes(handle, addr, bytes_remaining, buffer);
1998 if (retval != ERROR_OK)
1999 return retval;
2000 } else
2001 /*
2002 * all stlink support 8/32bit memory read/writes and only from
2003 * stlink V2J26 there is support for 16 bit memory read/write.
2004 * Honour 32 bit and, if possible, 16 bit too. Otherwise, handle
2005 * as 8bit access.
2006 */
2007 if (size != 1) {
2008
2009 /* When in jtag mode the stlink uses the auto-increment functionality.
2010 * However it expects us to pass the data correctly, this includes
2011 * alignment and any page boundaries. We already do this as part of the
2012 * adi_v5 implementation, but the stlink is a hla adapter and so this
2013 * needs implementing manually.
2014 * currently this only affects jtag mode, according to ST they do single
2015 * access in SWD mode - but this may change and so we do it for both modes */
2016
2017 /* we first need to check for any unaligned bytes */
2018 if (addr & (size - 1)) {
2019
2020 uint32_t head_bytes = size - (addr & (size - 1));
2021 retval = stlink_usb_write_mem8(handle, addr, head_bytes, buffer);
2022 if (retval == ERROR_WAIT && retries < MAX_WAIT_RETRIES) {
2023 usleep((1<<retries++) * 1000);
2024 continue;
2025 }
2026 if (retval != ERROR_OK)
2027 return retval;
2028 buffer += head_bytes;
2029 addr += head_bytes;
2030 count -= head_bytes;
2031 bytes_remaining -= head_bytes;
2032 }
2033
2034 if (bytes_remaining & (size - 1))
2035 retval = stlink_usb_write_mem(handle, addr, 1, bytes_remaining, buffer);
2036 else if (size == 2)
2037 retval = stlink_usb_write_mem16(handle, addr, bytes_remaining, buffer);
2038 else
2039 retval = stlink_usb_write_mem32(handle, addr, bytes_remaining, buffer);
2040
2041 } else
2042 retval = stlink_usb_write_mem8(handle, addr, bytes_remaining, buffer);
2043 if (retval == ERROR_WAIT && retries < MAX_WAIT_RETRIES) {
2044 usleep((1<<retries++) * 1000);
2045 continue;
2046 }
2047 if (retval != ERROR_OK)
2048 return retval;
2049
2050 buffer += bytes_remaining;
2051 addr += bytes_remaining;
2052 count -= bytes_remaining;
2053 }
2054
2055 return retval;
2056 }
2057
2058 /** */
2059 static int stlink_usb_override_target(const char *targetname)
2060 {
2061 return !strcmp(targetname, "cortex_m");
2062 }
2063
2064 static int stlink_speed_swim(void *handle, int khz, bool query)
2065 {
2066 /*
2067 we dont care what the khz rate is
2068 we only have low and high speed...
2069 before changing speed the SWIM_CSR HS bit
2070 must be updated
2071 */
2072 if (khz == 0)
2073 stlink_swim_speed(handle, 0);
2074 else
2075 stlink_swim_speed(handle, 1);
2076 return khz;
2077 }
2078
2079 static int stlink_match_speed_map(const struct speed_map *map, unsigned int map_size, int khz, bool query)
2080 {
2081 unsigned int i;
2082 int speed_index = -1;
2083 int speed_diff = INT_MAX;
2084 bool match = true;
2085
2086 for (i = 0; i < map_size; i++) {
2087 if (khz == map[i].speed) {
2088 speed_index = i;
2089 break;
2090 } else {
2091 int current_diff = khz - map[i].speed;
2092 /* get abs value for comparison */
2093 current_diff = (current_diff > 0) ? current_diff : -current_diff;
2094 if ((current_diff < speed_diff) && khz >= map[i].speed) {
2095 speed_diff = current_diff;
2096 speed_index = i;
2097 }
2098 }
2099 }
2100
2101 if (speed_index == -1) {
2102 /* this will only be here if we cannot match the slow speed.
2103 * use the slowest speed we support.*/
2104 speed_index = map_size - 1;
2105 match = false;
2106 } else if (i == map_size)
2107 match = false;
2108
2109 if (!match && query) {
2110 LOG_INFO("Unable to match requested speed %d kHz, using %d kHz", \
2111 khz, map[speed_index].speed);
2112 }
2113
2114 return speed_index;
2115 }
2116
2117 static int stlink_speed_swd(void *handle, int khz, bool query)
2118 {
2119 int speed_index;
2120 struct stlink_usb_handle_s *h = handle;
2121
2122 /* old firmware cannot change it */
2123 if (!(h->version.flags & STLINK_F_HAS_SWD_SET_FREQ))
2124 return khz;
2125
2126 speed_index = stlink_match_speed_map(stlink_khz_to_speed_map_swd,
2127 ARRAY_SIZE(stlink_khz_to_speed_map_swd), khz, query);
2128
2129 if (!query) {
2130 int result = stlink_usb_set_swdclk(h, stlink_khz_to_speed_map_swd[speed_index].speed_divisor);
2131 if (result != ERROR_OK) {
2132 LOG_ERROR("Unable to set adapter speed");
2133 return khz;
2134 }
2135 }
2136
2137 return stlink_khz_to_speed_map_swd[speed_index].speed;
2138 }
2139
2140 static int stlink_speed_jtag(void *handle, int khz, bool query)
2141 {
2142 int speed_index;
2143 struct stlink_usb_handle_s *h = handle;
2144
2145 /* only supported by stlink/v2 and for firmware >= 24 */
2146 if (h->version.stlink == 1 || h->version.jtag < 24)
2147 return khz;
2148
2149 speed_index = stlink_match_speed_map(stlink_khz_to_speed_map_jtag,
2150 ARRAY_SIZE(stlink_khz_to_speed_map_jtag), khz, query);
2151
2152 if (!query) {
2153 int result = stlink_usb_set_jtagclk(h, stlink_khz_to_speed_map_jtag[speed_index].speed_divisor);
2154 if (result != ERROR_OK) {
2155 LOG_ERROR("Unable to set adapter speed");
2156 return khz;
2157 }
2158 }
2159
2160 return stlink_khz_to_speed_map_jtag[speed_index].speed;
2161 }
2162
2163 void stlink_dump_speed_map(const struct speed_map *map, unsigned int map_size)
2164 {
2165 unsigned int i;
2166
2167 LOG_DEBUG("Supported clock speeds are:");
2168 for (i = 0; i < map_size; i++)
2169 LOG_DEBUG("%d kHz", map[i].speed);
2170 }
2171
2172 static int stlink_speed(void *handle, int khz, bool query)
2173 {
2174 struct stlink_usb_handle_s *h = handle;
2175
2176 if (!handle)
2177 return khz;
2178
2179 if (h->transport == HL_TRANSPORT_SWIM)
2180 return stlink_speed_swim(handle, khz, query);
2181 else if (h->transport == HL_TRANSPORT_SWD)
2182 return stlink_speed_swd(handle, khz, query);
2183 else if (h->transport == HL_TRANSPORT_JTAG)
2184 return stlink_speed_jtag(handle, khz, query);
2185
2186 return khz;
2187 }
2188
2189 /** */
2190 static int stlink_usb_close(void *handle)
2191 {
2192 int res;
2193 uint8_t mode;
2194 enum stlink_mode emode;
2195 struct stlink_usb_handle_s *h = handle;
2196
2197 if (h && h->fd)
2198 res = stlink_usb_current_mode(handle, &mode);
2199 else
2200 res = ERROR_FAIL;
2201 /* do not exit if return code != ERROR_OK,
2202 it prevents us from closing jtag_libusb */
2203
2204 if (res == ERROR_OK) {
2205 /* try to exit current mode */
2206 switch (mode) {
2207 case STLINK_DEV_DFU_MODE:
2208 emode = STLINK_MODE_DFU;
2209 break;
2210 case STLINK_DEV_DEBUG_MODE:
2211 emode = STLINK_MODE_DEBUG_SWD;
2212 break;
2213 case STLINK_DEV_SWIM_MODE:
2214 emode = STLINK_MODE_DEBUG_SWIM;
2215 break;
2216 case STLINK_DEV_BOOTLOADER_MODE:
2217 case STLINK_DEV_MASS_MODE:
2218 default:
2219 emode = STLINK_MODE_UNKNOWN;
2220 break;
2221 }
2222
2223 if (emode != STLINK_MODE_UNKNOWN)
2224 stlink_usb_mode_leave(handle, emode);
2225 /* do not check return code, it prevent
2226 us from closing jtag_libusb */
2227 }
2228
2229 if (h && h->fd)
2230 jtag_libusb_close(h->fd);
2231
2232 free(h);
2233
2234 return ERROR_OK;
2235 }
2236
2237 /** */
2238 static int stlink_usb_open(struct hl_interface_param_s *param, void **fd)
2239 {
2240 int err, retry_count = 1;
2241 struct stlink_usb_handle_s *h;
2242 enum stlink_jtag_api_version api;
2243
2244 LOG_DEBUG("stlink_usb_open");
2245
2246 h = calloc(1, sizeof(struct stlink_usb_handle_s));
2247
2248 if (h == 0) {
2249 LOG_DEBUG("malloc failed");
2250 return ERROR_FAIL;
2251 }
2252
2253 h->transport = param->transport;
2254
2255 for (unsigned i = 0; param->vid[i]; i++) {
2256 LOG_DEBUG("transport: %d vid: 0x%04x pid: 0x%04x serial: %s",
2257 param->transport, param->vid[i], param->pid[i],
2258 param->serial ? param->serial : "");
2259 }
2260
2261 /*
2262 On certain host USB configurations(e.g. MacBook Air)
2263 STLINKv2 dongle seems to have its FW in a funky state if,
2264 after plugging it in, you try to use openocd with it more
2265 then once (by launching and closing openocd). In cases like
2266 that initial attempt to read the FW info via
2267 stlink_usb_version will fail and the device has to be reset
2268 in order to become operational.
2269 */
2270 do {
2271 if (jtag_libusb_open(param->vid, param->pid, param->serial, &h->fd) != ERROR_OK) {
2272 LOG_ERROR("open failed");
2273 goto error_open;
2274 }
2275
2276 jtag_libusb_set_configuration(h->fd, 0);
2277
2278 if (jtag_libusb_claim_interface(h->fd, 0) != ERROR_OK) {
2279 LOG_DEBUG("claim interface failed");
2280 goto error_open;
2281 }
2282
2283 /* RX EP is common for all versions */
2284 h->rx_ep = STLINK_RX_EP;
2285
2286 uint16_t pid;
2287 if (jtag_libusb_get_pid(jtag_libusb_get_device(h->fd), &pid) != ERROR_OK) {
2288 LOG_DEBUG("libusb_get_pid failed");
2289 goto error_open;
2290 }
2291
2292 /* wrap version for first read */
2293 switch (pid) {
2294 case STLINK_V1_PID:
2295 h->version.stlink = 1;
2296 h->tx_ep = STLINK_TX_EP;
2297 break;
2298 case STLINK_V2_1_PID:
2299 case STLINK_V2_1_NO_MSD_PID:
2300 h->version.stlink = 2;
2301 h->tx_ep = STLINK_V2_1_TX_EP;
2302 h->trace_ep = STLINK_V2_1_TRACE_EP;
2303 break;
2304 default:
2305 /* fall through - we assume V2 to be the default version*/
2306 case STLINK_V2_PID:
2307 h->version.stlink = 2;
2308 h->tx_ep = STLINK_TX_EP;
2309 h->trace_ep = STLINK_TRACE_EP;
2310 break;
2311 }
2312
2313 /* get the device version */
2314 err = stlink_usb_version(h);
2315
2316 if (err == ERROR_OK) {
2317 break;
2318 } else if (h->version.stlink == 1 ||
2319 retry_count == 0) {
2320 LOG_ERROR("read version failed");
2321 goto error_open;
2322 } else {
2323 err = jtag_libusb_release_interface(h->fd, 0);
2324 if (err != ERROR_OK) {
2325 LOG_ERROR("release interface failed");
2326 goto error_open;
2327 }
2328
2329 err = jtag_libusb_reset_device(h->fd);
2330 if (err != ERROR_OK) {
2331 LOG_ERROR("reset device failed");
2332 goto error_open;
2333 }
2334
2335 jtag_libusb_close(h->fd);
2336 /*
2337 Give the device one second to settle down and
2338 reenumerate.
2339 */
2340 usleep(1 * 1000 * 1000);
2341 retry_count--;
2342 }
2343 } while (1);
2344
2345 /* check if mode is supported */
2346 err = ERROR_OK;
2347
2348 switch (h->transport) {
2349 case HL_TRANSPORT_SWD:
2350 if (h->version.jtag_api_max == STLINK_JTAG_API_V1)
2351 err = ERROR_FAIL;
2352 /* fall-through */
2353 case HL_TRANSPORT_JTAG:
2354 if (h->version.jtag == 0)
2355 err = ERROR_FAIL;
2356 break;
2357 case HL_TRANSPORT_SWIM:
2358 if (h->version.swim == 0)
2359 err = ERROR_FAIL;
2360 break;
2361 default:
2362 err = ERROR_FAIL;
2363 break;
2364 }
2365
2366 if (err != ERROR_OK) {
2367 LOG_ERROR("mode (transport) not supported by device");
2368 goto error_open;
2369 }
2370
2371 api = h->version.jtag_api_max;
2372
2373 LOG_INFO("using stlink api v%d", api);
2374
2375 /* set the used jtag api, this will default to the newest supported version */
2376 h->jtag_api = api;
2377
2378 /* initialize the debug hardware */
2379 err = stlink_usb_init_mode(h, param->connect_under_reset);
2380
2381 if (err != ERROR_OK) {
2382 LOG_ERROR("init mode failed (unable to connect to the target)");
2383 goto error_open;
2384 }
2385
2386 if (h->transport == HL_TRANSPORT_SWIM) {
2387 err = stlink_swim_enter(h);
2388 if (err != ERROR_OK) {
2389 LOG_ERROR("stlink_swim_enter_failed (unable to connect to the target)");
2390 goto error_open;
2391 }
2392 *fd = h;
2393 h->max_mem_packet = STLINK_DATA_SIZE;
2394 return ERROR_OK;
2395 }
2396
2397 if (h->transport == HL_TRANSPORT_JTAG) {
2398 /* jtag clock speed only supported by stlink/v2 and for firmware >= 24 */
2399 if (h->version.stlink >= 2 && h->version.jtag >= 24) {
2400 stlink_dump_speed_map(stlink_khz_to_speed_map_jtag, ARRAY_SIZE(stlink_khz_to_speed_map_jtag));
2401 stlink_speed(h, param->initial_interface_speed, false);
2402 }
2403 } else if (h->transport == HL_TRANSPORT_SWD) {
2404 if (h->version.flags & STLINK_F_HAS_SWD_SET_FREQ) {
2405 stlink_dump_speed_map(stlink_khz_to_speed_map_swd, ARRAY_SIZE(stlink_khz_to_speed_map_swd));
2406 stlink_speed(h, param->initial_interface_speed, false);
2407 }
2408 }
2409
2410 /* get cpuid, so we can determine the max page size
2411 * start with a safe default */
2412 h->max_mem_packet = (1 << 10);
2413
2414 uint8_t buffer[4];
2415 err = stlink_usb_read_mem32(h, CPUID, 4, buffer);
2416 if (err == ERROR_OK) {
2417 uint32_t cpuid = le_to_h_u32(buffer);
2418 int i = (cpuid >> 4) & 0xf;
2419 if (i == 4 || i == 3) {
2420 /* Cortex-M3/M4 has 4096 bytes autoincrement range */
2421 h->max_mem_packet = (1 << 12);
2422 }
2423 }
2424
2425 LOG_DEBUG("Using TAR autoincrement: %" PRIu32, h->max_mem_packet);
2426
2427 *fd = h;
2428
2429 return ERROR_OK;
2430
2431 error_open:
2432 stlink_usb_close(h);
2433
2434 return ERROR_FAIL;
2435 }
2436
2437 int stlink_config_trace(void *handle, bool enabled, enum tpiu_pin_protocol pin_protocol,
2438 uint32_t port_size, unsigned int *trace_freq)
2439 {
2440 struct stlink_usb_handle_s *h = handle;
2441
2442 if (enabled && (!(h->version.flags & STLINK_F_HAS_TRACE) ||
2443 pin_protocol != TPIU_PIN_PROTOCOL_ASYNC_UART)) {
2444 LOG_ERROR("The attached ST-LINK version doesn't support this trace mode");
2445 return ERROR_FAIL;
2446 }
2447
2448 if (!enabled) {
2449 stlink_usb_trace_disable(h);
2450 return ERROR_OK;
2451 }
2452
2453 if (*trace_freq > STLINK_TRACE_MAX_HZ) {
2454 LOG_ERROR("ST-LINK doesn't support SWO frequency higher than %u",
2455 STLINK_TRACE_MAX_HZ);
2456 return ERROR_FAIL;
2457 }
2458
2459 stlink_usb_trace_disable(h);
2460
2461 if (!*trace_freq)
2462 *trace_freq = STLINK_TRACE_MAX_HZ;
2463 h->trace.source_hz = *trace_freq;
2464
2465 return stlink_usb_trace_enable(h);
2466 }
2467
2468 /** */
2469 struct hl_layout_api_s stlink_usb_layout_api = {
2470 /** */
2471 .open = stlink_usb_open,
2472 /** */
2473 .close = stlink_usb_close,
2474 /** */
2475 .idcode = stlink_usb_idcode,
2476 /** */
2477 .state = stlink_usb_state,
2478 /** */
2479 .reset = stlink_usb_reset,
2480 /** */
2481 .assert_srst = stlink_usb_assert_srst,
2482 /** */
2483 .run = stlink_usb_run,
2484 /** */
2485 .halt = stlink_usb_halt,
2486 /** */
2487 .step = stlink_usb_step,
2488 /** */
2489 .read_regs = stlink_usb_read_regs,
2490 /** */
2491 .read_reg = stlink_usb_read_reg,
2492 /** */
2493 .write_reg = stlink_usb_write_reg,
2494 /** */
2495 .read_mem = stlink_usb_read_mem,
2496 /** */
2497 .write_mem = stlink_usb_write_mem,
2498 /** */
2499 .write_debug_reg = stlink_usb_write_debug_reg,
2500 /** */
2501 .override_target = stlink_usb_override_target,
2502 /** */
2503 .speed = stlink_speed,
2504 /** */
2505 .config_trace = stlink_config_trace,
2506 /** */
2507 .poll_trace = stlink_usb_trace_read,
2508 };

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)