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