stlink: fix incorrectly returned error on v2j28
[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 <helper/bits.h>
35 #include <jtag/interface.h>
36 #include <jtag/hla/hla_layout.h>
37 #include <jtag/hla/hla_transport.h>
38 #include <jtag/hla/hla_interface.h>
39 #include <jtag/swim.h>
40 #include <target/target.h>
41 #include <transport/transport.h>
42
43 #include <target/cortex_m.h>
44
45 #include "libusb_helper.h"
46
47 #ifdef HAVE_LIBUSB1
48 #define USE_LIBUSB_ASYNCIO
49 #endif
50
51 #define STLINK_SERIAL_LEN 24
52
53 #define ENDPOINT_IN 0x80
54 #define ENDPOINT_OUT 0x00
55
56 #define STLINK_WRITE_TIMEOUT 1000
57 #define STLINK_READ_TIMEOUT 1000
58
59 #define STLINK_RX_EP (1|ENDPOINT_IN)
60 #define STLINK_TX_EP (2|ENDPOINT_OUT)
61 #define STLINK_TRACE_EP (3|ENDPOINT_IN)
62
63 #define STLINK_V2_1_TX_EP (1|ENDPOINT_OUT)
64 #define STLINK_V2_1_TRACE_EP (2|ENDPOINT_IN)
65
66 #define STLINK_SG_SIZE (31)
67 #define STLINK_DATA_SIZE (4096)
68 #define STLINK_CMD_SIZE_V2 (16)
69 #define STLINK_CMD_SIZE_V1 (10)
70
71 #define STLINK_V1_PID (0x3744)
72 #define STLINK_V2_PID (0x3748)
73 #define STLINK_V2_1_PID (0x374B)
74 #define STLINK_V2_1_NO_MSD_PID (0x3752)
75 #define STLINK_V3_USBLOADER_PID (0x374D)
76 #define STLINK_V3E_PID (0x374E)
77 #define STLINK_V3S_PID (0x374F)
78 #define STLINK_V3_2VCP_PID (0x3753)
79
80 /*
81 * ST-Link/V1, ST-Link/V2 and ST-Link/V2.1 are full-speed USB devices and
82 * this limits the bulk packet size and the 8bit read/writes to max 64 bytes.
83 * STLINK-V3 is a high speed USB 2.0 and the limit is 512 bytes from FW V3J6.
84 */
85 #define STLINK_MAX_RW8 (64)
86 #define STLINKV3_MAX_RW8 (512)
87
88 /* "WAIT" responses will be retried (with exponential backoff) at
89 * most this many times before failing to caller.
90 */
91 #define MAX_WAIT_RETRIES 8
92
93 enum stlink_jtag_api_version {
94 STLINK_JTAG_API_V1 = 1,
95 STLINK_JTAG_API_V2,
96 STLINK_JTAG_API_V3,
97 };
98
99 enum stlink_mode {
100 STLINK_MODE_UNKNOWN = 0,
101 STLINK_MODE_DFU,
102 STLINK_MODE_MASS,
103 STLINK_MODE_DEBUG_JTAG,
104 STLINK_MODE_DEBUG_SWD,
105 STLINK_MODE_DEBUG_SWIM
106 };
107
108 /** */
109 struct stlink_usb_version {
110 /** */
111 int stlink;
112 /** */
113 int jtag;
114 /** */
115 int swim;
116 /** jtag api version supported */
117 enum stlink_jtag_api_version jtag_api;
118 /** one bit for each feature supported. See macros STLINK_F_* */
119 uint32_t flags;
120 };
121
122 /** */
123 struct stlink_usb_handle_s {
124 /** */
125 struct libusb_device_handle *fd;
126 /** */
127 struct libusb_transfer *trans;
128 /** */
129 uint8_t rx_ep;
130 /** */
131 uint8_t tx_ep;
132 /** */
133 uint8_t trace_ep;
134 /** */
135 uint8_t cmdbuf[STLINK_SG_SIZE];
136 /** */
137 uint8_t cmdidx;
138 /** */
139 uint8_t direction;
140 /** */
141 uint8_t databuf[STLINK_DATA_SIZE];
142 /** */
143 uint32_t max_mem_packet;
144 /** */
145 enum stlink_mode st_mode;
146 /** */
147 struct stlink_usb_version version;
148 /** */
149 uint16_t vid;
150 /** */
151 uint16_t pid;
152 /** */
153 struct {
154 /** whether SWO tracing is enabled or not */
155 bool enabled;
156 /** trace module source clock */
157 uint32_t source_hz;
158 } trace;
159 /** reconnect is needed next time we try to query the
160 * status */
161 bool reconnect_pending;
162 };
163
164 #define STLINK_SWIM_ERR_OK 0x00
165 #define STLINK_SWIM_BUSY 0x01
166 #define STLINK_DEBUG_ERR_OK 0x80
167 #define STLINK_DEBUG_ERR_FAULT 0x81
168 #define STLINK_SWD_AP_WAIT 0x10
169 #define STLINK_SWD_AP_FAULT 0x11
170 #define STLINK_SWD_AP_ERROR 0x12
171 #define STLINK_SWD_AP_PARITY_ERROR 0x13
172 #define STLINK_JTAG_GET_IDCODE_ERROR 0x09
173 #define STLINK_JTAG_WRITE_ERROR 0x0c
174 #define STLINK_JTAG_WRITE_VERIF_ERROR 0x0d
175 #define STLINK_SWD_DP_WAIT 0x14
176 #define STLINK_SWD_DP_FAULT 0x15
177 #define STLINK_SWD_DP_ERROR 0x16
178 #define STLINK_SWD_DP_PARITY_ERROR 0x17
179
180 #define STLINK_SWD_AP_WDATA_ERROR 0x18
181 #define STLINK_SWD_AP_STICKY_ERROR 0x19
182 #define STLINK_SWD_AP_STICKYORUN_ERROR 0x1a
183
184 #define STLINK_BAD_AP_ERROR 0x1d
185
186 #define STLINK_CORE_RUNNING 0x80
187 #define STLINK_CORE_HALTED 0x81
188 #define STLINK_CORE_STAT_UNKNOWN -1
189
190 #define STLINK_GET_VERSION 0xF1
191 #define STLINK_DEBUG_COMMAND 0xF2
192 #define STLINK_DFU_COMMAND 0xF3
193 #define STLINK_SWIM_COMMAND 0xF4
194 #define STLINK_GET_CURRENT_MODE 0xF5
195 #define STLINK_GET_TARGET_VOLTAGE 0xF7
196
197 #define STLINK_DEV_DFU_MODE 0x00
198 #define STLINK_DEV_MASS_MODE 0x01
199 #define STLINK_DEV_DEBUG_MODE 0x02
200 #define STLINK_DEV_SWIM_MODE 0x03
201 #define STLINK_DEV_BOOTLOADER_MODE 0x04
202 #define STLINK_DEV_UNKNOWN_MODE -1
203
204 #define STLINK_DFU_EXIT 0x07
205
206 /*
207 STLINK_SWIM_ENTER_SEQ
208 1.3ms low then 750Hz then 1.5kHz
209
210 STLINK_SWIM_GEN_RST
211 STM8 DM pulls reset pin low 50us
212
213 STLINK_SWIM_SPEED
214 uint8_t (0=low|1=high)
215
216 STLINK_SWIM_WRITEMEM
217 uint16_t length
218 uint32_t address
219
220 STLINK_SWIM_RESET
221 send syncronization seq (16us low, response 64 clocks low)
222 */
223 #define STLINK_SWIM_ENTER 0x00
224 #define STLINK_SWIM_EXIT 0x01
225 #define STLINK_SWIM_READ_CAP 0x02
226 #define STLINK_SWIM_SPEED 0x03
227 #define STLINK_SWIM_ENTER_SEQ 0x04
228 #define STLINK_SWIM_GEN_RST 0x05
229 #define STLINK_SWIM_RESET 0x06
230 #define STLINK_SWIM_ASSERT_RESET 0x07
231 #define STLINK_SWIM_DEASSERT_RESET 0x08
232 #define STLINK_SWIM_READSTATUS 0x09
233 #define STLINK_SWIM_WRITEMEM 0x0a
234 #define STLINK_SWIM_READMEM 0x0b
235 #define STLINK_SWIM_READBUF 0x0c
236
237 #define STLINK_DEBUG_GETSTATUS 0x01
238 #define STLINK_DEBUG_FORCEDEBUG 0x02
239 #define STLINK_DEBUG_APIV1_RESETSYS 0x03
240 #define STLINK_DEBUG_APIV1_READALLREGS 0x04
241 #define STLINK_DEBUG_APIV1_READREG 0x05
242 #define STLINK_DEBUG_APIV1_WRITEREG 0x06
243 #define STLINK_DEBUG_READMEM_32BIT 0x07
244 #define STLINK_DEBUG_WRITEMEM_32BIT 0x08
245 #define STLINK_DEBUG_RUNCORE 0x09
246 #define STLINK_DEBUG_STEPCORE 0x0a
247 #define STLINK_DEBUG_APIV1_SETFP 0x0b
248 #define STLINK_DEBUG_READMEM_8BIT 0x0c
249 #define STLINK_DEBUG_WRITEMEM_8BIT 0x0d
250 #define STLINK_DEBUG_APIV1_CLEARFP 0x0e
251 #define STLINK_DEBUG_APIV1_WRITEDEBUGREG 0x0f
252 #define STLINK_DEBUG_APIV1_SETWATCHPOINT 0x10
253
254 #define STLINK_DEBUG_ENTER_JTAG_RESET 0x00
255 #define STLINK_DEBUG_ENTER_SWD_NO_RESET 0xa3
256 #define STLINK_DEBUG_ENTER_JTAG_NO_RESET 0xa4
257
258 #define STLINK_DEBUG_APIV1_ENTER 0x20
259 #define STLINK_DEBUG_EXIT 0x21
260 #define STLINK_DEBUG_READCOREID 0x22
261
262 #define STLINK_DEBUG_APIV2_ENTER 0x30
263 #define STLINK_DEBUG_APIV2_READ_IDCODES 0x31
264 #define STLINK_DEBUG_APIV2_RESETSYS 0x32
265 #define STLINK_DEBUG_APIV2_READREG 0x33
266 #define STLINK_DEBUG_APIV2_WRITEREG 0x34
267 #define STLINK_DEBUG_APIV2_WRITEDEBUGREG 0x35
268 #define STLINK_DEBUG_APIV2_READDEBUGREG 0x36
269
270 #define STLINK_DEBUG_APIV2_READALLREGS 0x3A
271 #define STLINK_DEBUG_APIV2_GETLASTRWSTATUS 0x3B
272 #define STLINK_DEBUG_APIV2_DRIVE_NRST 0x3C
273
274 #define STLINK_DEBUG_APIV2_GETLASTRWSTATUS2 0x3E
275
276 #define STLINK_DEBUG_APIV2_START_TRACE_RX 0x40
277 #define STLINK_DEBUG_APIV2_STOP_TRACE_RX 0x41
278 #define STLINK_DEBUG_APIV2_GET_TRACE_NB 0x42
279 #define STLINK_DEBUG_APIV2_SWD_SET_FREQ 0x43
280 #define STLINK_DEBUG_APIV2_JTAG_SET_FREQ 0x44
281 #define STLINK_DEBUG_APIV2_READ_DAP_REG 0x45
282 #define STLINK_DEBUG_APIV2_WRITE_DAP_REG 0x46
283 #define STLINK_DEBUG_APIV2_READMEM_16BIT 0x47
284 #define STLINK_DEBUG_APIV2_WRITEMEM_16BIT 0x48
285
286 #define STLINK_DEBUG_APIV2_INIT_AP 0x4B
287 #define STLINK_DEBUG_APIV2_CLOSE_AP_DBG 0x4C
288
289 #define STLINK_APIV3_SET_COM_FREQ 0x61
290 #define STLINK_APIV3_GET_COM_FREQ 0x62
291
292 #define STLINK_APIV3_GET_VERSION_EX 0xFB
293
294 #define STLINK_DEBUG_APIV2_DRIVE_NRST_LOW 0x00
295 #define STLINK_DEBUG_APIV2_DRIVE_NRST_HIGH 0x01
296 #define STLINK_DEBUG_APIV2_DRIVE_NRST_PULSE 0x02
297
298 #define STLINK_DEBUG_PORT_ACCESS 0xffff
299
300 #define STLINK_TRACE_SIZE 4096
301 #define STLINK_TRACE_MAX_HZ 2000000
302
303 #define STLINK_V3_MAX_FREQ_NB 10
304
305 #define REQUEST_SENSE 0x03
306 #define REQUEST_SENSE_LENGTH 18
307
308 /*
309 * Map the relevant features, quirks and workaround for specific firmware
310 * version of stlink
311 */
312 #define STLINK_F_HAS_TRACE BIT(0)
313 #define STLINK_F_HAS_SWD_SET_FREQ BIT(1)
314 #define STLINK_F_HAS_JTAG_SET_FREQ BIT(2)
315 #define STLINK_F_HAS_MEM_16BIT BIT(3)
316 #define STLINK_F_HAS_GETLASTRWSTATUS2 BIT(4)
317 #define STLINK_F_HAS_DAP_REG BIT(5)
318 #define STLINK_F_QUIRK_JTAG_DP_READ BIT(6)
319 #define STLINK_F_HAS_AP_INIT BIT(7)
320 #define STLINK_F_HAS_DPBANKSEL BIT(8)
321 #define STLINK_F_HAS_RW8_512BYTES BIT(9)
322 #define STLINK_F_FIX_CLOSE_AP BIT(10)
323
324 /* aliases */
325 #define STLINK_F_HAS_TARGET_VOLT STLINK_F_HAS_TRACE
326
327 struct speed_map {
328 int speed;
329 int speed_divisor;
330 };
331
332 /* SWD clock speed */
333 static const struct speed_map stlink_khz_to_speed_map_swd[] = {
334 {4000, 0},
335 {1800, 1}, /* default */
336 {1200, 2},
337 {950, 3},
338 {480, 7},
339 {240, 15},
340 {125, 31},
341 {100, 40},
342 {50, 79},
343 {25, 158},
344 {15, 265},
345 {5, 798}
346 };
347
348 /* JTAG clock speed */
349 static const struct speed_map stlink_khz_to_speed_map_jtag[] = {
350 {9000, 4},
351 {4500, 8},
352 {2250, 16},
353 {1125, 32}, /* default */
354 {562, 64},
355 {281, 128},
356 {140, 256}
357 };
358
359 static void stlink_usb_init_buffer(void *handle, uint8_t direction, uint32_t size);
360 static int stlink_swim_status(void *handle);
361 void stlink_dump_speed_map(const struct speed_map *map, unsigned int map_size);
362 static int stlink_get_com_freq(void *handle, bool is_jtag, struct speed_map *map);
363 static int stlink_speed(void *handle, int khz, bool query);
364
365 /** */
366 static unsigned int stlink_usb_block(void *handle)
367 {
368 struct stlink_usb_handle_s *h = handle;
369
370 assert(handle != NULL);
371
372 if (h->version.flags & STLINK_F_HAS_RW8_512BYTES)
373 return STLINKV3_MAX_RW8;
374 else
375 return STLINK_MAX_RW8;
376 }
377
378
379
380 #ifdef USE_LIBUSB_ASYNCIO
381
382 static LIBUSB_CALL void sync_transfer_cb(struct libusb_transfer *transfer)
383 {
384 int *completed = transfer->user_data;
385 *completed = 1;
386 /* caller interprets result and frees transfer */
387 }
388
389
390 static void sync_transfer_wait_for_completion(struct libusb_transfer *transfer)
391 {
392 int r, *completed = transfer->user_data;
393
394 /* Assuming a single libusb context exists. There no existing interface into this
395 * module to pass a libusb context.
396 */
397 struct libusb_context *ctx = NULL;
398
399 while (!*completed) {
400 r = libusb_handle_events_completed(ctx, completed);
401 if (r < 0) {
402 if (r == LIBUSB_ERROR_INTERRUPTED)
403 continue;
404 libusb_cancel_transfer(transfer);
405 continue;
406 }
407 }
408 }
409
410
411 static int transfer_error_status(const struct libusb_transfer *transfer)
412 {
413 int r = 0;
414
415 switch (transfer->status) {
416 case LIBUSB_TRANSFER_COMPLETED:
417 r = 0;
418 break;
419 case LIBUSB_TRANSFER_TIMED_OUT:
420 r = LIBUSB_ERROR_TIMEOUT;
421 break;
422 case LIBUSB_TRANSFER_STALL:
423 r = LIBUSB_ERROR_PIPE;
424 break;
425 case LIBUSB_TRANSFER_OVERFLOW:
426 r = LIBUSB_ERROR_OVERFLOW;
427 break;
428 case LIBUSB_TRANSFER_NO_DEVICE:
429 r = LIBUSB_ERROR_NO_DEVICE;
430 break;
431 case LIBUSB_TRANSFER_ERROR:
432 case LIBUSB_TRANSFER_CANCELLED:
433 r = LIBUSB_ERROR_IO;
434 break;
435 default:
436 r = LIBUSB_ERROR_OTHER;
437 break;
438 }
439
440 return r;
441 }
442
443 struct jtag_xfer {
444 int ep;
445 uint8_t *buf;
446 size_t size;
447 /* Internal */
448 int retval;
449 int completed;
450 size_t transfer_size;
451 struct libusb_transfer *transfer;
452 };
453
454 static int jtag_libusb_bulk_transfer_n(
455 struct libusb_device_handle *dev_handle,
456 struct jtag_xfer *transfers,
457 size_t n_transfers,
458 int timeout)
459 {
460 int retval = 0;
461 int returnval = ERROR_OK;
462
463
464 for (size_t i = 0; i < n_transfers; ++i) {
465 transfers[i].retval = 0;
466 transfers[i].completed = 0;
467 transfers[i].transfer_size = 0;
468 transfers[i].transfer = libusb_alloc_transfer(0);
469
470 if (transfers[i].transfer == NULL) {
471 for (size_t j = 0; j < i; ++j)
472 libusb_free_transfer(transfers[j].transfer);
473
474 LOG_DEBUG("ERROR, failed to alloc usb transfers");
475 for (size_t k = 0; k < n_transfers; ++k)
476 transfers[k].retval = LIBUSB_ERROR_NO_MEM;
477 return ERROR_FAIL;
478 }
479 }
480
481 for (size_t i = 0; i < n_transfers; ++i) {
482 libusb_fill_bulk_transfer(
483 transfers[i].transfer,
484 dev_handle,
485 transfers[i].ep, transfers[i].buf, transfers[i].size,
486 sync_transfer_cb, &transfers[i].completed, timeout);
487 transfers[i].transfer->type = LIBUSB_TRANSFER_TYPE_BULK;
488
489 retval = libusb_submit_transfer(transfers[i].transfer);
490 if (retval < 0) {
491 LOG_DEBUG("ERROR, failed to submit transfer %zu, error %d", i, retval);
492
493 /* Probably no point continuing to submit transfers once a submission fails.
494 * As a result, tag all remaining transfers as errors.
495 */
496 for (size_t j = i; j < n_transfers; ++j)
497 transfers[j].retval = retval;
498
499 returnval = ERROR_FAIL;
500 break;
501 }
502 }
503
504 /* Wait for every submitted USB transfer to complete.
505 */
506 for (size_t i = 0; i < n_transfers; ++i) {
507 if (transfers[i].retval == 0) {
508 sync_transfer_wait_for_completion(transfers[i].transfer);
509
510 retval = transfer_error_status(transfers[i].transfer);
511 if (retval) {
512 returnval = ERROR_FAIL;
513 transfers[i].retval = retval;
514 LOG_DEBUG("ERROR, transfer %zu failed, error %d", i, retval);
515 } else {
516 /* Assuming actual_length is only valid if there is no transfer error.
517 */
518 transfers[i].transfer_size = transfers[i].transfer->actual_length;
519 }
520 }
521
522 libusb_free_transfer(transfers[i].transfer);
523 transfers[i].transfer = NULL;
524 }
525
526 return returnval;
527 }
528
529 #endif
530
531
532 /** */
533 static int stlink_usb_xfer_v1_get_status(void *handle)
534 {
535 struct stlink_usb_handle_s *h = handle;
536 int tr, ret;
537
538 assert(handle != NULL);
539
540 /* read status */
541 memset(h->cmdbuf, 0, STLINK_SG_SIZE);
542
543 ret = jtag_libusb_bulk_read(h->fd, h->rx_ep, (char *)h->cmdbuf, 13,
544 STLINK_READ_TIMEOUT, &tr);
545 if (ret || tr != 13)
546 return ERROR_FAIL;
547
548 uint32_t t1;
549
550 t1 = buf_get_u32(h->cmdbuf, 0, 32);
551
552 /* check for USBS */
553 if (t1 != 0x53425355)
554 return ERROR_FAIL;
555 /*
556 * CSW status:
557 * 0 success
558 * 1 command failure
559 * 2 phase error
560 */
561 if (h->cmdbuf[12] != 0)
562 return ERROR_FAIL;
563
564 return ERROR_OK;
565 }
566
567 #ifdef USE_LIBUSB_ASYNCIO
568 static int stlink_usb_xfer_rw(void *handle, int cmdsize, const uint8_t *buf, int size)
569 {
570 struct stlink_usb_handle_s *h = handle;
571
572 assert(handle != NULL);
573
574 size_t n_transfers = 0;
575 struct jtag_xfer transfers[2];
576
577 memset(transfers, 0, sizeof(transfers));
578
579 transfers[0].ep = h->tx_ep;
580 transfers[0].buf = h->cmdbuf;
581 transfers[0].size = cmdsize;
582
583 ++n_transfers;
584
585 if (h->direction == h->tx_ep && size) {
586 transfers[1].ep = h->tx_ep;
587 transfers[1].buf = (uint8_t *)buf;
588 transfers[1].size = size;
589
590 ++n_transfers;
591 } else if (h->direction == h->rx_ep && size) {
592 transfers[1].ep = h->rx_ep;
593 transfers[1].buf = (uint8_t *)buf;
594 transfers[1].size = size;
595
596 ++n_transfers;
597 }
598
599 return jtag_libusb_bulk_transfer_n(
600 h->fd,
601 transfers,
602 n_transfers,
603 STLINK_WRITE_TIMEOUT);
604 }
605 #else
606 static int stlink_usb_xfer_rw(void *handle, int cmdsize, const uint8_t *buf, int size)
607 {
608 struct stlink_usb_handle_s *h = handle;
609 int tr, ret;
610
611 assert(handle != NULL);
612
613 ret = jtag_libusb_bulk_write(h->fd, h->tx_ep, (char *)h->cmdbuf,
614 cmdsize, STLINK_WRITE_TIMEOUT, &tr);
615 if (ret || tr != cmdsize)
616 return ERROR_FAIL;
617
618 if (h->direction == h->tx_ep && size) {
619 ret = jtag_libusb_bulk_write(h->fd, h->tx_ep, (char *)buf,
620 size, STLINK_WRITE_TIMEOUT, &tr);
621 if (ret || tr != size) {
622 LOG_DEBUG("bulk write failed");
623 return ERROR_FAIL;
624 }
625 } else if (h->direction == h->rx_ep && size) {
626 ret = jtag_libusb_bulk_read(h->fd, h->rx_ep, (char *)buf,
627 size, STLINK_READ_TIMEOUT, &tr);
628 if (ret || tr != size) {
629 LOG_DEBUG("bulk read failed");
630 return ERROR_FAIL;
631 }
632 }
633
634 return ERROR_OK;
635 }
636 #endif
637
638 /** */
639 static int stlink_usb_xfer_v1_get_sense(void *handle)
640 {
641 int res;
642 struct stlink_usb_handle_s *h = handle;
643
644 assert(handle != NULL);
645
646 stlink_usb_init_buffer(handle, h->rx_ep, 16);
647
648 h->cmdbuf[h->cmdidx++] = REQUEST_SENSE;
649 h->cmdbuf[h->cmdidx++] = 0;
650 h->cmdbuf[h->cmdidx++] = 0;
651 h->cmdbuf[h->cmdidx++] = 0;
652 h->cmdbuf[h->cmdidx++] = REQUEST_SENSE_LENGTH;
653
654 res = stlink_usb_xfer_rw(handle, REQUEST_SENSE_LENGTH, h->databuf, 16);
655
656 if (res != ERROR_OK)
657 return res;
658
659 if (stlink_usb_xfer_v1_get_status(handle) != ERROR_OK)
660 return ERROR_FAIL;
661
662 return ERROR_OK;
663 }
664
665 /*
666 transfers block in cmdbuf
667 <size> indicates number of bytes in the following
668 data phase.
669 Ignore the (eventual) error code in the received packet.
670 */
671 static int stlink_usb_xfer_noerrcheck(void *handle, const uint8_t *buf, int size)
672 {
673 int err, cmdsize = STLINK_CMD_SIZE_V2;
674 struct stlink_usb_handle_s *h = handle;
675
676 assert(handle != NULL);
677
678 if (h->version.stlink == 1) {
679 cmdsize = STLINK_SG_SIZE;
680 /* put length in bCBWCBLength */
681 h->cmdbuf[14] = h->cmdidx-15;
682 }
683
684 err = stlink_usb_xfer_rw(handle, cmdsize, buf, size);
685
686 if (err != ERROR_OK)
687 return err;
688
689 if (h->version.stlink == 1) {
690 if (stlink_usb_xfer_v1_get_status(handle) != ERROR_OK) {
691 /* check csw status */
692 if (h->cmdbuf[12] == 1) {
693 LOG_DEBUG("get sense");
694 if (stlink_usb_xfer_v1_get_sense(handle) != ERROR_OK)
695 return ERROR_FAIL;
696 }
697 return ERROR_FAIL;
698 }
699 }
700
701 return ERROR_OK;
702 }
703
704 /**
705 Converts an STLINK status code held in the first byte of a response
706 to an openocd error, logs any error/wait status as debug output.
707 */
708 static int stlink_usb_error_check(void *handle)
709 {
710 struct stlink_usb_handle_s *h = handle;
711
712 assert(handle != NULL);
713
714 if (h->st_mode == STLINK_MODE_DEBUG_SWIM) {
715 switch (h->databuf[0]) {
716 case STLINK_SWIM_ERR_OK:
717 return ERROR_OK;
718 case STLINK_SWIM_BUSY:
719 return ERROR_WAIT;
720 default:
721 LOG_DEBUG("unknown/unexpected STLINK status code 0x%x", h->databuf[0]);
722 return ERROR_FAIL;
723 }
724 }
725
726 /* TODO: no error checking yet on api V1 */
727 if (h->version.jtag_api == STLINK_JTAG_API_V1)
728 h->databuf[0] = STLINK_DEBUG_ERR_OK;
729
730 switch (h->databuf[0]) {
731 case STLINK_DEBUG_ERR_OK:
732 return ERROR_OK;
733 case STLINK_DEBUG_ERR_FAULT:
734 LOG_DEBUG("SWD fault response (0x%x)", STLINK_DEBUG_ERR_FAULT);
735 return ERROR_FAIL;
736 case STLINK_SWD_AP_WAIT:
737 LOG_DEBUG("wait status SWD_AP_WAIT (0x%x)", STLINK_SWD_AP_WAIT);
738 return ERROR_WAIT;
739 case STLINK_SWD_DP_WAIT:
740 LOG_DEBUG("wait status SWD_DP_WAIT (0x%x)", STLINK_SWD_DP_WAIT);
741 return ERROR_WAIT;
742 case STLINK_JTAG_GET_IDCODE_ERROR:
743 LOG_DEBUG("STLINK_JTAG_GET_IDCODE_ERROR");
744 return ERROR_FAIL;
745 case STLINK_JTAG_WRITE_ERROR:
746 LOG_DEBUG("Write error");
747 return ERROR_FAIL;
748 case STLINK_JTAG_WRITE_VERIF_ERROR:
749 LOG_DEBUG("Write verify error, ignoring");
750 return ERROR_OK;
751 case STLINK_SWD_AP_FAULT:
752 /* git://git.ac6.fr/openocd commit 657e3e885b9ee10
753 * returns ERROR_OK with the comment:
754 * Change in error status when reading outside RAM.
755 * This fix allows CDT plugin to visualize memory.
756 */
757 LOG_DEBUG("STLINK_SWD_AP_FAULT");
758 return ERROR_FAIL;
759 case STLINK_SWD_AP_ERROR:
760 LOG_DEBUG("STLINK_SWD_AP_ERROR");
761 return ERROR_FAIL;
762 case STLINK_SWD_AP_PARITY_ERROR:
763 LOG_DEBUG("STLINK_SWD_AP_PARITY_ERROR");
764 return ERROR_FAIL;
765 case STLINK_SWD_DP_FAULT:
766 LOG_DEBUG("STLINK_SWD_DP_FAULT");
767 return ERROR_FAIL;
768 case STLINK_SWD_DP_ERROR:
769 LOG_DEBUG("STLINK_SWD_DP_ERROR");
770 return ERROR_FAIL;
771 case STLINK_SWD_DP_PARITY_ERROR:
772 LOG_DEBUG("STLINK_SWD_DP_PARITY_ERROR");
773 return ERROR_FAIL;
774 case STLINK_SWD_AP_WDATA_ERROR:
775 LOG_DEBUG("STLINK_SWD_AP_WDATA_ERROR");
776 return ERROR_FAIL;
777 case STLINK_SWD_AP_STICKY_ERROR:
778 LOG_DEBUG("STLINK_SWD_AP_STICKY_ERROR");
779 return ERROR_FAIL;
780 case STLINK_SWD_AP_STICKYORUN_ERROR:
781 LOG_DEBUG("STLINK_SWD_AP_STICKYORUN_ERROR");
782 return ERROR_FAIL;
783 case STLINK_BAD_AP_ERROR:
784 LOG_DEBUG("STLINK_BAD_AP_ERROR");
785 return ERROR_FAIL;
786 default:
787 LOG_DEBUG("unknown/unexpected STLINK status code 0x%x", h->databuf[0]);
788 return ERROR_FAIL;
789 }
790 }
791
792 /*
793 * Wrapper around stlink_usb_xfer_noerrcheck()
794 * to check the error code in the received packet
795 */
796 static int stlink_usb_xfer_errcheck(void *handle, const uint8_t *buf, int size)
797 {
798 int retval;
799
800 assert(size > 0);
801
802 retval = stlink_usb_xfer_noerrcheck(handle, buf, size);
803 if (retval != ERROR_OK)
804 return retval;
805
806 return stlink_usb_error_check(handle);
807 }
808
809 /** Issue an STLINK command via USB transfer, with retries on any wait status responses.
810
811 Works for commands where the STLINK_DEBUG status is returned in the first
812 byte of the response packet. For SWIM a SWIM_READSTATUS is requested instead.
813
814 Returns an openocd result code.
815 */
816 static int stlink_cmd_allow_retry(void *handle, const uint8_t *buf, int size)
817 {
818 int retries = 0;
819 int res;
820 struct stlink_usb_handle_s *h = handle;
821
822 while (1) {
823 if ((h->st_mode != STLINK_MODE_DEBUG_SWIM) || !retries) {
824 res = stlink_usb_xfer_noerrcheck(handle, buf, size);
825 if (res != ERROR_OK)
826 return res;
827 }
828
829 if (h->st_mode == STLINK_MODE_DEBUG_SWIM) {
830 res = stlink_swim_status(handle);
831 if (res != ERROR_OK)
832 return res;
833 }
834
835 res = stlink_usb_error_check(handle);
836 if (res == ERROR_WAIT && retries < MAX_WAIT_RETRIES) {
837 unsigned int delay_us = (1<<retries++) * 1000;
838 LOG_DEBUG("stlink_cmd_allow_retry ERROR_WAIT, retry %d, delaying %u microseconds", retries, delay_us);
839 usleep(delay_us);
840 continue;
841 }
842 return res;
843 }
844 }
845
846 /** */
847 static int stlink_usb_read_trace(void *handle, const uint8_t *buf, int size)
848 {
849 struct stlink_usb_handle_s *h = handle;
850 int tr, ret;
851
852 assert(handle != NULL);
853
854 assert(h->version.flags & STLINK_F_HAS_TRACE);
855
856 ret = jtag_libusb_bulk_read(h->fd, h->trace_ep, (char *)buf, size,
857 STLINK_READ_TIMEOUT, &tr);
858 if (ret || tr != size) {
859 LOG_ERROR("bulk trace read failed");
860 return ERROR_FAIL;
861 }
862
863 return ERROR_OK;
864 }
865
866 /*
867 this function writes transfer length in
868 the right place in the cb
869 */
870 static void stlink_usb_set_cbw_transfer_datalength(void *handle, uint32_t size)
871 {
872 struct stlink_usb_handle_s *h = handle;
873
874 buf_set_u32(h->cmdbuf+8, 0, 32, size);
875 }
876
877 static void stlink_usb_xfer_v1_create_cmd(void *handle, uint8_t direction, uint32_t size)
878 {
879 struct stlink_usb_handle_s *h = handle;
880
881 /* fill the send buffer */
882 strcpy((char *)h->cmdbuf, "USBC");
883 h->cmdidx += 4;
884 /* csw tag not used */
885 buf_set_u32(h->cmdbuf+h->cmdidx, 0, 32, 0);
886 h->cmdidx += 4;
887 /* cbw data transfer length (in the following data phase in or out) */
888 buf_set_u32(h->cmdbuf+h->cmdidx, 0, 32, size);
889 h->cmdidx += 4;
890 /* cbw flags */
891 h->cmdbuf[h->cmdidx++] = (direction == h->rx_ep ? ENDPOINT_IN : ENDPOINT_OUT);
892 h->cmdbuf[h->cmdidx++] = 0; /* lun */
893 /* cdb clength (is filled in at xfer) */
894 h->cmdbuf[h->cmdidx++] = 0;
895 }
896
897 /** */
898 static void stlink_usb_init_buffer(void *handle, uint8_t direction, uint32_t size)
899 {
900 struct stlink_usb_handle_s *h = handle;
901
902 h->direction = direction;
903
904 h->cmdidx = 0;
905
906 memset(h->cmdbuf, 0, STLINK_SG_SIZE);
907 memset(h->databuf, 0, STLINK_DATA_SIZE);
908
909 if (h->version.stlink == 1)
910 stlink_usb_xfer_v1_create_cmd(handle, direction, size);
911 }
912
913 /** */
914 static int stlink_usb_version(void *handle)
915 {
916 int res;
917 uint32_t flags;
918 uint16_t version;
919 uint8_t v, x, y, jtag, swim, msd, bridge = 0;
920 char v_str[5 * (1 + 3) + 1]; /* VvJjMmBbSs */
921 char *p;
922 struct stlink_usb_handle_s *h = handle;
923
924 assert(handle != NULL);
925
926 stlink_usb_init_buffer(handle, h->rx_ep, 6);
927
928 h->cmdbuf[h->cmdidx++] = STLINK_GET_VERSION;
929
930 res = stlink_usb_xfer_noerrcheck(handle, h->databuf, 6);
931
932 if (res != ERROR_OK)
933 return res;
934
935 version = be_to_h_u16(h->databuf);
936 v = (version >> 12) & 0x0f;
937 x = (version >> 6) & 0x3f;
938 y = version & 0x3f;
939
940 h->vid = le_to_h_u16(h->databuf + 2);
941 h->pid = le_to_h_u16(h->databuf + 4);
942
943 switch (h->pid) {
944 case STLINK_V2_1_PID:
945 case STLINK_V2_1_NO_MSD_PID:
946 if ((x <= 22 && y == 7) || (x >= 25 && y >= 7 && y <= 12)) {
947 /* MxSy : STM8 V2.1 - SWIM only */
948 msd = x;
949 swim = y;
950 jtag = 0;
951 } else {
952 /* JxMy : STM32 V2.1 - JTAG/SWD only */
953 jtag = x;
954 msd = y;
955 swim = 0;
956 }
957 break;
958 default:
959 jtag = x;
960 swim = y;
961 msd = 0;
962 break;
963 }
964
965 /* STLINK-V3 requires a specific command */
966 if (v == 3 && x == 0 && y == 0) {
967 stlink_usb_init_buffer(handle, h->rx_ep, 16);
968
969 h->cmdbuf[h->cmdidx++] = STLINK_APIV3_GET_VERSION_EX;
970
971 res = stlink_usb_xfer_noerrcheck(handle, h->databuf, 12);
972 if (res != ERROR_OK)
973 return res;
974
975 v = h->databuf[0];
976 swim = h->databuf[1];
977 jtag = h->databuf[2];
978 msd = h->databuf[3];
979 bridge = h->databuf[4];
980 h->vid = le_to_h_u16(h->databuf + 8);
981 h->pid = le_to_h_u16(h->databuf + 10);
982 }
983
984 h->version.stlink = v;
985 h->version.jtag = jtag;
986 h->version.swim = swim;
987
988 flags = 0;
989 switch (h->version.stlink) {
990 case 1:
991 /* ST-LINK/V1 from J11 switch to api-v2 (and support SWD) */
992 if (h->version.jtag >= 11)
993 h->version.jtag_api = STLINK_JTAG_API_V2;
994 else
995 h->version.jtag_api = STLINK_JTAG_API_V1;
996
997 break;
998 case 2:
999 /* all ST-LINK/V2 and ST-Link/V2.1 use api-v2 */
1000 h->version.jtag_api = STLINK_JTAG_API_V2;
1001
1002 /* API for trace from J13 */
1003 /* API for target voltage from J13 */
1004 if (h->version.jtag >= 13)
1005 flags |= STLINK_F_HAS_TRACE;
1006
1007 /* preferred API to get last R/W status from J15 */
1008 if (h->version.jtag >= 15)
1009 flags |= STLINK_F_HAS_GETLASTRWSTATUS2;
1010
1011 /* API to set SWD frequency from J22 */
1012 if (h->version.jtag >= 22)
1013 flags |= STLINK_F_HAS_SWD_SET_FREQ;
1014
1015 /* API to set JTAG frequency from J24 */
1016 /* API to access DAP registers from J24 */
1017 if (h->version.jtag >= 24) {
1018 flags |= STLINK_F_HAS_JTAG_SET_FREQ;
1019 flags |= STLINK_F_HAS_DAP_REG;
1020 }
1021
1022 /* Quirk for read DP in JTAG mode (V2 only) from J24, fixed in J32 */
1023 if (h->version.jtag >= 24 && h->version.jtag < 32)
1024 flags |= STLINK_F_QUIRK_JTAG_DP_READ;
1025
1026 /* API to read/write memory at 16 bit from J26 */
1027 if (h->version.jtag >= 26)
1028 flags |= STLINK_F_HAS_MEM_16BIT;
1029
1030 /* API required to init AP before any AP access from J28 */
1031 if (h->version.jtag >= 28)
1032 flags |= STLINK_F_HAS_AP_INIT;
1033
1034 /* API required to return proper error code on close AP from J29 */
1035 if (h->version.jtag >= 29)
1036 flags |= STLINK_F_FIX_CLOSE_AP;
1037
1038 /* Banked regs (DPv1 & DPv2) support from V2J32 */
1039 if (h->version.jtag >= 32)
1040 flags |= STLINK_F_HAS_DPBANKSEL;
1041
1042 break;
1043 case 3:
1044 /* all STLINK-V3 use api-v3 */
1045 h->version.jtag_api = STLINK_JTAG_API_V3;
1046
1047 /* STLINK-V3 is a superset of ST-LINK/V2 */
1048
1049 /* API for trace */
1050 /* API for target voltage */
1051 flags |= STLINK_F_HAS_TRACE;
1052
1053 /* preferred API to get last R/W status */
1054 flags |= STLINK_F_HAS_GETLASTRWSTATUS2;
1055
1056 /* API to access DAP registers */
1057 flags |= STLINK_F_HAS_DAP_REG;
1058
1059 /* API to read/write memory at 16 bit */
1060 flags |= STLINK_F_HAS_MEM_16BIT;
1061
1062 /* API required to init AP before any AP access */
1063 flags |= STLINK_F_HAS_AP_INIT;
1064
1065 /* API required to return proper error code on close AP */
1066 flags |= STLINK_F_FIX_CLOSE_AP;
1067
1068 /* Banked regs (DPv1 & DPv2) support from V3J2 */
1069 if (h->version.jtag >= 2)
1070 flags |= STLINK_F_HAS_DPBANKSEL;
1071
1072 /* 8bit read/write max packet size 512 bytes from V3J6 */
1073 if (h->version.jtag >= 6)
1074 flags |= STLINK_F_HAS_RW8_512BYTES;
1075
1076 break;
1077 default:
1078 break;
1079 }
1080 h->version.flags = flags;
1081
1082 p = v_str;
1083 p += sprintf(p, "V%d", v);
1084 if (jtag || !msd)
1085 p += sprintf(p, "J%d", jtag);
1086 if (msd)
1087 p += sprintf(p, "M%d", msd);
1088 if (bridge)
1089 p += sprintf(p, "B%d", bridge);
1090 if (swim || !msd)
1091 sprintf(p, "S%d", swim);
1092
1093 LOG_INFO("STLINK %s (API v%d) VID:PID %04X:%04X",
1094 v_str,
1095 h->version.jtag_api,
1096 h->vid,
1097 h->pid);
1098
1099 return ERROR_OK;
1100 }
1101
1102 static int stlink_usb_check_voltage(void *handle, float *target_voltage)
1103 {
1104 struct stlink_usb_handle_s *h = handle;
1105 uint32_t adc_results[2];
1106
1107 /* no error message, simply quit with error */
1108 if (!(h->version.flags & STLINK_F_HAS_TARGET_VOLT))
1109 return ERROR_COMMAND_NOTFOUND;
1110
1111 stlink_usb_init_buffer(handle, h->rx_ep, 8);
1112
1113 h->cmdbuf[h->cmdidx++] = STLINK_GET_TARGET_VOLTAGE;
1114
1115 int result = stlink_usb_xfer_noerrcheck(handle, h->databuf, 8);
1116
1117 if (result != ERROR_OK)
1118 return result;
1119
1120 /* convert result */
1121 adc_results[0] = le_to_h_u32(h->databuf);
1122 adc_results[1] = le_to_h_u32(h->databuf + 4);
1123
1124 *target_voltage = 0;
1125
1126 if (adc_results[0])
1127 *target_voltage = 2 * ((float)adc_results[1]) * (float)(1.2 / adc_results[0]);
1128
1129 LOG_INFO("Target voltage: %f", (double)*target_voltage);
1130
1131 return ERROR_OK;
1132 }
1133
1134 static int stlink_usb_set_swdclk(void *handle, uint16_t clk_divisor)
1135 {
1136 struct stlink_usb_handle_s *h = handle;
1137
1138 assert(handle != NULL);
1139
1140 if (!(h->version.flags & STLINK_F_HAS_SWD_SET_FREQ))
1141 return ERROR_COMMAND_NOTFOUND;
1142
1143 stlink_usb_init_buffer(handle, h->rx_ep, 2);
1144
1145 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1146 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_SWD_SET_FREQ;
1147 h_u16_to_le(h->cmdbuf+h->cmdidx, clk_divisor);
1148 h->cmdidx += 2;
1149
1150 int result = stlink_cmd_allow_retry(handle, h->databuf, 2);
1151
1152 if (result != ERROR_OK)
1153 return result;
1154
1155 return ERROR_OK;
1156 }
1157
1158 static int stlink_usb_set_jtagclk(void *handle, uint16_t clk_divisor)
1159 {
1160 struct stlink_usb_handle_s *h = handle;
1161
1162 assert(handle != NULL);
1163
1164 if (!(h->version.flags & STLINK_F_HAS_JTAG_SET_FREQ))
1165 return ERROR_COMMAND_NOTFOUND;
1166
1167 stlink_usb_init_buffer(handle, h->rx_ep, 2);
1168
1169 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1170 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_JTAG_SET_FREQ;
1171 h_u16_to_le(h->cmdbuf+h->cmdidx, clk_divisor);
1172 h->cmdidx += 2;
1173
1174 int result = stlink_cmd_allow_retry(handle, h->databuf, 2);
1175
1176 if (result != ERROR_OK)
1177 return result;
1178
1179 return ERROR_OK;
1180 }
1181
1182 /** */
1183 static int stlink_usb_current_mode(void *handle, uint8_t *mode)
1184 {
1185 int res;
1186 struct stlink_usb_handle_s *h = handle;
1187
1188 assert(handle != NULL);
1189
1190 stlink_usb_init_buffer(handle, h->rx_ep, 2);
1191
1192 h->cmdbuf[h->cmdidx++] = STLINK_GET_CURRENT_MODE;
1193
1194 res = stlink_usb_xfer_noerrcheck(handle, h->databuf, 2);
1195
1196 if (res != ERROR_OK)
1197 return res;
1198
1199 *mode = h->databuf[0];
1200
1201 return ERROR_OK;
1202 }
1203
1204 /** */
1205 static int stlink_usb_mode_enter(void *handle, enum stlink_mode type)
1206 {
1207 int rx_size = 0;
1208 struct stlink_usb_handle_s *h = handle;
1209
1210 assert(handle != NULL);
1211
1212 /* on api V2 we are able the read the latest command
1213 * status
1214 * TODO: we need the test on api V1 too
1215 */
1216 if (h->version.jtag_api != STLINK_JTAG_API_V1)
1217 rx_size = 2;
1218
1219 stlink_usb_init_buffer(handle, h->rx_ep, rx_size);
1220
1221 switch (type) {
1222 case STLINK_MODE_DEBUG_JTAG:
1223 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1224 if (h->version.jtag_api == STLINK_JTAG_API_V1)
1225 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_ENTER;
1226 else
1227 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_ENTER;
1228 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_ENTER_JTAG_NO_RESET;
1229 break;
1230 case STLINK_MODE_DEBUG_SWD:
1231 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1232 if (h->version.jtag_api == STLINK_JTAG_API_V1)
1233 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_ENTER;
1234 else
1235 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_ENTER;
1236 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_ENTER_SWD_NO_RESET;
1237 break;
1238 case STLINK_MODE_DEBUG_SWIM:
1239 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_COMMAND;
1240 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_ENTER;
1241 /* swim enter does not return any response or status */
1242 return stlink_usb_xfer_noerrcheck(handle, h->databuf, 0);
1243 case STLINK_MODE_DFU:
1244 case STLINK_MODE_MASS:
1245 default:
1246 return ERROR_FAIL;
1247 }
1248
1249 return stlink_cmd_allow_retry(handle, h->databuf, rx_size);
1250 }
1251
1252 /** */
1253 static int stlink_usb_mode_leave(void *handle, enum stlink_mode type)
1254 {
1255 int res;
1256 struct stlink_usb_handle_s *h = handle;
1257
1258 assert(handle != NULL);
1259
1260 /* command with no reply, use a valid endpoint but zero size */
1261 stlink_usb_init_buffer(handle, h->rx_ep, 0);
1262
1263 switch (type) {
1264 case STLINK_MODE_DEBUG_JTAG:
1265 case STLINK_MODE_DEBUG_SWD:
1266 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1267 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_EXIT;
1268 break;
1269 case STLINK_MODE_DEBUG_SWIM:
1270 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_COMMAND;
1271 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_EXIT;
1272 break;
1273 case STLINK_MODE_DFU:
1274 h->cmdbuf[h->cmdidx++] = STLINK_DFU_COMMAND;
1275 h->cmdbuf[h->cmdidx++] = STLINK_DFU_EXIT;
1276 break;
1277 case STLINK_MODE_MASS:
1278 default:
1279 return ERROR_FAIL;
1280 }
1281
1282 res = stlink_usb_xfer_noerrcheck(handle, h->databuf, 0);
1283
1284 if (res != ERROR_OK)
1285 return res;
1286
1287 return ERROR_OK;
1288 }
1289
1290 static int stlink_usb_assert_srst(void *handle, int srst);
1291
1292 static enum stlink_mode stlink_get_mode(enum hl_transports t)
1293 {
1294 switch (t) {
1295 case HL_TRANSPORT_SWD:
1296 return STLINK_MODE_DEBUG_SWD;
1297 case HL_TRANSPORT_JTAG:
1298 return STLINK_MODE_DEBUG_JTAG;
1299 default:
1300 return STLINK_MODE_UNKNOWN;
1301 }
1302 }
1303
1304 /** */
1305 static int stlink_usb_init_mode(void *handle, bool connect_under_reset, int initial_interface_speed)
1306 {
1307 int res;
1308 uint8_t mode;
1309 enum stlink_mode emode;
1310 struct stlink_usb_handle_s *h = handle;
1311
1312 assert(handle != NULL);
1313
1314 res = stlink_usb_current_mode(handle, &mode);
1315
1316 if (res != ERROR_OK)
1317 return res;
1318
1319 LOG_DEBUG("MODE: 0x%02X", mode);
1320
1321 /* try to exit current mode */
1322 switch (mode) {
1323 case STLINK_DEV_DFU_MODE:
1324 emode = STLINK_MODE_DFU;
1325 break;
1326 case STLINK_DEV_DEBUG_MODE:
1327 emode = STLINK_MODE_DEBUG_SWD;
1328 break;
1329 case STLINK_DEV_SWIM_MODE:
1330 emode = STLINK_MODE_DEBUG_SWIM;
1331 break;
1332 case STLINK_DEV_BOOTLOADER_MODE:
1333 case STLINK_DEV_MASS_MODE:
1334 default:
1335 emode = STLINK_MODE_UNKNOWN;
1336 break;
1337 }
1338
1339 if (emode != STLINK_MODE_UNKNOWN) {
1340 res = stlink_usb_mode_leave(handle, emode);
1341
1342 if (res != ERROR_OK)
1343 return res;
1344 }
1345
1346 res = stlink_usb_current_mode(handle, &mode);
1347
1348 if (res != ERROR_OK)
1349 return res;
1350
1351 /* we check the target voltage here as an aid to debugging connection problems.
1352 * the stlink requires the target Vdd to be connected for reliable debugging.
1353 * this cmd is supported in all modes except DFU
1354 */
1355 if (mode != STLINK_DEV_DFU_MODE) {
1356
1357 float target_voltage;
1358
1359 /* check target voltage (if supported) */
1360 res = stlink_usb_check_voltage(h, &target_voltage);
1361
1362 if (res != ERROR_OK) {
1363 if (res != ERROR_COMMAND_NOTFOUND)
1364 LOG_ERROR("voltage check failed");
1365 /* attempt to continue as it is not a catastrophic failure */
1366 } else {
1367 /* check for a sensible target voltage, operating range is 1.65-5.5v
1368 * according to datasheet */
1369 if (target_voltage < 1.5)
1370 LOG_ERROR("target voltage may be too low for reliable debugging");
1371 }
1372 }
1373
1374 LOG_DEBUG("MODE: 0x%02X", mode);
1375
1376 /* set selected mode */
1377 emode = h->st_mode;
1378
1379 if (emode == STLINK_MODE_UNKNOWN) {
1380 LOG_ERROR("selected mode (transport) not supported");
1381 return ERROR_FAIL;
1382 }
1383
1384 /* set the speed before entering the mode, as the chip discovery phase should be done at this speed too */
1385 if (emode == STLINK_MODE_DEBUG_JTAG) {
1386 if (h->version.flags & STLINK_F_HAS_JTAG_SET_FREQ) {
1387 stlink_dump_speed_map(stlink_khz_to_speed_map_jtag, ARRAY_SIZE(stlink_khz_to_speed_map_jtag));
1388 stlink_speed(h, initial_interface_speed, false);
1389 }
1390 } else if (emode == STLINK_MODE_DEBUG_SWD) {
1391 if (h->version.flags & STLINK_F_HAS_SWD_SET_FREQ) {
1392 stlink_dump_speed_map(stlink_khz_to_speed_map_swd, ARRAY_SIZE(stlink_khz_to_speed_map_swd));
1393 stlink_speed(h, initial_interface_speed, false);
1394 }
1395 }
1396
1397 if (h->version.jtag_api == STLINK_JTAG_API_V3) {
1398 struct speed_map map[STLINK_V3_MAX_FREQ_NB];
1399
1400 stlink_get_com_freq(h, (emode == STLINK_MODE_DEBUG_JTAG), map);
1401 stlink_dump_speed_map(map, ARRAY_SIZE(map));
1402 stlink_speed(h, initial_interface_speed, false);
1403 }
1404
1405 /* preliminary SRST assert:
1406 * We want SRST is asserted before activating debug signals (mode_enter).
1407 * As the required mode has not been set, the adapter may not know what pin to use.
1408 * Tested firmware STLINK v2 JTAG v29 API v2 SWIM v0 uses T_NRST pin by default
1409 * Tested firmware STLINK v2 JTAG v27 API v2 SWIM v6 uses T_NRST pin by default
1410 * after power on, SWIM_RST stays unchanged */
1411 if (connect_under_reset && emode != STLINK_MODE_DEBUG_SWIM)
1412 stlink_usb_assert_srst(handle, 0);
1413 /* do not check the return status here, we will
1414 proceed and enter the desired mode below
1415 and try asserting srst again. */
1416
1417 res = stlink_usb_mode_enter(handle, emode);
1418 if (res != ERROR_OK)
1419 return res;
1420
1421 /* assert SRST again: a little bit late but now the adapter knows for sure what pin to use */
1422 if (connect_under_reset) {
1423 res = stlink_usb_assert_srst(handle, 0);
1424 if (res != ERROR_OK)
1425 return res;
1426 }
1427
1428 res = stlink_usb_current_mode(handle, &mode);
1429
1430 if (res != ERROR_OK)
1431 return res;
1432
1433 LOG_DEBUG("MODE: 0x%02X", mode);
1434
1435 return ERROR_OK;
1436 }
1437
1438 /* request status from last swim request */
1439 static int stlink_swim_status(void *handle)
1440 {
1441 struct stlink_usb_handle_s *h = handle;
1442 int res;
1443
1444 stlink_usb_init_buffer(handle, h->rx_ep, 4);
1445 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_COMMAND;
1446 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_READSTATUS;
1447 /* error is checked by the caller */
1448 res = stlink_usb_xfer_noerrcheck(handle, h->databuf, 4);
1449 if (res != ERROR_OK)
1450 return res;
1451 return ERROR_OK;
1452 }
1453 /*
1454 the purpose of this function is unknown...
1455 capabilites? anyway for swim v6 it returns
1456 0001020600000000
1457 */
1458 __attribute__((unused))
1459 static int stlink_swim_cap(void *handle, uint8_t *cap)
1460 {
1461 struct stlink_usb_handle_s *h = handle;
1462 int res;
1463
1464 stlink_usb_init_buffer(handle, h->rx_ep, 8);
1465 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_COMMAND;
1466 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_READ_CAP;
1467 h->cmdbuf[h->cmdidx++] = 0x01;
1468 res = stlink_usb_xfer_noerrcheck(handle, h->databuf, 8);
1469 if (res != ERROR_OK)
1470 return res;
1471 memcpy(cap, h->databuf, 8);
1472 return ERROR_OK;
1473 }
1474
1475 /* debug dongle assert/deassert sreset line */
1476 static int stlink_swim_assert_reset(void *handle, int reset)
1477 {
1478 struct stlink_usb_handle_s *h = handle;
1479 int res;
1480
1481 stlink_usb_init_buffer(handle, h->rx_ep, 0);
1482 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_COMMAND;
1483 if (!reset)
1484 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_ASSERT_RESET;
1485 else
1486 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_DEASSERT_RESET;
1487 res = stlink_cmd_allow_retry(handle, h->databuf, 0);
1488 if (res != ERROR_OK)
1489 return res;
1490 return ERROR_OK;
1491 }
1492
1493 /*
1494 send swim enter seq
1495 1.3ms low then 750Hz then 1.5kHz
1496 */
1497 static int stlink_swim_enter(void *handle)
1498 {
1499 struct stlink_usb_handle_s *h = handle;
1500 int res;
1501
1502 stlink_usb_init_buffer(handle, h->rx_ep, 0);
1503 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_COMMAND;
1504 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_ENTER_SEQ;
1505 res = stlink_cmd_allow_retry(handle, h->databuf, 0);
1506 if (res != ERROR_OK)
1507 return res;
1508 return ERROR_OK;
1509 }
1510
1511 /* switch high/low speed swim */
1512 static int stlink_swim_speed(void *handle, int speed)
1513 {
1514 struct stlink_usb_handle_s *h = handle;
1515 int res;
1516
1517 stlink_usb_init_buffer(handle, h->rx_ep, 0);
1518 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_COMMAND;
1519 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_SPEED;
1520 if (speed)
1521 h->cmdbuf[h->cmdidx++] = 1;
1522 else
1523 h->cmdbuf[h->cmdidx++] = 0;
1524 res = stlink_cmd_allow_retry(handle, h->databuf, 0);
1525 if (res != ERROR_OK)
1526 return res;
1527 return ERROR_OK;
1528 }
1529
1530 /*
1531 initiate srst from swim.
1532 nrst is pulled low for 50us.
1533 */
1534 static int stlink_swim_generate_rst(void *handle)
1535 {
1536 struct stlink_usb_handle_s *h = handle;
1537 int res;
1538
1539 stlink_usb_init_buffer(handle, h->rx_ep, 0);
1540 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_COMMAND;
1541 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_GEN_RST;
1542 res = stlink_cmd_allow_retry(handle, h->databuf, 0);
1543 if (res != ERROR_OK)
1544 return res;
1545 return ERROR_OK;
1546 }
1547
1548 /*
1549 send resyncronize sequence
1550 swim is pulled low for 16us
1551 reply is 64 clks low
1552 */
1553 static int stlink_swim_resync(void *handle)
1554 {
1555 struct stlink_usb_handle_s *h = handle;
1556 int res;
1557
1558 stlink_usb_init_buffer(handle, h->rx_ep, 0);
1559 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_COMMAND;
1560 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_RESET;
1561 res = stlink_cmd_allow_retry(handle, h->databuf, 0);
1562 if (res != ERROR_OK)
1563 return res;
1564 return ERROR_OK;
1565 }
1566
1567 static int stlink_swim_writebytes(void *handle, uint32_t addr, uint32_t len, const uint8_t *data)
1568 {
1569 struct stlink_usb_handle_s *h = handle;
1570 int res;
1571 unsigned int i;
1572 unsigned int datalen = 0;
1573 int cmdsize = STLINK_CMD_SIZE_V2;
1574
1575 if (len > STLINK_DATA_SIZE)
1576 return ERROR_FAIL;
1577
1578 if (h->version.stlink == 1)
1579 cmdsize = STLINK_SG_SIZE;
1580
1581 stlink_usb_init_buffer(handle, h->tx_ep, 0);
1582 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_COMMAND;
1583 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_WRITEMEM;
1584 h_u16_to_be(h->cmdbuf+h->cmdidx, len);
1585 h->cmdidx += 2;
1586 h_u32_to_be(h->cmdbuf+h->cmdidx, addr);
1587 h->cmdidx += 4;
1588 for (i = 0; i < len; i++) {
1589 if (h->cmdidx == cmdsize)
1590 h->databuf[datalen++] = *(data++);
1591 else
1592 h->cmdbuf[h->cmdidx++] = *(data++);
1593 }
1594 if (h->version.stlink == 1)
1595 stlink_usb_set_cbw_transfer_datalength(handle, datalen);
1596
1597 res = stlink_cmd_allow_retry(handle, h->databuf, datalen);
1598 if (res != ERROR_OK)
1599 return res;
1600 return ERROR_OK;
1601 }
1602
1603 static int stlink_swim_readbytes(void *handle, uint32_t addr, uint32_t len, uint8_t *data)
1604 {
1605 struct stlink_usb_handle_s *h = handle;
1606 int res;
1607
1608 if (len > STLINK_DATA_SIZE)
1609 return ERROR_FAIL;
1610
1611 stlink_usb_init_buffer(handle, h->rx_ep, 0);
1612 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_COMMAND;
1613 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_READMEM;
1614 h_u16_to_be(h->cmdbuf+h->cmdidx, len);
1615 h->cmdidx += 2;
1616 h_u32_to_be(h->cmdbuf+h->cmdidx, addr);
1617 h->cmdidx += 4;
1618 res = stlink_cmd_allow_retry(handle, h->databuf, 0);
1619 if (res != ERROR_OK)
1620 return res;
1621
1622 stlink_usb_init_buffer(handle, h->rx_ep, len);
1623 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_COMMAND;
1624 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_READBUF;
1625 res = stlink_usb_xfer_noerrcheck(handle, data, len);
1626 if (res != ERROR_OK)
1627 return res;
1628
1629 return ERROR_OK;
1630 }
1631
1632 /** */
1633 static int stlink_usb_idcode(void *handle, uint32_t *idcode)
1634 {
1635 int res, offset;
1636 struct stlink_usb_handle_s *h = handle;
1637
1638 assert(handle != NULL);
1639
1640 /* there is no swim read core id cmd */
1641 if (h->st_mode == STLINK_MODE_DEBUG_SWIM) {
1642 *idcode = 0;
1643 return ERROR_OK;
1644 }
1645
1646 stlink_usb_init_buffer(handle, h->rx_ep, 12);
1647
1648 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1649 if (h->version.jtag_api == STLINK_JTAG_API_V1) {
1650 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_READCOREID;
1651
1652 res = stlink_usb_xfer_noerrcheck(handle, h->databuf, 4);
1653 offset = 0;
1654 } else {
1655 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_READ_IDCODES;
1656
1657 res = stlink_usb_xfer_errcheck(handle, h->databuf, 12);
1658 offset = 4;
1659 }
1660
1661 if (res != ERROR_OK)
1662 return res;
1663
1664 *idcode = le_to_h_u32(h->databuf + offset);
1665
1666 LOG_DEBUG("IDCODE: 0x%08" PRIX32, *idcode);
1667
1668 return ERROR_OK;
1669 }
1670
1671 static int stlink_usb_v2_read_debug_reg(void *handle, uint32_t addr, uint32_t *val)
1672 {
1673 struct stlink_usb_handle_s *h = handle;
1674 int res;
1675
1676 assert(handle != NULL);
1677
1678 stlink_usb_init_buffer(handle, h->rx_ep, 8);
1679
1680 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1681 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_READDEBUGREG;
1682 h_u32_to_le(h->cmdbuf+h->cmdidx, addr);
1683 h->cmdidx += 4;
1684
1685 res = stlink_cmd_allow_retry(handle, h->databuf, 8);
1686 if (res != ERROR_OK)
1687 return res;
1688
1689 *val = le_to_h_u32(h->databuf + 4);
1690 return ERROR_OK;
1691 }
1692
1693 static int stlink_usb_write_debug_reg(void *handle, uint32_t addr, uint32_t val)
1694 {
1695 struct stlink_usb_handle_s *h = handle;
1696
1697 assert(handle != NULL);
1698
1699 stlink_usb_init_buffer(handle, h->rx_ep, 2);
1700
1701 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1702 if (h->version.jtag_api == STLINK_JTAG_API_V1)
1703 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_WRITEDEBUGREG;
1704 else
1705 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_WRITEDEBUGREG;
1706 h_u32_to_le(h->cmdbuf+h->cmdidx, addr);
1707 h->cmdidx += 4;
1708 h_u32_to_le(h->cmdbuf+h->cmdidx, val);
1709 h->cmdidx += 4;
1710
1711 return stlink_cmd_allow_retry(handle, h->databuf, 2);
1712 }
1713
1714 /** */
1715 static int stlink_usb_trace_read(void *handle, uint8_t *buf, size_t *size)
1716 {
1717 struct stlink_usb_handle_s *h = handle;
1718
1719 assert(handle != NULL);
1720
1721 if (h->trace.enabled && (h->version.flags & STLINK_F_HAS_TRACE)) {
1722 int res;
1723
1724 stlink_usb_init_buffer(handle, h->rx_ep, 10);
1725
1726 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1727 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_GET_TRACE_NB;
1728
1729 res = stlink_usb_xfer_noerrcheck(handle, h->databuf, 2);
1730 if (res != ERROR_OK)
1731 return res;
1732
1733 size_t bytes_avail = le_to_h_u16(h->databuf);
1734 *size = bytes_avail < *size ? bytes_avail : *size - 1;
1735
1736 if (*size > 0) {
1737 res = stlink_usb_read_trace(handle, buf, *size);
1738 if (res != ERROR_OK)
1739 return res;
1740 return ERROR_OK;
1741 }
1742 }
1743 *size = 0;
1744 return ERROR_OK;
1745 }
1746
1747 static enum target_state stlink_usb_v2_get_status(void *handle)
1748 {
1749 int result;
1750 uint32_t status;
1751
1752 result = stlink_usb_v2_read_debug_reg(handle, DCB_DHCSR, &status);
1753 if (result != ERROR_OK)
1754 return TARGET_UNKNOWN;
1755
1756 if (status & S_HALT)
1757 return TARGET_HALTED;
1758 else if (status & S_RESET_ST)
1759 return TARGET_RESET;
1760
1761 return TARGET_RUNNING;
1762 }
1763
1764 /** */
1765 static enum target_state stlink_usb_state(void *handle)
1766 {
1767 int res;
1768 struct stlink_usb_handle_s *h = handle;
1769
1770 assert(handle != NULL);
1771
1772 if (h->reconnect_pending) {
1773 LOG_INFO("Previous state query failed, trying to reconnect");
1774 res = stlink_usb_mode_enter(handle, h->st_mode);
1775 if (res != ERROR_OK)
1776 return TARGET_UNKNOWN;
1777
1778 h->reconnect_pending = false;
1779 }
1780
1781 if (h->version.jtag_api != STLINK_JTAG_API_V1) {
1782 res = stlink_usb_v2_get_status(handle);
1783 if (res == TARGET_UNKNOWN)
1784 h->reconnect_pending = true;
1785 return res;
1786 }
1787
1788 stlink_usb_init_buffer(handle, h->rx_ep, 2);
1789
1790 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1791 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_GETSTATUS;
1792
1793 res = stlink_usb_xfer_noerrcheck(handle, h->databuf, 2);
1794
1795 if (res != ERROR_OK)
1796 return TARGET_UNKNOWN;
1797
1798 if (h->databuf[0] == STLINK_CORE_RUNNING)
1799 return TARGET_RUNNING;
1800 if (h->databuf[0] == STLINK_CORE_HALTED)
1801 return TARGET_HALTED;
1802
1803 h->reconnect_pending = true;
1804
1805 return TARGET_UNKNOWN;
1806 }
1807
1808 static int stlink_usb_assert_srst(void *handle, int srst)
1809 {
1810 struct stlink_usb_handle_s *h = handle;
1811
1812 assert(handle != NULL);
1813
1814 if (h->st_mode == STLINK_MODE_DEBUG_SWIM)
1815 return stlink_swim_assert_reset(handle, srst);
1816
1817 if (h->version.stlink == 1)
1818 return ERROR_COMMAND_NOTFOUND;
1819
1820 stlink_usb_init_buffer(handle, h->rx_ep, 2);
1821
1822 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1823 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_DRIVE_NRST;
1824 h->cmdbuf[h->cmdidx++] = srst;
1825
1826 return stlink_cmd_allow_retry(handle, h->databuf, 2);
1827 }
1828
1829 /** */
1830 static void stlink_usb_trace_disable(void *handle)
1831 {
1832 int res = ERROR_OK;
1833 struct stlink_usb_handle_s *h = handle;
1834
1835 assert(handle != NULL);
1836
1837 assert(h->version.flags & STLINK_F_HAS_TRACE);
1838
1839 LOG_DEBUG("Tracing: disable");
1840
1841 stlink_usb_init_buffer(handle, h->rx_ep, 2);
1842 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1843 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_STOP_TRACE_RX;
1844 res = stlink_usb_xfer_errcheck(handle, h->databuf, 2);
1845
1846 if (res == ERROR_OK)
1847 h->trace.enabled = false;
1848 }
1849
1850
1851 /** */
1852 static int stlink_usb_trace_enable(void *handle)
1853 {
1854 int res;
1855 struct stlink_usb_handle_s *h = handle;
1856
1857 assert(handle != NULL);
1858
1859 if (h->version.flags & STLINK_F_HAS_TRACE) {
1860 stlink_usb_init_buffer(handle, h->rx_ep, 10);
1861
1862 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1863 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_START_TRACE_RX;
1864 h_u16_to_le(h->cmdbuf+h->cmdidx, (uint16_t)STLINK_TRACE_SIZE);
1865 h->cmdidx += 2;
1866 h_u32_to_le(h->cmdbuf+h->cmdidx, h->trace.source_hz);
1867 h->cmdidx += 4;
1868
1869 res = stlink_usb_xfer_errcheck(handle, h->databuf, 2);
1870
1871 if (res == ERROR_OK) {
1872 h->trace.enabled = true;
1873 LOG_DEBUG("Tracing: recording at %" PRIu32 "Hz", h->trace.source_hz);
1874 }
1875 } else {
1876 LOG_ERROR("Tracing is not supported by this version.");
1877 res = ERROR_FAIL;
1878 }
1879
1880 return res;
1881 }
1882
1883 /** */
1884 static int stlink_usb_reset(void *handle)
1885 {
1886 struct stlink_usb_handle_s *h = handle;
1887 int retval;
1888
1889 assert(handle != NULL);
1890
1891 stlink_usb_init_buffer(handle, h->rx_ep, 2);
1892
1893 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1894
1895 if (h->version.jtag_api == STLINK_JTAG_API_V1)
1896 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_RESETSYS;
1897 else
1898 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_RESETSYS;
1899
1900 retval = stlink_cmd_allow_retry(handle, h->databuf, 2);
1901 if (retval != ERROR_OK)
1902 return retval;
1903
1904 if (h->trace.enabled) {
1905 stlink_usb_trace_disable(h);
1906 return stlink_usb_trace_enable(h);
1907 }
1908
1909 return ERROR_OK;
1910 }
1911
1912 /** */
1913 static int stlink_usb_run(void *handle)
1914 {
1915 int res;
1916 struct stlink_usb_handle_s *h = handle;
1917
1918 assert(handle != NULL);
1919
1920 if (h->version.jtag_api != STLINK_JTAG_API_V1) {
1921 res = stlink_usb_write_debug_reg(handle, DCB_DHCSR, DBGKEY|C_DEBUGEN);
1922
1923 return res;
1924 }
1925
1926 stlink_usb_init_buffer(handle, h->rx_ep, 2);
1927
1928 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1929 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_RUNCORE;
1930
1931 return stlink_cmd_allow_retry(handle, h->databuf, 2);
1932 }
1933
1934 /** */
1935 static int stlink_usb_halt(void *handle)
1936 {
1937 int res;
1938 struct stlink_usb_handle_s *h = handle;
1939
1940 assert(handle != NULL);
1941
1942 if (h->version.jtag_api != STLINK_JTAG_API_V1) {
1943 res = stlink_usb_write_debug_reg(handle, DCB_DHCSR, DBGKEY|C_HALT|C_DEBUGEN);
1944
1945 return res;
1946 }
1947
1948 stlink_usb_init_buffer(handle, h->rx_ep, 2);
1949
1950 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1951 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_FORCEDEBUG;
1952
1953 return stlink_cmd_allow_retry(handle, h->databuf, 2);
1954 }
1955
1956 /** */
1957 static int stlink_usb_step(void *handle)
1958 {
1959 struct stlink_usb_handle_s *h = handle;
1960
1961 assert(handle != NULL);
1962
1963 if (h->version.jtag_api != STLINK_JTAG_API_V1) {
1964 /* TODO: this emulates the v1 api, it should really use a similar auto mask isr
1965 * that the Cortex-M3 currently does. */
1966 stlink_usb_write_debug_reg(handle, DCB_DHCSR, DBGKEY|C_HALT|C_MASKINTS|C_DEBUGEN);
1967 stlink_usb_write_debug_reg(handle, DCB_DHCSR, DBGKEY|C_STEP|C_MASKINTS|C_DEBUGEN);
1968 return stlink_usb_write_debug_reg(handle, DCB_DHCSR, DBGKEY|C_HALT|C_DEBUGEN);
1969 }
1970
1971 stlink_usb_init_buffer(handle, h->rx_ep, 2);
1972
1973 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1974 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_STEPCORE;
1975
1976 return stlink_cmd_allow_retry(handle, h->databuf, 2);
1977 }
1978
1979 /** */
1980 static int stlink_usb_read_regs(void *handle)
1981 {
1982 int res;
1983 struct stlink_usb_handle_s *h = handle;
1984
1985 assert(handle != NULL);
1986
1987 stlink_usb_init_buffer(handle, h->rx_ep, 88);
1988
1989 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1990 if (h->version.jtag_api == STLINK_JTAG_API_V1) {
1991
1992 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_READALLREGS;
1993 res = stlink_usb_xfer_noerrcheck(handle, h->databuf, 84);
1994 /* regs data from offset 0 */
1995 } else {
1996 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_READALLREGS;
1997 res = stlink_usb_xfer_errcheck(handle, h->databuf, 88);
1998 /* status at offset 0, regs data from offset 4 */
1999 }
2000
2001 return res;
2002 }
2003
2004 /** */
2005 static int stlink_usb_read_reg(void *handle, int num, uint32_t *val)
2006 {
2007 int res;
2008 struct stlink_usb_handle_s *h = handle;
2009
2010 assert(handle != NULL);
2011
2012 stlink_usb_init_buffer(handle, h->rx_ep, h->version.jtag_api == STLINK_JTAG_API_V1 ? 4 : 8);
2013
2014 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
2015 if (h->version.jtag_api == STLINK_JTAG_API_V1)
2016 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_READREG;
2017 else
2018 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_READREG;
2019 h->cmdbuf[h->cmdidx++] = num;
2020
2021 if (h->version.jtag_api == STLINK_JTAG_API_V1) {
2022 res = stlink_usb_xfer_noerrcheck(handle, h->databuf, 4);
2023 if (res != ERROR_OK)
2024 return res;
2025 *val = le_to_h_u32(h->databuf);
2026 return ERROR_OK;
2027 } else {
2028 res = stlink_cmd_allow_retry(handle, h->databuf, 8);
2029 if (res != ERROR_OK)
2030 return res;
2031 *val = le_to_h_u32(h->databuf + 4);
2032 return ERROR_OK;
2033 }
2034 }
2035
2036 /** */
2037 static int stlink_usb_write_reg(void *handle, int num, uint32_t val)
2038 {
2039 struct stlink_usb_handle_s *h = handle;
2040
2041 assert(handle != NULL);
2042
2043 stlink_usb_init_buffer(handle, h->rx_ep, 2);
2044
2045 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
2046 if (h->version.jtag_api == STLINK_JTAG_API_V1)
2047 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_WRITEREG;
2048 else
2049 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_WRITEREG;
2050 h->cmdbuf[h->cmdidx++] = num;
2051 h_u32_to_le(h->cmdbuf+h->cmdidx, val);
2052 h->cmdidx += 4;
2053
2054 return stlink_cmd_allow_retry(handle, h->databuf, 2);
2055 }
2056
2057 static int stlink_usb_get_rw_status(void *handle)
2058 {
2059 struct stlink_usb_handle_s *h = handle;
2060
2061 assert(handle != NULL);
2062
2063 if (h->version.jtag_api == STLINK_JTAG_API_V1)
2064 return ERROR_OK;
2065
2066 stlink_usb_init_buffer(handle, h->rx_ep, 2);
2067
2068 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
2069 if (h->version.flags & STLINK_F_HAS_GETLASTRWSTATUS2) {
2070 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_GETLASTRWSTATUS2;
2071 return stlink_usb_xfer_errcheck(handle, h->databuf, 12);
2072 } else {
2073 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_GETLASTRWSTATUS;
2074 return stlink_usb_xfer_errcheck(handle, h->databuf, 2);
2075 }
2076 }
2077
2078 /** */
2079 static int stlink_usb_read_mem8(void *handle, uint32_t addr, uint16_t len,
2080 uint8_t *buffer)
2081 {
2082 int res;
2083 uint16_t read_len = len;
2084 struct stlink_usb_handle_s *h = handle;
2085
2086 assert(handle != NULL);
2087
2088 /* max 8 bit read/write is 64 bytes or 512 bytes for v3 */
2089 if (len > stlink_usb_block(h)) {
2090 LOG_DEBUG("max buffer (%d) length exceeded", stlink_usb_block(h));
2091 return ERROR_FAIL;
2092 }
2093
2094 stlink_usb_init_buffer(handle, h->rx_ep, read_len);
2095
2096 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
2097 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_READMEM_8BIT;
2098 h_u32_to_le(h->cmdbuf+h->cmdidx, addr);
2099 h->cmdidx += 4;
2100 h_u16_to_le(h->cmdbuf+h->cmdidx, len);
2101 h->cmdidx += 2;
2102
2103 /* we need to fix read length for single bytes */
2104 if (read_len == 1)
2105 read_len++;
2106
2107 res = stlink_usb_xfer_noerrcheck(handle, h->databuf, read_len);
2108
2109 if (res != ERROR_OK)
2110 return res;
2111
2112 memcpy(buffer, h->databuf, len);
2113
2114 return stlink_usb_get_rw_status(handle);
2115 }
2116
2117 /** */
2118 static int stlink_usb_write_mem8(void *handle, uint32_t addr, uint16_t len,
2119 const uint8_t *buffer)
2120 {
2121 int res;
2122 struct stlink_usb_handle_s *h = handle;
2123
2124 assert(handle != NULL);
2125
2126 /* max 8 bit read/write is 64 bytes or 512 bytes for v3 */
2127 if (len > stlink_usb_block(h)) {
2128 LOG_DEBUG("max buffer length (%d) exceeded", stlink_usb_block(h));
2129 return ERROR_FAIL;
2130 }
2131
2132 stlink_usb_init_buffer(handle, h->tx_ep, len);
2133
2134 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
2135 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_WRITEMEM_8BIT;
2136 h_u32_to_le(h->cmdbuf+h->cmdidx, addr);
2137 h->cmdidx += 4;
2138 h_u16_to_le(h->cmdbuf+h->cmdidx, len);
2139 h->cmdidx += 2;
2140
2141 res = stlink_usb_xfer_noerrcheck(handle, buffer, len);
2142
2143 if (res != ERROR_OK)
2144 return res;
2145
2146 return stlink_usb_get_rw_status(handle);
2147 }
2148
2149 /** */
2150 static int stlink_usb_read_mem16(void *handle, uint32_t addr, uint16_t len,
2151 uint8_t *buffer)
2152 {
2153 int res;
2154 struct stlink_usb_handle_s *h = handle;
2155
2156 assert(handle != NULL);
2157
2158 if (!(h->version.flags & STLINK_F_HAS_MEM_16BIT))
2159 return ERROR_COMMAND_NOTFOUND;
2160
2161 /* data must be a multiple of 2 and half-word aligned */
2162 if (len % 2 || addr % 2) {
2163 LOG_DEBUG("Invalid data alignment");
2164 return ERROR_TARGET_UNALIGNED_ACCESS;
2165 }
2166
2167 stlink_usb_init_buffer(handle, h->rx_ep, len);
2168
2169 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
2170 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_READMEM_16BIT;
2171 h_u32_to_le(h->cmdbuf+h->cmdidx, addr);
2172 h->cmdidx += 4;
2173 h_u16_to_le(h->cmdbuf+h->cmdidx, len);
2174 h->cmdidx += 2;
2175
2176 res = stlink_usb_xfer_noerrcheck(handle, h->databuf, len);
2177
2178 if (res != ERROR_OK)
2179 return res;
2180
2181 memcpy(buffer, h->databuf, len);
2182
2183 return stlink_usb_get_rw_status(handle);
2184 }
2185
2186 /** */
2187 static int stlink_usb_write_mem16(void *handle, uint32_t addr, uint16_t len,
2188 const uint8_t *buffer)
2189 {
2190 int res;
2191 struct stlink_usb_handle_s *h = handle;
2192
2193 assert(handle != NULL);
2194
2195 if (!(h->version.flags & STLINK_F_HAS_MEM_16BIT))
2196 return ERROR_COMMAND_NOTFOUND;
2197
2198 /* data must be a multiple of 2 and half-word aligned */
2199 if (len % 2 || addr % 2) {
2200 LOG_DEBUG("Invalid data alignment");
2201 return ERROR_TARGET_UNALIGNED_ACCESS;
2202 }
2203
2204 stlink_usb_init_buffer(handle, h->tx_ep, len);
2205
2206 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
2207 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_WRITEMEM_16BIT;
2208 h_u32_to_le(h->cmdbuf+h->cmdidx, addr);
2209 h->cmdidx += 4;
2210 h_u16_to_le(h->cmdbuf+h->cmdidx, len);
2211 h->cmdidx += 2;
2212
2213 res = stlink_usb_xfer_noerrcheck(handle, buffer, len);
2214
2215 if (res != ERROR_OK)
2216 return res;
2217
2218 return stlink_usb_get_rw_status(handle);
2219 }
2220
2221 /** */
2222 static int stlink_usb_read_mem32(void *handle, uint32_t addr, uint16_t len,
2223 uint8_t *buffer)
2224 {
2225 int res;
2226 struct stlink_usb_handle_s *h = handle;
2227
2228 assert(handle != NULL);
2229
2230 /* data must be a multiple of 4 and word aligned */
2231 if (len % 4 || addr % 4) {
2232 LOG_DEBUG("Invalid data alignment");
2233 return ERROR_TARGET_UNALIGNED_ACCESS;
2234 }
2235
2236 stlink_usb_init_buffer(handle, h->rx_ep, len);
2237
2238 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
2239 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_READMEM_32BIT;
2240 h_u32_to_le(h->cmdbuf+h->cmdidx, addr);
2241 h->cmdidx += 4;
2242 h_u16_to_le(h->cmdbuf+h->cmdidx, len);
2243 h->cmdidx += 2;
2244
2245 res = stlink_usb_xfer_noerrcheck(handle, h->databuf, len);
2246
2247 if (res != ERROR_OK)
2248 return res;
2249
2250 memcpy(buffer, h->databuf, len);
2251
2252 return stlink_usb_get_rw_status(handle);
2253 }
2254
2255 /** */
2256 static int stlink_usb_write_mem32(void *handle, uint32_t addr, uint16_t len,
2257 const uint8_t *buffer)
2258 {
2259 int res;
2260 struct stlink_usb_handle_s *h = handle;
2261
2262 assert(handle != NULL);
2263
2264 /* data must be a multiple of 4 and word aligned */
2265 if (len % 4 || addr % 4) {
2266 LOG_DEBUG("Invalid data alignment");
2267 return ERROR_TARGET_UNALIGNED_ACCESS;
2268 }
2269
2270 stlink_usb_init_buffer(handle, h->tx_ep, len);
2271
2272 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
2273 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_WRITEMEM_32BIT;
2274 h_u32_to_le(h->cmdbuf+h->cmdidx, addr);
2275 h->cmdidx += 4;
2276 h_u16_to_le(h->cmdbuf+h->cmdidx, len);
2277 h->cmdidx += 2;
2278
2279 res = stlink_usb_xfer_noerrcheck(handle, buffer, len);
2280
2281 if (res != ERROR_OK)
2282 return res;
2283
2284 return stlink_usb_get_rw_status(handle);
2285 }
2286
2287 static uint32_t stlink_max_block_size(uint32_t tar_autoincr_block, uint32_t address)
2288 {
2289 uint32_t max_tar_block = (tar_autoincr_block - ((tar_autoincr_block - 1) & address));
2290 if (max_tar_block == 0)
2291 max_tar_block = 4;
2292 return max_tar_block;
2293 }
2294
2295 static int stlink_usb_read_mem(void *handle, uint32_t addr, uint32_t size,
2296 uint32_t count, uint8_t *buffer)
2297 {
2298 int retval = ERROR_OK;
2299 uint32_t bytes_remaining;
2300 int retries = 0;
2301 struct stlink_usb_handle_s *h = handle;
2302
2303 /* calculate byte count */
2304 count *= size;
2305
2306 /* switch to 8 bit if stlink does not support 16 bit memory read */
2307 if (size == 2 && !(h->version.flags & STLINK_F_HAS_MEM_16BIT))
2308 size = 1;
2309
2310 while (count) {
2311
2312 bytes_remaining = (size != 1) ?
2313 stlink_max_block_size(h->max_mem_packet, addr) : stlink_usb_block(h);
2314
2315 if (count < bytes_remaining)
2316 bytes_remaining = count;
2317
2318 /*
2319 * all stlink support 8/32bit memory read/writes and only from
2320 * stlink V2J26 there is support for 16 bit memory read/write.
2321 * Honour 32 bit and, if possible, 16 bit too. Otherwise, handle
2322 * as 8bit access.
2323 */
2324 if (size != 1) {
2325
2326 /* When in jtag mode the stlink uses the auto-increment functionality.
2327 * However it expects us to pass the data correctly, this includes
2328 * alignment and any page boundaries. We already do this as part of the
2329 * adi_v5 implementation, but the stlink is a hla adapter and so this
2330 * needs implementing manually.
2331 * currently this only affects jtag mode, according to ST they do single
2332 * access in SWD mode - but this may change and so we do it for both modes */
2333
2334 /* we first need to check for any unaligned bytes */
2335 if (addr & (size - 1)) {
2336
2337 uint32_t head_bytes = size - (addr & (size - 1));
2338 retval = stlink_usb_read_mem8(handle, addr, head_bytes, buffer);
2339 if (retval == ERROR_WAIT && retries < MAX_WAIT_RETRIES) {
2340 usleep((1<<retries++) * 1000);
2341 continue;
2342 }
2343 if (retval != ERROR_OK)
2344 return retval;
2345 buffer += head_bytes;
2346 addr += head_bytes;
2347 count -= head_bytes;
2348 bytes_remaining -= head_bytes;
2349 }
2350
2351 if (bytes_remaining & (size - 1))
2352 retval = stlink_usb_read_mem(handle, addr, 1, bytes_remaining, buffer);
2353 else if (size == 2)
2354 retval = stlink_usb_read_mem16(handle, addr, bytes_remaining, buffer);
2355 else
2356 retval = stlink_usb_read_mem32(handle, addr, bytes_remaining, buffer);
2357 } else
2358 retval = stlink_usb_read_mem8(handle, addr, bytes_remaining, buffer);
2359
2360 if (retval == ERROR_WAIT && retries < MAX_WAIT_RETRIES) {
2361 usleep((1<<retries++) * 1000);
2362 continue;
2363 }
2364 if (retval != ERROR_OK)
2365 return retval;
2366
2367 buffer += bytes_remaining;
2368 addr += bytes_remaining;
2369 count -= bytes_remaining;
2370 }
2371
2372 return retval;
2373 }
2374
2375 static int stlink_usb_write_mem(void *handle, uint32_t addr, uint32_t size,
2376 uint32_t count, const uint8_t *buffer)
2377 {
2378 int retval = ERROR_OK;
2379 uint32_t bytes_remaining;
2380 int retries = 0;
2381 struct stlink_usb_handle_s *h = handle;
2382
2383 /* calculate byte count */
2384 count *= size;
2385
2386 /* switch to 8 bit if stlink does not support 16 bit memory read */
2387 if (size == 2 && !(h->version.flags & STLINK_F_HAS_MEM_16BIT))
2388 size = 1;
2389
2390 while (count) {
2391
2392 bytes_remaining = (size != 1) ?
2393 stlink_max_block_size(h->max_mem_packet, addr) : stlink_usb_block(h);
2394
2395 if (count < bytes_remaining)
2396 bytes_remaining = count;
2397
2398 /*
2399 * all stlink support 8/32bit memory read/writes and only from
2400 * stlink V2J26 there is support for 16 bit memory read/write.
2401 * Honour 32 bit and, if possible, 16 bit too. Otherwise, handle
2402 * as 8bit access.
2403 */
2404 if (size != 1) {
2405
2406 /* When in jtag mode the stlink uses the auto-increment functionality.
2407 * However it expects us to pass the data correctly, this includes
2408 * alignment and any page boundaries. We already do this as part of the
2409 * adi_v5 implementation, but the stlink is a hla adapter and so this
2410 * needs implementing manually.
2411 * currently this only affects jtag mode, according to ST they do single
2412 * access in SWD mode - but this may change and so we do it for both modes */
2413
2414 /* we first need to check for any unaligned bytes */
2415 if (addr & (size - 1)) {
2416
2417 uint32_t head_bytes = size - (addr & (size - 1));
2418 retval = stlink_usb_write_mem8(handle, addr, head_bytes, buffer);
2419 if (retval == ERROR_WAIT && retries < MAX_WAIT_RETRIES) {
2420 usleep((1<<retries++) * 1000);
2421 continue;
2422 }
2423 if (retval != ERROR_OK)
2424 return retval;
2425 buffer += head_bytes;
2426 addr += head_bytes;
2427 count -= head_bytes;
2428 bytes_remaining -= head_bytes;
2429 }
2430
2431 if (bytes_remaining & (size - 1))
2432 retval = stlink_usb_write_mem(handle, addr, 1, bytes_remaining, buffer);
2433 else if (size == 2)
2434 retval = stlink_usb_write_mem16(handle, addr, bytes_remaining, buffer);
2435 else
2436 retval = stlink_usb_write_mem32(handle, addr, bytes_remaining, buffer);
2437
2438 } else
2439 retval = stlink_usb_write_mem8(handle, addr, bytes_remaining, buffer);
2440 if (retval == ERROR_WAIT && retries < MAX_WAIT_RETRIES) {
2441 usleep((1<<retries++) * 1000);
2442 continue;
2443 }
2444 if (retval != ERROR_OK)
2445 return retval;
2446
2447 buffer += bytes_remaining;
2448 addr += bytes_remaining;
2449 count -= bytes_remaining;
2450 }
2451
2452 return retval;
2453 }
2454
2455 /** */
2456 static int stlink_usb_override_target(const char *targetname)
2457 {
2458 return !strcmp(targetname, "cortex_m");
2459 }
2460
2461 static int stlink_speed_swim(void *handle, int khz, bool query)
2462 {
2463 int retval;
2464
2465 /*
2466 we only have low and high speed...
2467 before changing speed the SWIM_CSR HS bit
2468 must be updated
2469 */
2470 if (!query) {
2471 retval = stlink_swim_speed(handle, (khz < SWIM_FREQ_HIGH) ? 0 : 1);
2472 if (retval != ERROR_OK)
2473 LOG_ERROR("Unable to set adapter speed");
2474 }
2475
2476 return (khz < SWIM_FREQ_HIGH) ? SWIM_FREQ_LOW : SWIM_FREQ_HIGH;
2477 }
2478
2479 static int stlink_match_speed_map(const struct speed_map *map, unsigned int map_size, int khz, bool query)
2480 {
2481 unsigned int i;
2482 int speed_index = -1;
2483 int speed_diff = INT_MAX;
2484 int last_valid_speed = -1;
2485 bool match = true;
2486
2487 for (i = 0; i < map_size; i++) {
2488 if (!map[i].speed)
2489 continue;
2490 last_valid_speed = i;
2491 if (khz == map[i].speed) {
2492 speed_index = i;
2493 break;
2494 } else {
2495 int current_diff = khz - map[i].speed;
2496 /* get abs value for comparison */
2497 current_diff = (current_diff > 0) ? current_diff : -current_diff;
2498 if ((current_diff < speed_diff) && khz >= map[i].speed) {
2499 speed_diff = current_diff;
2500 speed_index = i;
2501 }
2502 }
2503 }
2504
2505 if (speed_index == -1) {
2506 /* this will only be here if we cannot match the slow speed.
2507 * use the slowest speed we support.*/
2508 speed_index = last_valid_speed;
2509 match = false;
2510 } else if (i == map_size)
2511 match = false;
2512
2513 if (!match && query) {
2514 LOG_INFO("Unable to match requested speed %d kHz, using %d kHz",
2515 khz, map[speed_index].speed);
2516 }
2517
2518 return speed_index;
2519 }
2520
2521 static int stlink_speed_swd(void *handle, int khz, bool query)
2522 {
2523 int speed_index;
2524 struct stlink_usb_handle_s *h = handle;
2525
2526 /* old firmware cannot change it */
2527 if (!(h->version.flags & STLINK_F_HAS_SWD_SET_FREQ))
2528 return khz;
2529
2530 speed_index = stlink_match_speed_map(stlink_khz_to_speed_map_swd,
2531 ARRAY_SIZE(stlink_khz_to_speed_map_swd), khz, query);
2532
2533 if (!query) {
2534 int result = stlink_usb_set_swdclk(h, stlink_khz_to_speed_map_swd[speed_index].speed_divisor);
2535 if (result != ERROR_OK) {
2536 LOG_ERROR("Unable to set adapter speed");
2537 return khz;
2538 }
2539 }
2540
2541 return stlink_khz_to_speed_map_swd[speed_index].speed;
2542 }
2543
2544 static int stlink_speed_jtag(void *handle, int khz, bool query)
2545 {
2546 int speed_index;
2547 struct stlink_usb_handle_s *h = handle;
2548
2549 /* old firmware cannot change it */
2550 if (!(h->version.flags & STLINK_F_HAS_JTAG_SET_FREQ))
2551 return khz;
2552
2553 speed_index = stlink_match_speed_map(stlink_khz_to_speed_map_jtag,
2554 ARRAY_SIZE(stlink_khz_to_speed_map_jtag), khz, query);
2555
2556 if (!query) {
2557 int result = stlink_usb_set_jtagclk(h, stlink_khz_to_speed_map_jtag[speed_index].speed_divisor);
2558 if (result != ERROR_OK) {
2559 LOG_ERROR("Unable to set adapter speed");
2560 return khz;
2561 }
2562 }
2563
2564 return stlink_khz_to_speed_map_jtag[speed_index].speed;
2565 }
2566
2567 void stlink_dump_speed_map(const struct speed_map *map, unsigned int map_size)
2568 {
2569 unsigned int i;
2570
2571 LOG_DEBUG("Supported clock speeds are:");
2572 for (i = 0; i < map_size; i++)
2573 if (map[i].speed)
2574 LOG_DEBUG("%d kHz", map[i].speed);
2575 }
2576
2577 static int stlink_get_com_freq(void *handle, bool is_jtag, struct speed_map *map)
2578 {
2579 struct stlink_usb_handle_s *h = handle;
2580 int i;
2581
2582 if (h->version.jtag_api != STLINK_JTAG_API_V3) {
2583 LOG_ERROR("Unknown command");
2584 return 0;
2585 }
2586
2587 stlink_usb_init_buffer(handle, h->rx_ep, 16);
2588
2589 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
2590 h->cmdbuf[h->cmdidx++] = STLINK_APIV3_GET_COM_FREQ;
2591 h->cmdbuf[h->cmdidx++] = is_jtag ? 1 : 0;
2592
2593 int res = stlink_usb_xfer_errcheck(handle, h->databuf, 52);
2594
2595 int size = h->databuf[8];
2596
2597 if (size > STLINK_V3_MAX_FREQ_NB)
2598 size = STLINK_V3_MAX_FREQ_NB;
2599
2600 for (i = 0; i < size; i++) {
2601 map[i].speed = le_to_h_u32(&h->databuf[12 + 4 * i]);
2602 map[i].speed_divisor = i;
2603 }
2604
2605 /* set to zero all the next entries */
2606 for (i = size; i < STLINK_V3_MAX_FREQ_NB; i++)
2607 map[i].speed = 0;
2608
2609 return res;
2610 }
2611
2612 static int stlink_set_com_freq(void *handle, bool is_jtag, unsigned int frequency)
2613 {
2614 struct stlink_usb_handle_s *h = handle;
2615
2616 if (h->version.jtag_api != STLINK_JTAG_API_V3) {
2617 LOG_ERROR("Unknown command");
2618 return 0;
2619 }
2620
2621 stlink_usb_init_buffer(handle, h->rx_ep, 16);
2622
2623 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
2624 h->cmdbuf[h->cmdidx++] = STLINK_APIV3_SET_COM_FREQ;
2625 h->cmdbuf[h->cmdidx++] = is_jtag ? 1 : 0;
2626 h->cmdbuf[h->cmdidx++] = 0;
2627
2628 h_u32_to_le(&h->cmdbuf[4], frequency);
2629
2630 return stlink_usb_xfer_errcheck(handle, h->databuf, 8);
2631 }
2632
2633 static int stlink_speed_v3(void *handle, bool is_jtag, int khz, bool query)
2634 {
2635 struct stlink_usb_handle_s *h = handle;
2636 int speed_index;
2637 struct speed_map map[STLINK_V3_MAX_FREQ_NB];
2638
2639 stlink_get_com_freq(h, is_jtag, map);
2640
2641 speed_index = stlink_match_speed_map(map, ARRAY_SIZE(map), khz, query);
2642
2643 if (!query) {
2644 int result = stlink_set_com_freq(h, is_jtag, map[speed_index].speed);
2645 if (result != ERROR_OK) {
2646 LOG_ERROR("Unable to set adapter speed");
2647 return khz;
2648 }
2649 }
2650 return map[speed_index].speed;
2651 }
2652
2653 static int stlink_speed(void *handle, int khz, bool query)
2654 {
2655 struct stlink_usb_handle_s *h = handle;
2656
2657 if (!handle)
2658 return khz;
2659
2660 switch (h->st_mode) {
2661 case STLINK_MODE_DEBUG_SWIM:
2662 return stlink_speed_swim(handle, khz, query);
2663 case STLINK_MODE_DEBUG_SWD:
2664 if (h->version.jtag_api == STLINK_JTAG_API_V3)
2665 return stlink_speed_v3(handle, false, khz, query);
2666 else
2667 return stlink_speed_swd(handle, khz, query);
2668 break;
2669 case STLINK_MODE_DEBUG_JTAG:
2670 if (h->version.jtag_api == STLINK_JTAG_API_V3)
2671 return stlink_speed_v3(handle, true, khz, query);
2672 else
2673 return stlink_speed_jtag(handle, khz, query);
2674 break;
2675 default:
2676 break;
2677 }
2678
2679 return khz;
2680 }
2681
2682 /** */
2683 static int stlink_usb_close(void *handle)
2684 {
2685 int res;
2686 uint8_t mode;
2687 enum stlink_mode emode;
2688 struct stlink_usb_handle_s *h = handle;
2689
2690 if (h && h->fd)
2691 res = stlink_usb_current_mode(handle, &mode);
2692 else
2693 res = ERROR_FAIL;
2694 /* do not exit if return code != ERROR_OK,
2695 it prevents us from closing jtag_libusb */
2696
2697 if (res == ERROR_OK) {
2698 /* try to exit current mode */
2699 switch (mode) {
2700 case STLINK_DEV_DFU_MODE:
2701 emode = STLINK_MODE_DFU;
2702 break;
2703 case STLINK_DEV_DEBUG_MODE:
2704 emode = STLINK_MODE_DEBUG_SWD;
2705 break;
2706 case STLINK_DEV_SWIM_MODE:
2707 emode = STLINK_MODE_DEBUG_SWIM;
2708 break;
2709 case STLINK_DEV_BOOTLOADER_MODE:
2710 case STLINK_DEV_MASS_MODE:
2711 default:
2712 emode = STLINK_MODE_UNKNOWN;
2713 break;
2714 }
2715
2716 if (emode != STLINK_MODE_UNKNOWN)
2717 stlink_usb_mode_leave(handle, emode);
2718 /* do not check return code, it prevent
2719 us from closing jtag_libusb */
2720 }
2721
2722 if (h && h->fd)
2723 jtag_libusb_close(h->fd);
2724
2725 free(h);
2726
2727 return ERROR_OK;
2728 }
2729
2730 /* Compute ST-Link serial number from the device descriptor
2731 * this function will help to work-around a bug in old ST-Link/V2 DFU
2732 * the buggy DFU returns an incorrect serial in the USB descriptor
2733 * example for the following serial "57FF72067265575742132067"
2734 * - the correct descriptor serial is:
2735 * 0x32, 0x03, 0x35, 0x00, 0x37, 0x00, 0x46, 0x00, 0x46, 0x00, 0x37, 0x00, 0x32, 0x00 ...
2736 * this contains the length (0x32 = 50), the type (0x3 = DT_STRING) and the serial in unicode format
2737 * the serial part is: 0x0035, 0x0037, 0x0046, 0x0046, 0x0037, 0x0032 ... >> 57FF72 ...
2738 * this format could be read correctly by 'libusb_get_string_descriptor_ascii'
2739 * so this case is managed by libusb_helper::string_descriptor_equal
2740 * - the buggy DFU is not doing any unicode conversion and returns a raw serial data in the descriptor
2741 * 0x1a, 0x03, 0x57, 0x00, 0xFF, 0x00, 0x72, 0x00 ...
2742 * >> 57 FF 72 ...
2743 * based on the length (0x1a = 26) we could easily decide if we have to fixup the serial
2744 * and then we have just to convert the raw data into printable characters using sprintf
2745 */
2746 char *stlink_usb_get_alternate_serial(libusb_device_handle *device,
2747 struct libusb_device_descriptor *dev_desc)
2748 {
2749 int usb_retval;
2750 unsigned char desc_serial[(STLINK_SERIAL_LEN + 1) * 2];
2751
2752 if (dev_desc->iSerialNumber == 0)
2753 return NULL;
2754
2755 /* get the LANGID from String Descriptor Zero */
2756 usb_retval = libusb_get_string_descriptor(device, 0, 0, desc_serial,
2757 sizeof(desc_serial));
2758
2759 if (usb_retval < LIBUSB_SUCCESS) {
2760 LOG_ERROR("libusb_get_string_descriptor() failed: %s(%d)",
2761 libusb_error_name(usb_retval), usb_retval);
2762 return NULL;
2763 } else if (usb_retval < 4) {
2764 /* the size should be least 4 bytes to contain a minimum of 1 supported LANGID */
2765 LOG_ERROR("could not get the LANGID");
2766 return NULL;
2767 }
2768
2769 uint32_t langid = desc_serial[2] | (desc_serial[3] << 8);
2770
2771 /* get the serial */
2772 usb_retval = libusb_get_string_descriptor(device, dev_desc->iSerialNumber,
2773 langid, desc_serial, sizeof(desc_serial));
2774
2775 unsigned char len = desc_serial[0];
2776
2777 if (usb_retval < LIBUSB_SUCCESS) {
2778 LOG_ERROR("libusb_get_string_descriptor() failed: %s(%d)",
2779 libusb_error_name(usb_retval), usb_retval);
2780 return NULL;
2781 } else if (desc_serial[1] != LIBUSB_DT_STRING || len > usb_retval) {
2782 LOG_ERROR("invalid string in ST-LINK USB serial descriptor");
2783 return NULL;
2784 }
2785
2786 if (len == ((STLINK_SERIAL_LEN + 1) * 2)) {
2787 /* good ST-Link adapter, this case is managed by
2788 * libusb::libusb_get_string_descriptor_ascii */
2789 return NULL;
2790 } else if (len != ((STLINK_SERIAL_LEN / 2 + 1) * 2)) {
2791 LOG_ERROR("unexpected serial length (%d) in descriptor", len);
2792 return NULL;
2793 }
2794
2795 /* else (len == 26) => buggy ST-Link */
2796
2797 char *alternate_serial = malloc((STLINK_SERIAL_LEN + 1) * sizeof(char));
2798 if (alternate_serial == NULL)
2799 return NULL;
2800
2801 for (unsigned int i = 0; i < STLINK_SERIAL_LEN; i += 2)
2802 sprintf(alternate_serial + i, "%02X", desc_serial[i + 2]);
2803
2804 alternate_serial[STLINK_SERIAL_LEN] = '\0';
2805
2806 return alternate_serial;
2807 }
2808
2809 /** */
2810 static int stlink_usb_open(struct hl_interface_param_s *param, enum stlink_mode mode, void **fd)
2811 {
2812 int err, retry_count = 1;
2813 struct stlink_usb_handle_s *h;
2814
2815 LOG_DEBUG("stlink_usb_open");
2816
2817 h = calloc(1, sizeof(struct stlink_usb_handle_s));
2818
2819 if (h == 0) {
2820 LOG_DEBUG("malloc failed");
2821 return ERROR_FAIL;
2822 }
2823
2824 h->st_mode = mode;
2825
2826 for (unsigned i = 0; param->vid[i]; i++) {
2827 LOG_DEBUG("transport: %d vid: 0x%04x pid: 0x%04x serial: %s",
2828 h->st_mode, param->vid[i], param->pid[i],
2829 param->serial ? param->serial : "");
2830 }
2831
2832 /*
2833 On certain host USB configurations(e.g. MacBook Air)
2834 STLINKv2 dongle seems to have its FW in a funky state if,
2835 after plugging it in, you try to use openocd with it more
2836 then once (by launching and closing openocd). In cases like
2837 that initial attempt to read the FW info via
2838 stlink_usb_version will fail and the device has to be reset
2839 in order to become operational.
2840 */
2841 do {
2842 if (jtag_libusb_open(param->vid, param->pid, param->serial,
2843 &h->fd, stlink_usb_get_alternate_serial) != ERROR_OK) {
2844 LOG_ERROR("open failed");
2845 goto error_open;
2846 }
2847
2848 jtag_libusb_set_configuration(h->fd, 0);
2849
2850 if (libusb_claim_interface(h->fd, 0) != ERROR_OK) {
2851 LOG_DEBUG("claim interface failed");
2852 goto error_open;
2853 }
2854
2855 /* RX EP is common for all versions */
2856 h->rx_ep = STLINK_RX_EP;
2857
2858 uint16_t pid;
2859 if (jtag_libusb_get_pid(libusb_get_device(h->fd), &pid) != ERROR_OK) {
2860 LOG_DEBUG("libusb_get_pid failed");
2861 goto error_open;
2862 }
2863
2864 /* wrap version for first read */
2865 switch (pid) {
2866 case STLINK_V1_PID:
2867 h->version.stlink = 1;
2868 h->tx_ep = STLINK_TX_EP;
2869 break;
2870 case STLINK_V3_USBLOADER_PID:
2871 case STLINK_V3E_PID:
2872 case STLINK_V3S_PID:
2873 case STLINK_V3_2VCP_PID:
2874 h->version.stlink = 3;
2875 h->tx_ep = STLINK_V2_1_TX_EP;
2876 h->trace_ep = STLINK_V2_1_TRACE_EP;
2877 break;
2878 case STLINK_V2_1_PID:
2879 case STLINK_V2_1_NO_MSD_PID:
2880 h->version.stlink = 2;
2881 h->tx_ep = STLINK_V2_1_TX_EP;
2882 h->trace_ep = STLINK_V2_1_TRACE_EP;
2883 break;
2884 default:
2885 /* fall through - we assume V2 to be the default version*/
2886 case STLINK_V2_PID:
2887 h->version.stlink = 2;
2888 h->tx_ep = STLINK_TX_EP;
2889 h->trace_ep = STLINK_TRACE_EP;
2890 break;
2891 }
2892
2893 /* get the device version */
2894 err = stlink_usb_version(h);
2895
2896 if (err == ERROR_OK) {
2897 break;
2898 } else if (h->version.stlink == 1 ||
2899 retry_count == 0) {
2900 LOG_ERROR("read version failed");
2901 goto error_open;
2902 } else {
2903 err = libusb_release_interface(h->fd, 0);
2904 if (err != ERROR_OK) {
2905 LOG_ERROR("release interface failed");
2906 goto error_open;
2907 }
2908
2909 err = libusb_reset_device(h->fd);
2910 if (err != ERROR_OK) {
2911 LOG_ERROR("reset device failed");
2912 goto error_open;
2913 }
2914
2915 jtag_libusb_close(h->fd);
2916 /*
2917 Give the device one second to settle down and
2918 reenumerate.
2919 */
2920 usleep(1 * 1000 * 1000);
2921 retry_count--;
2922 }
2923 } while (1);
2924
2925 /* check if mode is supported */
2926 err = ERROR_OK;
2927
2928 switch (h->st_mode) {
2929 case STLINK_MODE_DEBUG_SWD:
2930 if (h->version.jtag_api == STLINK_JTAG_API_V1)
2931 err = ERROR_FAIL;
2932 /* fall-through */
2933 case STLINK_MODE_DEBUG_JTAG:
2934 if (h->version.jtag == 0)
2935 err = ERROR_FAIL;
2936 break;
2937 case STLINK_MODE_DEBUG_SWIM:
2938 if (h->version.swim == 0)
2939 err = ERROR_FAIL;
2940 break;
2941 default:
2942 err = ERROR_FAIL;
2943 break;
2944 }
2945
2946 if (err != ERROR_OK) {
2947 LOG_ERROR("mode (transport) not supported by device");
2948 goto error_open;
2949 }
2950
2951 /* initialize the debug hardware */
2952 err = stlink_usb_init_mode(h, param->connect_under_reset, param->initial_interface_speed);
2953
2954 if (err != ERROR_OK) {
2955 LOG_ERROR("init mode failed (unable to connect to the target)");
2956 goto error_open;
2957 }
2958
2959 if (h->st_mode == STLINK_MODE_DEBUG_SWIM) {
2960 err = stlink_swim_enter(h);
2961 if (err != ERROR_OK) {
2962 LOG_ERROR("stlink_swim_enter_failed (unable to connect to the target)");
2963 goto error_open;
2964 }
2965 *fd = h;
2966 h->max_mem_packet = STLINK_DATA_SIZE;
2967 return ERROR_OK;
2968 }
2969
2970 /* get cpuid, so we can determine the max page size
2971 * start with a safe default */
2972 h->max_mem_packet = (1 << 10);
2973
2974 uint8_t buffer[4];
2975 err = stlink_usb_read_mem32(h, CPUID, 4, buffer);
2976 if (err == ERROR_OK) {
2977 uint32_t cpuid = le_to_h_u32(buffer);
2978 int i = (cpuid >> 4) & 0xf;
2979 if (i == 4 || i == 3) {
2980 /* Cortex-M3/M4 has 4096 bytes autoincrement range */
2981 h->max_mem_packet = (1 << 12);
2982 }
2983 }
2984
2985 LOG_DEBUG("Using TAR autoincrement: %" PRIu32, h->max_mem_packet);
2986
2987 *fd = h;
2988
2989 return ERROR_OK;
2990
2991 error_open:
2992 stlink_usb_close(h);
2993
2994 return ERROR_FAIL;
2995 }
2996
2997 static int stlink_usb_hl_open(struct hl_interface_param_s *param, void **fd)
2998 {
2999 return stlink_usb_open(param, stlink_get_mode(param->transport), fd);
3000 }
3001
3002 int stlink_config_trace(void *handle, bool enabled,
3003 enum tpiu_pin_protocol pin_protocol, uint32_t port_size,
3004 unsigned int *trace_freq, unsigned int traceclkin_freq,
3005 uint16_t *prescaler)
3006 {
3007 struct stlink_usb_handle_s *h = handle;
3008 uint16_t presc;
3009
3010 if (enabled && (!(h->version.flags & STLINK_F_HAS_TRACE) ||
3011 pin_protocol != TPIU_PIN_PROTOCOL_ASYNC_UART)) {
3012 LOG_ERROR("The attached ST-LINK version doesn't support this trace mode");
3013 return ERROR_FAIL;
3014 }
3015
3016 if (!enabled) {
3017 stlink_usb_trace_disable(h);
3018 return ERROR_OK;
3019 }
3020
3021 if (*trace_freq > STLINK_TRACE_MAX_HZ) {
3022 LOG_ERROR("ST-LINK doesn't support SWO frequency higher than %u",
3023 STLINK_TRACE_MAX_HZ);
3024 return ERROR_FAIL;
3025 }
3026
3027 stlink_usb_trace_disable(h);
3028
3029 if (!*trace_freq)
3030 *trace_freq = STLINK_TRACE_MAX_HZ;
3031
3032 presc = traceclkin_freq / *trace_freq;
3033
3034 if (traceclkin_freq % *trace_freq > 0)
3035 presc++;
3036
3037 if (presc > TPIU_ACPR_MAX_SWOSCALER) {
3038 LOG_ERROR("SWO frequency is not suitable. Please choose a different "
3039 "frequency.");
3040 return ERROR_FAIL;
3041 }
3042
3043 *prescaler = presc;
3044 h->trace.source_hz = *trace_freq;
3045
3046 return stlink_usb_trace_enable(h);
3047 }
3048
3049 /** */
3050 static int stlink_usb_init_access_port(void *handle, unsigned char ap_num)
3051 {
3052 struct stlink_usb_handle_s *h = handle;
3053
3054 assert(handle != NULL);
3055
3056 if (!(h->version.flags & STLINK_F_HAS_AP_INIT))
3057 return ERROR_COMMAND_NOTFOUND;
3058
3059 LOG_DEBUG_IO("init ap_num = %d", ap_num);
3060 stlink_usb_init_buffer(handle, h->rx_ep, 16);
3061 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
3062 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_INIT_AP;
3063 h->cmdbuf[h->cmdidx++] = ap_num;
3064
3065 return stlink_usb_xfer_errcheck(handle, h->databuf, 2);
3066 }
3067
3068 /** */
3069 static int stlink_usb_close_access_port(void *handle, unsigned char ap_num)
3070 {
3071 struct stlink_usb_handle_s *h = handle;
3072
3073 assert(handle != NULL);
3074
3075 if (!(h->version.flags & STLINK_F_HAS_AP_INIT))
3076 return ERROR_COMMAND_NOTFOUND;
3077
3078 LOG_DEBUG_IO("close ap_num = %d", ap_num);
3079 stlink_usb_init_buffer(handle, h->rx_ep, 16);
3080 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
3081 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_CLOSE_AP_DBG;
3082 h->cmdbuf[h->cmdidx++] = ap_num;
3083
3084 /* ignore incorrectly returned error on bogus FW */
3085 if (h->version.flags & STLINK_F_FIX_CLOSE_AP)
3086 return stlink_usb_xfer_errcheck(handle, h->databuf, 2);
3087 else
3088 return stlink_usb_xfer_noerrcheck(handle, h->databuf, 2);
3089
3090 }
3091
3092 /** */
3093 static int stlink_read_dap_register(void *handle, unsigned short dap_port,
3094 unsigned short addr, uint32_t *val)
3095 {
3096 struct stlink_usb_handle_s *h = handle;
3097 int retval;
3098
3099 assert(handle != NULL);
3100
3101 if (!(h->version.flags & STLINK_F_HAS_DAP_REG))
3102 return ERROR_COMMAND_NOTFOUND;
3103
3104 stlink_usb_init_buffer(handle, h->rx_ep, 16);
3105 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
3106 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_READ_DAP_REG;
3107 h_u16_to_le(&h->cmdbuf[2], dap_port);
3108 h_u16_to_le(&h->cmdbuf[4], addr);
3109
3110 retval = stlink_usb_xfer_errcheck(handle, h->databuf, 8);
3111 *val = le_to_h_u32(h->databuf + 4);
3112 LOG_DEBUG_IO("dap_port_read = %d, addr = 0x%x, value = 0x%x", dap_port, addr, *val);
3113 return retval;
3114 }
3115
3116 /** */
3117 static int stlink_write_dap_register(void *handle, unsigned short dap_port,
3118 unsigned short addr, uint32_t val)
3119 {
3120 struct stlink_usb_handle_s *h = handle;
3121
3122 assert(handle != NULL);
3123
3124 if (!(h->version.flags & STLINK_F_HAS_DAP_REG))
3125 return ERROR_COMMAND_NOTFOUND;
3126
3127 LOG_DEBUG_IO("dap_write port = %d, addr = 0x%x, value = 0x%x", dap_port, addr, val);
3128 stlink_usb_init_buffer(handle, h->rx_ep, 16);
3129 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
3130 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_WRITE_DAP_REG;
3131 h_u16_to_le(&h->cmdbuf[2], dap_port);
3132 h_u16_to_le(&h->cmdbuf[4], addr);
3133 h_u32_to_le(&h->cmdbuf[6], val);
3134 return stlink_usb_xfer_errcheck(handle, h->databuf, 2);
3135 }
3136
3137 /** */
3138 struct hl_layout_api_s stlink_usb_layout_api = {
3139 /** */
3140 .open = stlink_usb_hl_open,
3141 /** */
3142 .close = stlink_usb_close,
3143 /** */
3144 .idcode = stlink_usb_idcode,
3145 /** */
3146 .state = stlink_usb_state,
3147 /** */
3148 .reset = stlink_usb_reset,
3149 /** */
3150 .assert_srst = stlink_usb_assert_srst,
3151 /** */
3152 .run = stlink_usb_run,
3153 /** */
3154 .halt = stlink_usb_halt,
3155 /** */
3156 .step = stlink_usb_step,
3157 /** */
3158 .read_regs = stlink_usb_read_regs,
3159 /** */
3160 .read_reg = stlink_usb_read_reg,
3161 /** */
3162 .write_reg = stlink_usb_write_reg,
3163 /** */
3164 .read_mem = stlink_usb_read_mem,
3165 /** */
3166 .write_mem = stlink_usb_write_mem,
3167 /** */
3168 .write_debug_reg = stlink_usb_write_debug_reg,
3169 /** */
3170 .override_target = stlink_usb_override_target,
3171 /** */
3172 .speed = stlink_speed,
3173 /** */
3174 .config_trace = stlink_config_trace,
3175 /** */
3176 .poll_trace = stlink_usb_trace_read,
3177 };
3178
3179 /*****************************************************************************
3180 * DAP direct interface
3181 */
3182
3183 static struct stlink_usb_handle_s *stlink_dap_handle;
3184 static struct hl_interface_param_s stlink_dap_param;
3185 static DECLARE_BITMAP(opened_ap, DP_APSEL_MAX + 1);
3186 static int stlink_dap_error = ERROR_OK;
3187
3188 static int stlink_dap_op_queue_dp_read(struct adiv5_dap *dap, unsigned reg,
3189 uint32_t *data);
3190
3191 /** */
3192 static int stlink_dap_record_error(int error)
3193 {
3194 if (stlink_dap_error == ERROR_OK)
3195 stlink_dap_error = error;
3196 return ERROR_OK;
3197 }
3198
3199 /** */
3200 static int stlink_dap_get_and_clear_error(void)
3201 {
3202 int retval = stlink_dap_error;
3203 stlink_dap_error = ERROR_OK;
3204 return retval;
3205 }
3206
3207 /** */
3208 static int stlink_dap_open_ap(unsigned short apsel)
3209 {
3210 int retval;
3211
3212 /* nothing to do on old versions */
3213 if (!(stlink_dap_handle->version.flags & STLINK_F_HAS_AP_INIT))
3214 return ERROR_OK;
3215
3216 if (apsel > DP_APSEL_MAX)
3217 return ERROR_FAIL;
3218
3219 if (test_bit(apsel, opened_ap))
3220 return ERROR_OK;
3221
3222 retval = stlink_usb_init_access_port(stlink_dap_handle, apsel);
3223 if (retval != ERROR_OK)
3224 return retval;
3225
3226 LOG_DEBUG("AP %d enabled", apsel);
3227 set_bit(apsel, opened_ap);
3228 return ERROR_OK;
3229 }
3230
3231 /** */
3232 static int stlink_dap_closeall_ap(void)
3233 {
3234 int retval, apsel;
3235
3236 /* nothing to do on old versions */
3237 if (!(stlink_dap_handle->version.flags & STLINK_F_HAS_AP_INIT))
3238 return ERROR_OK;
3239
3240 for (apsel = 0; apsel <= DP_APSEL_MAX; apsel++) {
3241 if (!test_bit(apsel, opened_ap))
3242 continue;
3243 retval = stlink_usb_close_access_port(stlink_dap_handle, apsel);
3244 if (retval != ERROR_OK)
3245 return retval;
3246 clear_bit(apsel, opened_ap);
3247 }
3248 return ERROR_OK;
3249 }
3250
3251 /** */
3252 static int stlink_dap_reinit_interface(void)
3253 {
3254 int retval;
3255
3256 /*
3257 * On JTAG only, it should be enough to call stlink_usb_reset(). But on
3258 * some firmware version it does not work as expected, and there is no
3259 * equivalent for SWD.
3260 * At least for now, to reset the interface quit from JTAG/SWD mode then
3261 * select the mode again.
3262 */
3263
3264 if (!stlink_dap_handle->reconnect_pending) {
3265 stlink_dap_handle->reconnect_pending = true;
3266 stlink_usb_mode_leave(stlink_dap_handle, stlink_dap_handle->st_mode);
3267 }
3268
3269 retval = stlink_usb_mode_enter(stlink_dap_handle, stlink_dap_handle->st_mode);
3270 if (retval != ERROR_OK)
3271 return retval;
3272
3273 stlink_dap_handle->reconnect_pending = false;
3274 /* on new FW, calling mode-leave closes all the opened AP; reopen them! */
3275 if (stlink_dap_handle->version.flags & STLINK_F_HAS_AP_INIT)
3276 for (int apsel = 0; apsel <= DP_APSEL_MAX; apsel++)
3277 if (test_bit(apsel, opened_ap)) {
3278 clear_bit(apsel, opened_ap);
3279 stlink_dap_open_ap(apsel);
3280 }
3281 return ERROR_OK;
3282 }
3283
3284 /** */
3285 static int stlink_dap_op_connect(struct adiv5_dap *dap)
3286 {
3287 uint32_t idcode;
3288 int retval;
3289
3290 LOG_INFO("stlink_dap_op_connect(%sconnect)", dap->do_reconnect ? "re" : "");
3291
3292 /* Check if we should reset srst already when connecting, but not if reconnecting. */
3293 if (!dap->do_reconnect) {
3294 enum reset_types jtag_reset_config = jtag_get_reset_config();
3295
3296 if (jtag_reset_config & RESET_CNCT_UNDER_SRST) {
3297 if (jtag_reset_config & RESET_SRST_NO_GATING)
3298 adapter_assert_reset();
3299 else
3300 LOG_WARNING("\'srst_nogate\' reset_config option is required");
3301 }
3302 }
3303
3304 dap->do_reconnect = false;
3305 dap_invalidate_cache(dap);
3306
3307 retval = dap_dp_init(dap);
3308 if (retval != ERROR_OK) {
3309 dap->do_reconnect = true;
3310 return retval;
3311 }
3312
3313 retval = stlink_usb_idcode(stlink_dap_handle, &idcode);
3314 if (retval == ERROR_OK)
3315 LOG_INFO("%s %#8.8" PRIx32,
3316 (stlink_dap_handle->st_mode == STLINK_MODE_DEBUG_JTAG) ? "JTAG IDCODE" : "SWD DPIDR",
3317 idcode);
3318 else
3319 dap->do_reconnect = true;
3320
3321 return retval;
3322 }
3323
3324 /** */
3325 static int stlink_dap_check_reconnect(struct adiv5_dap *dap)
3326 {
3327 int retval;
3328
3329 if (!dap->do_reconnect)
3330 return ERROR_OK;
3331
3332 retval = stlink_dap_reinit_interface();
3333 if (retval != ERROR_OK)
3334 return retval;
3335
3336 return stlink_dap_op_connect(dap);
3337 }
3338
3339 /** */
3340 static int stlink_dap_op_send_sequence(struct adiv5_dap *dap, enum swd_special_seq seq)
3341 {
3342 /* Ignore the request */
3343 return ERROR_OK;
3344 }
3345
3346 /** */
3347 static int stlink_dap_op_queue_dp_read(struct adiv5_dap *dap, unsigned reg,
3348 uint32_t *data)
3349 {
3350 uint32_t dummy;
3351 int retval;
3352
3353 if (!(stlink_dap_handle->version.flags & STLINK_F_HAS_DPBANKSEL))
3354 if (reg & 0x000000F0) {
3355 LOG_ERROR("Banked DP registers not supported in current STLink FW");
3356 return ERROR_COMMAND_NOTFOUND;
3357 }
3358
3359 retval = stlink_dap_check_reconnect(dap);
3360 if (retval != ERROR_OK)
3361 return retval;
3362
3363 data = data ? : &dummy;
3364 if (stlink_dap_handle->version.flags & STLINK_F_QUIRK_JTAG_DP_READ
3365 && stlink_dap_handle->st_mode == STLINK_MODE_DEBUG_JTAG) {
3366 /* Quirk required in JTAG. Read RDBUFF to get the data */
3367 retval = stlink_read_dap_register(stlink_dap_handle,
3368 STLINK_DEBUG_PORT_ACCESS, reg, &dummy);
3369 if (retval == ERROR_OK)
3370 retval = stlink_read_dap_register(stlink_dap_handle,
3371 STLINK_DEBUG_PORT_ACCESS, DP_RDBUFF, data);
3372 } else {
3373 retval = stlink_read_dap_register(stlink_dap_handle,
3374 STLINK_DEBUG_PORT_ACCESS, reg, data);
3375 }
3376
3377 return stlink_dap_record_error(retval);
3378 }
3379
3380 /** */
3381 static int stlink_dap_op_queue_dp_write(struct adiv5_dap *dap, unsigned reg,
3382 uint32_t data)
3383 {
3384 int retval;
3385
3386 if (!(stlink_dap_handle->version.flags & STLINK_F_HAS_DPBANKSEL))
3387 if (reg & 0x000000F0) {
3388 LOG_ERROR("Banked DP registers not supported in current STLink FW");
3389 return ERROR_COMMAND_NOTFOUND;
3390 }
3391
3392 if (reg == DP_SELECT && (data & DP_SELECT_DPBANK) != 0) {
3393 /* ignored if STLINK_F_HAS_DPBANKSEL, not properly managed otherwise */
3394 LOG_DEBUG("Ignoring DPBANKSEL while write SELECT");
3395 data &= ~DP_SELECT_DPBANK;
3396 }
3397
3398 retval = stlink_dap_check_reconnect(dap);
3399 if (retval != ERROR_OK)
3400 return retval;
3401
3402 /* ST-Link does not like that we set CORUNDETECT */
3403 if (reg == DP_CTRL_STAT)
3404 data &= ~CORUNDETECT;
3405
3406 retval = stlink_write_dap_register(stlink_dap_handle,
3407 STLINK_DEBUG_PORT_ACCESS, reg, data);
3408 return stlink_dap_record_error(retval);
3409 }
3410
3411 /** */
3412 static int stlink_dap_op_queue_ap_read(struct adiv5_ap *ap, unsigned reg,
3413 uint32_t *data)
3414 {
3415 struct adiv5_dap *dap = ap->dap;
3416 uint32_t dummy;
3417 int retval;
3418
3419 retval = stlink_dap_check_reconnect(dap);
3420 if (retval != ERROR_OK)
3421 return retval;
3422
3423 if (reg != AP_REG_IDR) {
3424 retval = stlink_dap_open_ap(ap->ap_num);
3425 if (retval != ERROR_OK)
3426 return retval;
3427 }
3428 data = data ? : &dummy;
3429 retval = stlink_read_dap_register(stlink_dap_handle, ap->ap_num, reg,
3430 data);
3431 dap->stlink_flush_ap_write = false;
3432 return stlink_dap_record_error(retval);
3433 }
3434
3435 /** */
3436 static int stlink_dap_op_queue_ap_write(struct adiv5_ap *ap, unsigned reg,
3437 uint32_t data)
3438 {
3439 struct adiv5_dap *dap = ap->dap;
3440 int retval;
3441
3442 retval = stlink_dap_check_reconnect(dap);
3443 if (retval != ERROR_OK)
3444 return retval;
3445
3446 retval = stlink_dap_open_ap(ap->ap_num);
3447 if (retval != ERROR_OK)
3448 return retval;
3449
3450 retval = stlink_write_dap_register(stlink_dap_handle, ap->ap_num, reg,
3451 data);
3452 dap->stlink_flush_ap_write = true;
3453 return stlink_dap_record_error(retval);
3454 }
3455
3456 /** */
3457 static int stlink_dap_op_queue_ap_abort(struct adiv5_dap *dap, uint8_t *ack)
3458 {
3459 LOG_WARNING("stlink_dap_op_queue_ap_abort()");
3460 return ERROR_OK;
3461 }
3462
3463 /** */
3464 static int stlink_dap_op_run(struct adiv5_dap *dap)
3465 {
3466 uint32_t ctrlstat, pwrmask;
3467 int retval, saved_retval;
3468
3469 /* Here no LOG_DEBUG. This is called continuously! */
3470
3471 /*
3472 * ST-Link returns immediately after a DAP write, without waiting for it
3473 * to complete.
3474 * Run a dummy read to DP_RDBUFF, as suggested in
3475 * http://infocenter.arm.com/help/topic/com.arm.doc.faqs/ka16363.html
3476 */
3477 if (dap->stlink_flush_ap_write) {
3478 dap->stlink_flush_ap_write = false;
3479 retval = stlink_dap_op_queue_dp_read(dap, DP_RDBUFF, NULL);
3480 if (retval != ERROR_OK) {
3481 dap->do_reconnect = true;
3482 return retval;
3483 }
3484 }
3485
3486 saved_retval = stlink_dap_get_and_clear_error();
3487
3488 retval = stlink_dap_op_queue_dp_read(dap, DP_CTRL_STAT, &ctrlstat);
3489 if (retval != ERROR_OK) {
3490 dap->do_reconnect = true;
3491 return retval;
3492 }
3493 retval = stlink_dap_get_and_clear_error();
3494 if (retval != ERROR_OK) {
3495 LOG_ERROR("Fail reading CTRL/STAT register. Force reconnect");
3496 dap->do_reconnect = true;
3497 return retval;
3498 }
3499
3500 if (ctrlstat & SSTICKYERR) {
3501 if (stlink_dap_handle->st_mode == STLINK_MODE_DEBUG_JTAG)
3502 retval = stlink_dap_op_queue_dp_write(dap, DP_CTRL_STAT,
3503 ctrlstat & (dap->dp_ctrl_stat | SSTICKYERR));
3504 else
3505 retval = stlink_dap_op_queue_dp_write(dap, DP_ABORT, STKERRCLR);
3506 if (retval != ERROR_OK) {
3507 dap->do_reconnect = true;
3508 return retval;
3509 }
3510 retval = stlink_dap_get_and_clear_error();
3511 if (retval != ERROR_OK) {
3512 dap->do_reconnect = true;
3513 return retval;
3514 }
3515 }
3516
3517 /* check for power lost */
3518 pwrmask = dap->dp_ctrl_stat & (CDBGPWRUPREQ | CSYSPWRUPREQ);
3519 if ((ctrlstat & pwrmask) != pwrmask)
3520 dap->do_reconnect = true;
3521
3522 return saved_retval;
3523 }
3524
3525 /** */
3526 static void stlink_dap_op_quit(struct adiv5_dap *dap)
3527 {
3528 int retval;
3529
3530 retval = stlink_dap_closeall_ap();
3531 if (retval != ERROR_OK)
3532 LOG_ERROR("Error closing APs");
3533 }
3534
3535 static int stlink_swim_op_srst(void)
3536 {
3537 return stlink_swim_generate_rst(stlink_dap_handle);
3538 }
3539
3540 static int stlink_swim_op_read_mem(uint32_t addr, uint32_t size,
3541 uint32_t count, uint8_t *buffer)
3542 {
3543 int retval;
3544 uint32_t bytes_remaining;
3545
3546 LOG_DEBUG_IO("read at 0x%08x len %d*0x%08x", addr, size, count);
3547 count *= size;
3548
3549 while (count) {
3550 bytes_remaining = (count > STLINK_DATA_SIZE) ? STLINK_DATA_SIZE : count;
3551 retval = stlink_swim_readbytes(stlink_dap_handle, addr, bytes_remaining, buffer);
3552 if (retval != ERROR_OK)
3553 return retval;
3554
3555 buffer += bytes_remaining;
3556 addr += bytes_remaining;
3557 count -= bytes_remaining;
3558 }
3559
3560 return ERROR_OK;
3561 }
3562
3563 static int stlink_swim_op_write_mem(uint32_t addr, uint32_t size,
3564 uint32_t count, const uint8_t *buffer)
3565 {
3566 int retval;
3567 uint32_t bytes_remaining;
3568
3569 LOG_DEBUG_IO("write at 0x%08x len %d*0x%08x", addr, size, count);
3570 count *= size;
3571
3572 while (count) {
3573 bytes_remaining = (count > STLINK_DATA_SIZE) ? STLINK_DATA_SIZE : count;
3574 retval = stlink_swim_writebytes(stlink_dap_handle, addr, bytes_remaining, buffer);
3575 if (retval != ERROR_OK)
3576 return retval;
3577
3578 buffer += bytes_remaining;
3579 addr += bytes_remaining;
3580 count -= bytes_remaining;
3581 }
3582
3583 return ERROR_OK;
3584 }
3585
3586 static int stlink_swim_op_reconnect(void)
3587 {
3588 int retval;
3589
3590 retval = stlink_usb_mode_enter(stlink_dap_handle, STLINK_MODE_DEBUG_SWIM);
3591 if (retval != ERROR_OK)
3592 return retval;
3593
3594 return stlink_swim_resync(stlink_dap_handle);
3595 }
3596
3597 static int stlink_dap_config_trace(bool enabled,
3598 enum tpiu_pin_protocol pin_protocol, uint32_t port_size,
3599 unsigned int *trace_freq, unsigned int traceclkin_freq,
3600 uint16_t *prescaler)
3601 {
3602 return stlink_config_trace(stlink_dap_handle, enabled, pin_protocol,
3603 port_size, trace_freq, traceclkin_freq,
3604 prescaler);
3605 }
3606
3607 static int stlink_dap_trace_read(uint8_t *buf, size_t *size)
3608 {
3609 return stlink_usb_trace_read(stlink_dap_handle, buf, size);
3610 }
3611
3612 /** */
3613 COMMAND_HANDLER(stlink_dap_serial_command)
3614 {
3615 LOG_DEBUG("stlink_dap_serial_command");
3616
3617 if (CMD_ARGC != 1) {
3618 LOG_ERROR("Expected exactly one argument for \"st-link serial <serial-number>\".");
3619 return ERROR_COMMAND_SYNTAX_ERROR;
3620 }
3621
3622 if (stlink_dap_param.serial) {
3623 LOG_WARNING("Command \"st-link serial\" already used. Replacing previous value");
3624 free((void *)stlink_dap_param.serial);
3625 }
3626
3627 stlink_dap_param.serial = strdup(CMD_ARGV[0]);
3628 return ERROR_OK;
3629 }
3630
3631 /** */
3632 COMMAND_HANDLER(stlink_dap_vid_pid)
3633 {
3634 unsigned int i, max_usb_ids = HLA_MAX_USB_IDS;
3635
3636 if (CMD_ARGC > max_usb_ids * 2) {
3637 LOG_WARNING("ignoring extra IDs in vid_pid "
3638 "(maximum is %d pairs)", max_usb_ids);
3639 CMD_ARGC = max_usb_ids * 2;
3640 }
3641 if (CMD_ARGC < 2 || (CMD_ARGC & 1)) {
3642 LOG_WARNING("incomplete vid_pid configuration directive");
3643 return ERROR_COMMAND_SYNTAX_ERROR;
3644 }
3645 for (i = 0; i < CMD_ARGC; i += 2) {
3646 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i], stlink_dap_param.vid[i / 2]);
3647 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i + 1], stlink_dap_param.pid[i / 2]);
3648 }
3649
3650 /* null termination */
3651 stlink_dap_param.vid[i / 2] = stlink_dap_param.pid[i / 2] = 0;
3652
3653 return ERROR_OK;
3654 }
3655
3656 /** */
3657 static const struct command_registration stlink_dap_subcommand_handlers[] = {
3658 {
3659 .name = "serial",
3660 .handler = stlink_dap_serial_command,
3661 .mode = COMMAND_CONFIG,
3662 .help = "set the serial number of the adapter",
3663 .usage = "<serial_number>",
3664 },
3665 {
3666 .name = "vid_pid",
3667 .handler = stlink_dap_vid_pid,
3668 .mode = COMMAND_CONFIG,
3669 .help = "USB VID and PID of the adapter",
3670 .usage = "(vid pid)+",
3671 },
3672 COMMAND_REGISTRATION_DONE
3673 };
3674
3675 /** */
3676 static const struct command_registration stlink_dap_command_handlers[] = {
3677 {
3678 .name = "st-link",
3679 .mode = COMMAND_ANY,
3680 .help = "perform st-link management",
3681 .chain = stlink_dap_subcommand_handlers,
3682 .usage = "",
3683 },
3684 COMMAND_REGISTRATION_DONE
3685 };
3686
3687 /** */
3688 static int stlink_dap_init(void)
3689 {
3690 enum reset_types jtag_reset_config = jtag_get_reset_config();
3691 enum stlink_mode mode;
3692 int retval;
3693
3694 LOG_DEBUG("stlink_dap_init()");
3695
3696 if (jtag_reset_config & RESET_CNCT_UNDER_SRST) {
3697 if (jtag_reset_config & RESET_SRST_NO_GATING)
3698 stlink_dap_param.connect_under_reset = true;
3699 else
3700 LOG_WARNING("\'srst_nogate\' reset_config option is required");
3701 }
3702
3703 if (transport_is_dapdirect_swd())
3704 mode = STLINK_MODE_DEBUG_SWD;
3705 else if (transport_is_dapdirect_jtag())
3706 mode = STLINK_MODE_DEBUG_JTAG;
3707 else if (transport_is_swim())
3708 mode = STLINK_MODE_DEBUG_SWIM;
3709 else {
3710 LOG_ERROR("Unsupported transport");
3711 return ERROR_FAIL;
3712 }
3713
3714 retval = stlink_usb_open(&stlink_dap_param, mode, (void **)&stlink_dap_handle);
3715 if (retval != ERROR_OK)
3716 return retval;
3717
3718 if ((mode != STLINK_MODE_DEBUG_SWIM) &&
3719 !(stlink_dap_handle->version.flags & STLINK_F_HAS_DAP_REG)) {
3720 LOG_ERROR("ST-Link version does not support DAP direct transport");
3721 return ERROR_FAIL;
3722 }
3723 return ERROR_OK;
3724 }
3725
3726 /** */
3727 static int stlink_dap_quit(void)
3728 {
3729 LOG_DEBUG("stlink_dap_quit()");
3730
3731 free((void *)stlink_dap_param.serial);
3732 stlink_dap_param.serial = NULL;
3733
3734 return stlink_usb_close(stlink_dap_handle);
3735 }
3736
3737 /** */
3738 static int stlink_dap_reset(int req_trst, int req_srst)
3739 {
3740 LOG_DEBUG("stlink_dap_reset(%d)", req_srst);
3741 return stlink_usb_assert_srst(stlink_dap_handle,
3742 req_srst ? STLINK_DEBUG_APIV2_DRIVE_NRST_LOW
3743 : STLINK_DEBUG_APIV2_DRIVE_NRST_HIGH);
3744 }
3745
3746 /** */
3747 static int stlink_dap_speed(int speed)
3748 {
3749 if (speed == 0) {
3750 LOG_ERROR("RTCK not supported. Set nonzero adapter_khz.");
3751 return ERROR_JTAG_NOT_IMPLEMENTED;
3752 }
3753
3754 stlink_dap_param.initial_interface_speed = speed;
3755 stlink_speed(stlink_dap_handle, speed, false);
3756 return ERROR_OK;
3757 }
3758
3759 /** */
3760 static int stlink_dap_khz(int khz, int *jtag_speed)
3761 {
3762 if (khz == 0) {
3763 LOG_ERROR("RCLK not supported");
3764 return ERROR_FAIL;
3765 }
3766
3767 *jtag_speed = stlink_speed(stlink_dap_handle, khz, true);
3768 return ERROR_OK;
3769 }
3770
3771 /** */
3772 static int stlink_dap_speed_div(int speed, int *khz)
3773 {
3774 *khz = speed;
3775 return ERROR_OK;
3776 }
3777
3778 static const struct dap_ops stlink_dap_ops = {
3779 .connect = stlink_dap_op_connect,
3780 .send_sequence = stlink_dap_op_send_sequence,
3781 .queue_dp_read = stlink_dap_op_queue_dp_read,
3782 .queue_dp_write = stlink_dap_op_queue_dp_write,
3783 .queue_ap_read = stlink_dap_op_queue_ap_read,
3784 .queue_ap_write = stlink_dap_op_queue_ap_write,
3785 .queue_ap_abort = stlink_dap_op_queue_ap_abort,
3786 .run = stlink_dap_op_run,
3787 .sync = NULL, /* optional */
3788 .quit = stlink_dap_op_quit, /* optional */
3789 };
3790
3791 static const struct swim_driver stlink_swim_ops = {
3792 .srst = stlink_swim_op_srst,
3793 .read_mem = stlink_swim_op_read_mem,
3794 .write_mem = stlink_swim_op_write_mem,
3795 .reconnect = stlink_swim_op_reconnect,
3796 };
3797
3798 static const char *const stlink_dap_transport[] = { "dapdirect_swd", "dapdirect_jtag", "swim", NULL };
3799
3800 struct adapter_driver stlink_dap_adapter_driver = {
3801 .name = "st-link",
3802 .transports = stlink_dap_transport,
3803 .commands = stlink_dap_command_handlers,
3804
3805 .init = stlink_dap_init,
3806 .quit = stlink_dap_quit,
3807 .reset = stlink_dap_reset,
3808 .speed = stlink_dap_speed,
3809 .khz = stlink_dap_khz,
3810 .speed_div = stlink_dap_speed_div,
3811 .config_trace = stlink_dap_config_trace,
3812 .poll_trace = stlink_dap_trace_read,
3813
3814 .dap_jtag_ops = &stlink_dap_ops,
3815 .dap_swd_ops = &stlink_dap_ops,
3816 .swim_ops = &stlink_swim_ops,
3817 };

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)