44 #ifdef HAVE_ARPA_INET_H
45 #include <arpa/inet.h>
48 #ifdef HAVE_NETINET_TCP_H
49 #include <netinet/tcp.h>
55 #define USE_LIBUSB_ASYNCIO
58 #define STLINK_SERIAL_LEN 24
60 #define ENDPOINT_IN 0x80
61 #define ENDPOINT_OUT 0x00
63 #define STLINK_WRITE_TIMEOUT (LIBUSB_TIMEOUT_MS)
64 #define STLINK_READ_TIMEOUT (LIBUSB_TIMEOUT_MS)
66 #define STLINK_RX_EP (1|ENDPOINT_IN)
67 #define STLINK_TX_EP (2|ENDPOINT_OUT)
68 #define STLINK_TRACE_EP (3|ENDPOINT_IN)
70 #define STLINK_V2_1_TX_EP (1|ENDPOINT_OUT)
71 #define STLINK_V2_1_TRACE_EP (2|ENDPOINT_IN)
73 #define STLINK_SG_SIZE (31)
74 #define STLINK_DATA_SIZE (6144)
75 #define STLINK_CMD_SIZE_V2 (16)
76 #define STLINK_CMD_SIZE_V1 (10)
78 #define STLINK_V1_PID (0x3744)
79 #define STLINK_V2_PID (0x3748)
80 #define STLINK_V2_1_PID (0x374B)
81 #define STLINK_V2_1_NO_MSD_PID (0x3752)
82 #define STLINK_V3_USBLOADER_PID (0x374D)
83 #define STLINK_V3E_PID (0x374E)
84 #define STLINK_V3S_PID (0x374F)
85 #define STLINK_V3_2VCP_PID (0x3753)
86 #define STLINK_V3E_NO_MSD_PID (0x3754)
98 #define STLINK_MAX_RW8 (64)
99 #define STLINKV3_MAX_RW8 (512)
100 #define STLINK_MAX_RW16_32 STLINK_DATA_SIZE
101 #define STLINK_SWIM_DATA_SIZE STLINK_DATA_SIZE
106 #define MAX_WAIT_RETRIES 8
109 #define STLINK_HLA_AP_NUM 0
110 #define STLINK_HLA_CSW 0
143 struct libusb_device_handle *
fd;
185 #define MAX_QUEUE_DEPTH (4096)
207 #define CMD_MEM_AP_2_SIZE(cmd) ((cmd) & 7)
314 #define STLINK_SWIM_ERR_OK 0x00
315 #define STLINK_SWIM_BUSY 0x01
316 #define STLINK_DEBUG_ERR_OK 0x80
317 #define STLINK_DEBUG_ERR_FAULT 0x81
318 #define STLINK_SWD_AP_WAIT 0x10
319 #define STLINK_SWD_AP_FAULT 0x11
320 #define STLINK_SWD_AP_ERROR 0x12
321 #define STLINK_SWD_AP_PARITY_ERROR 0x13
322 #define STLINK_JTAG_GET_IDCODE_ERROR 0x09
323 #define STLINK_JTAG_WRITE_ERROR 0x0c
324 #define STLINK_JTAG_WRITE_VERIF_ERROR 0x0d
325 #define STLINK_SWD_DP_WAIT 0x14
326 #define STLINK_SWD_DP_FAULT 0x15
327 #define STLINK_SWD_DP_ERROR 0x16
328 #define STLINK_SWD_DP_PARITY_ERROR 0x17
330 #define STLINK_SWD_AP_WDATA_ERROR 0x18
331 #define STLINK_SWD_AP_STICKY_ERROR 0x19
332 #define STLINK_SWD_AP_STICKYORUN_ERROR 0x1a
334 #define STLINK_BAD_AP_ERROR 0x1d
336 #define STLINK_CORE_RUNNING 0x80
337 #define STLINK_CORE_HALTED 0x81
338 #define STLINK_CORE_STAT_UNKNOWN -1
340 #define STLINK_GET_VERSION 0xF1
341 #define STLINK_DEBUG_COMMAND 0xF2
342 #define STLINK_DFU_COMMAND 0xF3
343 #define STLINK_SWIM_COMMAND 0xF4
344 #define STLINK_GET_CURRENT_MODE 0xF5
345 #define STLINK_GET_TARGET_VOLTAGE 0xF7
347 #define STLINK_DEV_DFU_MODE 0x00
348 #define STLINK_DEV_MASS_MODE 0x01
349 #define STLINK_DEV_DEBUG_MODE 0x02
350 #define STLINK_DEV_SWIM_MODE 0x03
351 #define STLINK_DEV_BOOTLOADER_MODE 0x04
352 #define STLINK_DEV_UNKNOWN_MODE -1
354 #define STLINK_DFU_EXIT 0x07
373 #define STLINK_SWIM_ENTER 0x00
374 #define STLINK_SWIM_EXIT 0x01
375 #define STLINK_SWIM_READ_CAP 0x02
376 #define STLINK_SWIM_SPEED 0x03
377 #define STLINK_SWIM_ENTER_SEQ 0x04
378 #define STLINK_SWIM_GEN_RST 0x05
379 #define STLINK_SWIM_RESET 0x06
380 #define STLINK_SWIM_ASSERT_RESET 0x07
381 #define STLINK_SWIM_DEASSERT_RESET 0x08
382 #define STLINK_SWIM_READSTATUS 0x09
383 #define STLINK_SWIM_WRITEMEM 0x0a
384 #define STLINK_SWIM_READMEM 0x0b
385 #define STLINK_SWIM_READBUF 0x0c
387 #define STLINK_DEBUG_GETSTATUS 0x01
388 #define STLINK_DEBUG_FORCEDEBUG 0x02
389 #define STLINK_DEBUG_APIV1_RESETSYS 0x03
390 #define STLINK_DEBUG_APIV1_READALLREGS 0x04
391 #define STLINK_DEBUG_APIV1_READREG 0x05
392 #define STLINK_DEBUG_APIV1_WRITEREG 0x06
393 #define STLINK_DEBUG_READMEM_32BIT 0x07
394 #define STLINK_DEBUG_WRITEMEM_32BIT 0x08
395 #define STLINK_DEBUG_RUNCORE 0x09
396 #define STLINK_DEBUG_STEPCORE 0x0a
397 #define STLINK_DEBUG_APIV1_SETFP 0x0b
398 #define STLINK_DEBUG_READMEM_8BIT 0x0c
399 #define STLINK_DEBUG_WRITEMEM_8BIT 0x0d
400 #define STLINK_DEBUG_APIV1_CLEARFP 0x0e
401 #define STLINK_DEBUG_APIV1_WRITEDEBUGREG 0x0f
402 #define STLINK_DEBUG_APIV1_SETWATCHPOINT 0x10
404 #define STLINK_DEBUG_ENTER_JTAG_RESET 0x00
405 #define STLINK_DEBUG_ENTER_SWD_NO_RESET 0xa3
406 #define STLINK_DEBUG_ENTER_JTAG_NO_RESET 0xa4
408 #define STLINK_DEBUG_APIV1_ENTER 0x20
409 #define STLINK_DEBUG_EXIT 0x21
410 #define STLINK_DEBUG_READCOREID 0x22
412 #define STLINK_DEBUG_APIV2_ENTER 0x30
413 #define STLINK_DEBUG_APIV2_READ_IDCODES 0x31
414 #define STLINK_DEBUG_APIV2_RESETSYS 0x32
415 #define STLINK_DEBUG_APIV2_READREG 0x33
416 #define STLINK_DEBUG_APIV2_WRITEREG 0x34
417 #define STLINK_DEBUG_APIV2_WRITEDEBUGREG 0x35
418 #define STLINK_DEBUG_APIV2_READDEBUGREG 0x36
420 #define STLINK_DEBUG_APIV2_READALLREGS 0x3A
421 #define STLINK_DEBUG_APIV2_GETLASTRWSTATUS 0x3B
422 #define STLINK_DEBUG_APIV2_DRIVE_NRST 0x3C
424 #define STLINK_DEBUG_APIV2_GETLASTRWSTATUS2 0x3E
426 #define STLINK_DEBUG_APIV2_START_TRACE_RX 0x40
427 #define STLINK_DEBUG_APIV2_STOP_TRACE_RX 0x41
428 #define STLINK_DEBUG_APIV2_GET_TRACE_NB 0x42
429 #define STLINK_DEBUG_APIV2_SWD_SET_FREQ 0x43
430 #define STLINK_DEBUG_APIV2_JTAG_SET_FREQ 0x44
431 #define STLINK_DEBUG_APIV2_READ_DAP_REG 0x45
432 #define STLINK_DEBUG_APIV2_WRITE_DAP_REG 0x46
433 #define STLINK_DEBUG_APIV2_READMEM_16BIT 0x47
434 #define STLINK_DEBUG_APIV2_WRITEMEM_16BIT 0x48
436 #define STLINK_DEBUG_APIV2_INIT_AP 0x4B
437 #define STLINK_DEBUG_APIV2_CLOSE_AP_DBG 0x4C
439 #define STLINK_DEBUG_WRITEMEM_32BIT_NO_ADDR_INC 0x50
440 #define STLINK_DEBUG_APIV2_RW_MISC_OUT 0x51
441 #define STLINK_DEBUG_APIV2_RW_MISC_IN 0x52
443 #define STLINK_DEBUG_READMEM_32BIT_NO_ADDR_INC 0x54
445 #define STLINK_APIV3_SET_COM_FREQ 0x61
446 #define STLINK_APIV3_GET_COM_FREQ 0x62
448 #define STLINK_APIV3_GET_VERSION_EX 0xFB
450 #define STLINK_DEBUG_APIV2_DRIVE_NRST_LOW 0x00
451 #define STLINK_DEBUG_APIV2_DRIVE_NRST_HIGH 0x01
452 #define STLINK_DEBUG_APIV2_DRIVE_NRST_PULSE 0x02
454 #define STLINK_DEBUG_PORT_ACCESS 0xffff
456 #define STLINK_TRACE_SIZE 4096
457 #define STLINK_TRACE_MAX_HZ 2000000
458 #define STLINK_V3_TRACE_MAX_HZ 24000000
460 #define STLINK_V3_MAX_FREQ_NB 10
462 #define REQUEST_SENSE 0x03
463 #define REQUEST_SENSE_LENGTH 18
466 #define STLINK_TCP_CMD_REFRESH_DEVICE_LIST 0x00
467 #define STLINK_TCP_CMD_GET_NB_DEV 0x01
468 #define STLINK_TCP_CMD_GET_DEV_INFO 0x02
469 #define STLINK_TCP_CMD_OPEN_DEV 0x03
470 #define STLINK_TCP_CMD_CLOSE_DEV 0x04
471 #define STLINK_TCP_CMD_SEND_USB_CMD 0x05
472 #define STLINK_TCP_CMD_GET_SERVER_VERSION 0x06
473 #define STLINK_TCP_CMD_GET_NB_OF_DEV_CLIENTS 0x07
476 #define OPENOCD_STLINK_TCP_API_VERSION 1
477 #define STLINK_TCP_REQUEST_WRITE 0
478 #define STLINK_TCP_REQUEST_READ 1
479 #define STLINK_TCP_REQUEST_READ_SWO 3
480 #define STLINK_TCP_SS_SIZE 4
481 #define STLINK_TCP_USB_CMD_SIZE 32
482 #define STLINK_TCP_SERIAL_SIZE 32
483 #define STLINK_TCP_SEND_BUFFER_SIZE 10240
484 #define STLINK_TCP_RECV_BUFFER_SIZE 10240
487 #define STLINK_TCP_SS_OK 0x00000001
488 #define STLINK_TCP_SS_MEMORY_PROBLEM 0x00001000
489 #define STLINK_TCP_SS_TIMEOUT 0x00001001
490 #define STLINK_TCP_SS_BAD_PARAMETER 0x00001002
491 #define STLINK_TCP_SS_OPEN_ERR 0x00001003
492 #define STLINK_TCP_SS_TRUNCATED_DATA 0x00001052
493 #define STLINK_TCP_SS_CMD_NOT_AVAILABLE 0x00001053
494 #define STLINK_TCP_SS_TCP_ERROR 0x00002001
495 #define STLINK_TCP_SS_TCP_CANT_CONNECT 0x00002002
496 #define STLINK_TCP_SS_TCP_CLOSE_ERROR 0x00002003
497 #define STLINK_TCP_SS_TCP_BUSY 0x00002004
498 #define STLINK_TCP_SS_WIN32_ERROR 0x00010000
504 #define STLINK_F_HAS_TRACE BIT(0)
505 #define STLINK_F_HAS_GETLASTRWSTATUS2 BIT(1)
506 #define STLINK_F_HAS_SWD_SET_FREQ BIT(2)
507 #define STLINK_F_HAS_JTAG_SET_FREQ BIT(3)
508 #define STLINK_F_QUIRK_JTAG_DP_READ BIT(4)
509 #define STLINK_F_HAS_DAP_REG BIT(5)
510 #define STLINK_F_HAS_MEM_16BIT BIT(6)
511 #define STLINK_F_HAS_AP_INIT BIT(7)
512 #define STLINK_F_FIX_CLOSE_AP BIT(8)
513 #define STLINK_F_HAS_DPBANKSEL BIT(9)
514 #define STLINK_F_HAS_RW8_512BYTES BIT(10)
517 #define STLINK_F_HAS_TARGET_VOLT STLINK_F_HAS_TRACE
518 #define STLINK_F_HAS_FPU_REG STLINK_F_HAS_GETLASTRWSTATUS2
519 #define STLINK_F_HAS_MEM_WR_NO_INC STLINK_F_HAS_MEM_16BIT
520 #define STLINK_F_HAS_MEM_RD_NO_INC STLINK_F_HAS_DPBANKSEL
521 #define STLINK_F_HAS_RW_MISC STLINK_F_HAS_DPBANKSEL
522 #define STLINK_F_HAS_CSW STLINK_F_HAS_DPBANKSEL
524 #define STLINK_REGSEL_IS_FPU(x) ((x) > 0x1F)
562 static int stlink_speed(
void *handle,
int khz,
bool query);
578 #ifdef USE_LIBUSB_ASYNCIO
580 static LIBUSB_CALL void sync_transfer_cb(
struct libusb_transfer *transfer)
582 int *completed = transfer->user_data;
588 static void sync_transfer_wait_for_completion(
struct libusb_transfer *transfer)
590 int r, *completed = transfer->user_data;
592 while (!*completed) {
595 if (r == LIBUSB_ERROR_INTERRUPTED)
597 libusb_cancel_transfer(transfer);
604 static int transfer_error_status(
const struct libusb_transfer *transfer)
608 switch (transfer->status) {
609 case LIBUSB_TRANSFER_COMPLETED:
612 case LIBUSB_TRANSFER_TIMED_OUT:
613 r = LIBUSB_ERROR_TIMEOUT;
615 case LIBUSB_TRANSFER_STALL:
616 r = LIBUSB_ERROR_PIPE;
618 case LIBUSB_TRANSFER_OVERFLOW:
619 r = LIBUSB_ERROR_OVERFLOW;
621 case LIBUSB_TRANSFER_NO_DEVICE:
622 r = LIBUSB_ERROR_NO_DEVICE;
624 case LIBUSB_TRANSFER_ERROR:
625 case LIBUSB_TRANSFER_CANCELLED:
629 r = LIBUSB_ERROR_OTHER;
643 size_t transfer_size;
644 struct libusb_transfer *transfer;
647 static int jtag_libusb_bulk_transfer_n(
648 struct libusb_device_handle *dev_handle,
649 struct jtag_xfer *transfers,
657 for (
size_t i = 0; i < n_transfers; ++i) {
658 transfers[i].retval = 0;
659 transfers[i].completed = 0;
660 transfers[i].transfer_size = 0;
661 transfers[i].transfer = libusb_alloc_transfer(0);
663 if (!transfers[i].transfer) {
664 for (
size_t j = 0; j < i; ++j)
665 libusb_free_transfer(transfers[j].transfer);
667 LOG_DEBUG(
"ERROR, failed to alloc usb transfers");
668 for (
size_t k = 0; k < n_transfers; ++k)
669 transfers[k].retval = LIBUSB_ERROR_NO_MEM;
674 for (
size_t i = 0; i < n_transfers; ++i) {
675 libusb_fill_bulk_transfer(
676 transfers[i].transfer,
678 transfers[i].ep, transfers[i].buf, transfers[i].
size,
679 sync_transfer_cb, &transfers[i].completed,
timeout);
680 transfers[i].transfer->type = LIBUSB_TRANSFER_TYPE_BULK;
682 retval = libusb_submit_transfer(transfers[i].transfer);
684 LOG_DEBUG(
"ERROR, failed to submit transfer %zu, error %d", i, retval);
689 for (
size_t j = i; j < n_transfers; ++j)
690 transfers[j].retval = retval;
699 for (
size_t i = 0; i < n_transfers; ++i) {
700 if (transfers[i].retval == 0) {
701 sync_transfer_wait_for_completion(transfers[i].transfer);
703 retval = transfer_error_status(transfers[i].transfer);
706 transfers[i].retval = retval;
707 LOG_DEBUG(
"ERROR, transfer %zu failed, error %d", i, retval);
711 transfers[i].transfer_size = transfers[i].transfer->actual_length;
715 libusb_free_transfer(transfers[i].transfer);
716 transfers[i].transfer =
NULL;
746 if (t1 != 0x53425355)
760 #ifdef USE_LIBUSB_ASYNCIO
767 size_t n_transfers = 0;
768 struct jtag_xfer transfers[2];
770 memset(transfers, 0,
sizeof(transfers));
772 transfers[0].ep = h->
tx_ep;
773 transfers[0].buf = h->
cmdbuf;
774 transfers[0].size = cmdsize;
779 transfers[1].ep = h->
tx_ep;
780 transfers[1].buf = (uint8_t *)buf;
781 transfers[1].size =
size;
785 transfers[1].ep = h->
rx_ep;
786 transfers[1].buf = (uint8_t *)buf;
787 transfers[1].size =
size;
792 return jtag_libusb_bulk_transfer_n(
808 if (ret || tr != cmdsize)
814 if (ret || tr !=
size) {
821 if (ret || tr !=
size) {
866 if (ret || tr !=
size) {
922 if (sent_size != send_size) {
925 LOG_DEBUG(
"socket send error: %s (errno %d)", strerror(errno), errno);
927 LOG_DEBUG(
"sent size %d (expected %d)", sent_size, send_size);
933 int remaining_bytes = recv_size;
937 while (remaining_bytes > 0) {
939 LOG_DEBUG(
"received size %d (expected %d)", recv_size - remaining_bytes, recv_size);
947 if (received == -1) {
948 LOG_DEBUG(
"socket recv error: %s (errno %d)", strerror(errno), errno);
953 recv_buf += received;
954 remaining_bytes -= received;
958 LOG_ERROR(
"failed to receive USB CMD response");
962 if (check_tcp_status) {
970 LOG_ERROR(
"TCP error status 0x%X", tcp_ss);
1011 LOG_ERROR(
"STLINK_TCP command buffer overflow");
1018 LOG_ERROR(
"STLINK_TCP data buffer overflow");
1086 LOG_DEBUG(
"STLINK_JTAG_GET_IDCODE_ERROR");
1092 LOG_DEBUG(
"Write verify error, ignoring");
1106 LOG_DEBUG(
"STLINK_SWD_AP_PARITY_ERROR");
1115 LOG_DEBUG(
"STLINK_SWD_DP_PARITY_ERROR");
1121 LOG_DEBUG(
"STLINK_SWD_AP_STICKY_ERROR");
1124 LOG_DEBUG(
"STLINK_SWD_AP_STICKYORUN_ERROR");
1180 unsigned int delay_us = (1<<retries++) * 1000;
1181 LOG_DEBUG(
"stlink_cmd_allow_retry ERROR_WAIT, retry %d, delaying %u microseconds", retries,
delay_us);
1217 strcpy((
char *)h->
cmdbuf,
"USBC");
1254 uint8_t v, x, y, jtag, swim, msd, bridge = 0;
1255 char v_str[5 * (1 + 3) + 1];
1281 if ((x <= 22 && y == 7) || (x >= 25 && y >= 7 && y <= 12)) {
1301 if (v == 3 && x == 0 && y == 0) {
1424 p += sprintf(p,
"V%d", v);
1426 p += sprintf(p,
"J%d", jtag);
1428 p += sprintf(p,
"M%d", msd);
1430 p += sprintf(p,
"B%d", bridge);
1432 sprintf(p,
"S%d", swim);
1434 LOG_INFO(
"STLINK %s (API v%d) VID:PID %04X:%04X",
1446 uint32_t adc_results[2];
1465 *target_voltage = 0;
1468 *target_voltage = 2 * ((float)adc_results[1]) * (float)(1.2 / adc_results[0]);
1470 LOG_INFO(
"Target voltage: %f", (
double)*target_voltage);
1710 float target_voltage;
1722 if (target_voltage < 1.5)
1723 LOG_ERROR(
"target voltage may be too low for reliable debugging");
1733 LOG_ERROR(
"selected mode (transport) not supported");
1776 if (connect_under_reset) {
1813 static int stlink_swim_cap(
void *handle, uint8_t *cap)
1926 unsigned int datalen = 0;
1942 for (i = 0; i < len; i++) {
1943 if (h->
cmdidx == cmdsize)
1944 h->
databuf[datalen++] = *(data++);
2020 LOG_DEBUG(
"IDCODE: 0x%08" PRIX32, *idcode);
2127 LOG_INFO(
"Previous state query failed, trying to reconnect");
2230 LOG_ERROR(
"Tracing is not supported by this version.");
2452 uint32_t
addr, uint16_t len, uint8_t *
buffer)
2455 uint16_t read_len = len;
2497 uint32_t
addr, uint16_t len,
const uint8_t *
buffer)
2535 uint32_t
addr, uint16_t len, uint8_t *
buffer)
2554 if (len % 2 ||
addr % 2) {
2583 uint32_t
addr, uint16_t len,
const uint8_t *
buffer)
2602 if (len % 2 ||
addr % 2) {
2629 uint32_t
addr, uint16_t len, uint8_t *
buffer)
2645 if (len % 4 ||
addr % 4) {
2674 uint32_t
addr, uint16_t len,
const uint8_t *
buffer)
2690 if (len % 4 ||
addr % 4) {
2716 uint32_t
addr, uint16_t len, uint8_t *
buffer)
2720 assert(handle !=
NULL);
2731 if (len % 4 ||
addr % 4) {
2758 uint32_t
addr, uint16_t len,
const uint8_t *
buffer)
2762 assert(handle !=
NULL);
2773 if (len % 4 ||
addr % 4) {
2799 uint32_t max_tar_block = (tar_autoincr_block - ((tar_autoincr_block - 1) & address));
2800 if (max_tar_block == 0)
2802 return max_tar_block;
2809 uint32_t bytes_remaining;
2821 bytes_remaining = (
size != 1) ?
2824 if (
count < bytes_remaining)
2825 bytes_remaining =
count;
2847 usleep((1 << retries++) * 1000);
2854 count -= head_bytes;
2855 bytes_remaining -= head_bytes;
2858 if (bytes_remaining & (
size - 1))
2869 usleep((1 << retries++) * 1000);
2875 buffer += bytes_remaining;
2876 addr += bytes_remaining;
2877 count -= bytes_remaining;
2894 uint32_t bytes_remaining;
2907 bytes_remaining = (
size != 1) ?
2910 if (
count < bytes_remaining)
2911 bytes_remaining =
count;
2935 usleep((1<<retries++) * 1000);
2942 count -= head_bytes;
2943 bytes_remaining -= head_bytes;
2946 if (bytes_remaining & (
size - 1))
2956 usleep((1<<retries++) * 1000);
2962 buffer += bytes_remaining;
2963 addr += bytes_remaining;
2964 count -= bytes_remaining;
2980 return !strcmp(targetname,
"cortex_m");
2995 LOG_ERROR(
"Unable to set adapter speed");
3004 int speed_index = -1;
3005 int speed_diff = INT_MAX;
3006 int last_valid_speed = -1;
3009 for (i = 0; i < map_size; i++) {
3012 last_valid_speed = i;
3013 if (khz == map[i].speed) {
3017 int current_diff = khz - map[i].
speed;
3019 current_diff = (current_diff > 0) ? current_diff : -current_diff;
3020 if ((current_diff < speed_diff) && khz >= map[i].
speed) {
3021 speed_diff = current_diff;
3027 if (speed_index == -1) {
3030 speed_index = last_valid_speed;
3032 }
else if (i == map_size)
3035 if (!match && query) {
3036 LOG_INFO(
"Unable to match requested speed %d kHz, using %d kHz",
3037 khz, map[speed_index].speed);
3058 LOG_ERROR(
"Unable to set adapter speed");
3081 LOG_ERROR(
"Unable to set adapter speed");
3093 LOG_DEBUG(
"Supported clock speeds are:");
3094 for (i = 0; i < map_size; i++)
3122 for (i = 0; i <
size; i++) {
3168 LOG_ERROR(
"Unable to set adapter speed");
3172 return map[speed_index].
speed;
3248 LOG_ERROR(
"error closing the socket, errno: %s", strerror(errno));
3288 struct libusb_device_descriptor *dev_desc)
3293 if (dev_desc->iSerialNumber == 0)
3297 usb_retval = libusb_get_string_descriptor(
device, 0, 0, desc_serial,
3298 sizeof(desc_serial));
3300 if (usb_retval < LIBUSB_SUCCESS) {
3301 LOG_ERROR(
"libusb_get_string_descriptor() failed: %s(%d)",
3302 libusb_error_name(usb_retval), usb_retval);
3304 }
else if (usb_retval < 4) {
3310 uint32_t langid = desc_serial[2] | (desc_serial[3] << 8);
3313 usb_retval = libusb_get_string_descriptor(
device, dev_desc->iSerialNumber,
3314 langid, desc_serial,
sizeof(desc_serial));
3316 unsigned char len = desc_serial[0];
3318 if (usb_retval < LIBUSB_SUCCESS) {
3319 LOG_ERROR(
"libusb_get_string_descriptor() failed: %s(%d)",
3320 libusb_error_name(usb_retval), usb_retval);
3322 }
else if (desc_serial[1] != LIBUSB_DT_STRING || len > usb_retval) {
3323 LOG_ERROR(
"invalid string in ST-LINK USB serial descriptor");
3332 LOG_ERROR(
"unexpected serial length (%d) in descriptor", len);
3339 if (!alternate_serial)
3343 sprintf(alternate_serial + i,
"%02X", desc_serial[i + 2]);
3347 return alternate_serial;
3354 int err, retry_count = 1;
3451 usleep(1 * 1000 * 1000);
3467 LOG_ERROR(
"stlink-server does not support SWIM mode");
3491 LOG_ERROR(
"error creating the socket, errno: %s", strerror(errno));
3495 struct sockaddr_in serv;
3496 memset(&serv, 0,
sizeof(
struct sockaddr_in));
3497 serv.sin_family = AF_INET;
3498 serv.sin_port = htons(
param->stlink_tcp_port);
3499 serv.sin_addr.s_addr = inet_addr(
"127.0.0.1");
3504 if (setsockopt(h->
tcp_backend_priv.
fd, IPPROTO_TCP, TCP_NODELAY, (
const void *)&optval,
sizeof(
int)) == -1) {
3505 LOG_ERROR(
"cannot set sock option 'TCP_NODELAY', errno: %s", strerror(errno));
3510 if (setsockopt(h->
tcp_backend_priv.
fd, SOL_SOCKET, SO_RCVBUF, (
const void *)&optval,
sizeof(
int)) == -1) {
3511 LOG_ERROR(
"cannot set sock option 'SO_RCVBUF', errno: %s", strerror(errno));
3516 if (setsockopt(h->
tcp_backend_priv.
fd, SOL_SOCKET, SO_SNDBUF, (
const void *)&optval,
sizeof(
int)) == -1) {
3517 LOG_ERROR(
"cannot set sock option 'SO_SNDBUF', errno: %s", strerror(errno));
3521 if (connect(h->
tcp_backend_priv.
fd, (
const struct sockaddr *)&serv,
sizeof(serv)) == -1) {
3522 LOG_ERROR(
"cannot connect to stlink server, errno: %s", strerror(errno));
3528 LOG_INFO(
"connected to stlink-server");
3536 LOG_ERROR(
"cannot get the stlink-server version");
3544 LOG_INFO(
"stlink-server API v%d, version %d.%d.%d",
3570 if (connected_stlinks == 0) {
3575 LOG_DEBUG(
"%d ST-LINK detected", connected_stlinks);
3577 if (connected_stlinks > 255) {
3578 LOG_WARNING(
"STLink server cannot handle more than 255 ST-LINK connected");
3579 connected_stlinks = 255;
3584 uint8_t stlink_used;
3585 bool stlink_id_matched =
false;
3587 bool stlink_serial_matched = !adapter_serial;
3589 for (uint32_t stlink_id = 0; stlink_id < connected_stlinks; stlink_id++) {
3606 for (
int i = 0;
param->
vid[i]; i++) {
3608 stlink_id_matched =
true;
3613 if (!stlink_id_matched)
3617 if (adapter_serial) {
3626 sprintf(fixed_serial + i,
"%02X", adapter_serial[i / 2]);
3630 stlink_serial_matched = strcmp(fixed_serial,
serial) == 0;
3632 stlink_serial_matched = strcmp(adapter_serial,
serial) == 0;
3636 if (!stlink_serial_matched)
3637 LOG_DEBUG(
"Device serial number '%s' doesn't match requested serial '%s'",
3643 if (!stlink_id_matched) {
3644 LOG_ERROR(
"ST-LINK open failed (vid/pid mismatch)");
3648 if (!stlink_serial_matched) {
3649 LOG_ERROR(
"ST-LINK open failed (serial mismatch)");
3655 LOG_ERROR(
"the selected device is already used");
3704 for (
unsigned i = 0;
param->
vid[i]; i++) {
3705 LOG_DEBUG(
"transport: %d vid: 0x%04x pid: 0x%04x serial: %s",
3710 if (
param->use_stlink_tcp)
3740 LOG_ERROR(
"mode (transport) not supported by device");
3748 LOG_ERROR(
"init mode failed (unable to connect to the target)");
3755 LOG_ERROR(
"stlink_swim_enter_failed (unable to connect to the target)");
3774 int i = (cpuid >> 4) & 0xf;
3775 if (i == 4 || i == 3) {
3800 unsigned int *trace_freq,
unsigned int traceclkin_freq,
3801 uint16_t *prescaler)
3806 LOG_ERROR(
"The attached ST-LINK version doesn't support trace");
3819 LOG_ERROR(
"The attached ST-LINK version doesn't support this trace mode");
3827 if (*trace_freq > max_trace_freq) {
3828 LOG_ERROR(
"ST-LINK doesn't support SWO frequency higher than %u",
3834 *trace_freq = max_trace_freq;
3836 unsigned int presc = (traceclkin_freq + *trace_freq / 2) / *trace_freq;
3838 LOG_ERROR(
"SWO frequency is not suitable. Please choose a different "
3844 unsigned int max_deviation = (traceclkin_freq * 3) / 100;
3845 if (presc * *trace_freq < traceclkin_freq - max_deviation ||
3846 presc * *trace_freq > traceclkin_freq + max_deviation) {
3847 LOG_ERROR(
"SWO frequency is not suitable. Please choose a different "
3907 unsigned int buflen =
ALIGN_UP(items, 4) + 4 * items;
3911 assert(handle !=
NULL);
3928 unsigned int buflen = 2 * 4 * items;
3932 assert(handle !=
NULL);
3953 unsigned short addr, uint32_t *val)
3971 LOG_DEBUG_IO(
"dap_port_read = %d, addr = 0x%x, value = 0x%" PRIx32, dap_port,
addr, *val);
3977 unsigned short addr, uint32_t val)
3986 LOG_DEBUG_IO(
"dap_write port = %d, addr = 0x%x, value = 0x%" PRIx32, dap_port,
addr, val);
4144 for (
unsigned int apsel = 0; apsel <=
DP_APSEL_MAX; apsel++)
4168 LOG_WARNING(
"\'srst_nogate\' reset_config option is required");
4223 if (
reg & 0x000000F0) {
4224 LOG_ERROR(
"Banked DP registers not supported in current STLink FW");
4228 data = data ? data : &
dummy;
4251 if (
reg & 0x000000F0) {
4252 LOG_ERROR(
"Banked DP registers not supported in current STLink FW");
4258 LOG_DEBUG(
"Ignoring DPBANKSEL while write SELECT");
4279 static bool error_flagged;
4281 LOG_ERROR(
"ADIv6 dap not supported by stlink dap-direct mode");
4282 error_flagged =
true;
4291 data = data ? data : &
dummy;
4305 static bool error_flagged;
4307 LOG_ERROR(
"ADIv6 dap not supported by stlink dap-direct mode");
4308 error_flagged =
true;
4329 #define RW_MISC_CMD_ADDRESS 1
4330 #define RW_MISC_CMD_WRITE 2
4331 #define RW_MISC_CMD_READ 3
4332 #define RW_MISC_CMD_APNUM 5
4336 uint8_t buf[2 * 4 * items];
4338 LOG_DEBUG(
"Queue: %u commands in %u items", len, items);
4342 unsigned int val_index =
ALIGN_UP(items, 4);
4343 for (
unsigned int i = 0; i < len; i++) {
4345 ap_num = q[i].
mem_ap.ap->ap_num;
4384 unsigned int err_index = 4 * items;
4385 for (
unsigned int i = 0; i < len; i++) {
4388 LOG_ERROR(
"unknown/unexpected STLINK status code 0x%x", errcode);
4392 ap_num = q[i].
mem_ap.ap->ap_num;
4397 LOG_ERROR(
"unknown/unexpected STLINK status code 0x%x", errcode);
4409 LOG_ERROR(
"unknown/unexpected STLINK status code 0x%x", errcode);
4423 uint8_t buf[bufsize];
4424 uint8_t ap_num = q[0].
mem_ap.ap->ap_num;
4426 uint32_t csw = q[0].
mem_ap.csw;
4434 for (
unsigned int i = 0; i <
count; i++)
4435 buf[i] = q[i].
mem_ap.data >> 8 * (q[i].
mem_ap.addr & 3);
4439 for (
unsigned int i = 0; i <
count; i++)
4444 for (
unsigned int i = 0; i <
count; i++)
4454 for (
unsigned int i = 0; i <
count; i++)
4455 *q[i].
mem_ap.p_data = buf[i] << 8 * (q[i].
mem_ap.addr & 3);
4461 for (
unsigned int i = 0; i <
count; i++)
4471 for (
unsigned int i = 0; i <
count; i++)
4481 #define STLINK_V2_RW_MISC_SIZE (64)
4482 #define STLINK_V3_RW_MISC_SIZE (1227)
4485 unsigned int *pkt_items)
4488 unsigned int i, items = 0;
4503 for (i = 0; i < len; i++) {
4506 unsigned int count = 1;
4509 ap_num = q[i].
mem_ap.ap->ap_num;
4513 if (items +
count > misc_max_items)
4526 unsigned int len_max;
4534 if (len > 1 && incr == 4 && q[0].
mem_ap.addr == q[1].
mem_ap.addr)
4540 for (
unsigned int i = 1; i < len; i++)
4541 if (q[i].
cmd != q[0].
cmd ||
4552 unsigned int count, misc_items = 0;
4558 if (count_misc > count_buf) {
4585 unsigned int skip = 1;
4614 LOG_ERROR(
"ST-Link: Unknown queue command %d", q->
cmd);
4629 uint32_t ctrlstat, pwrmask;
4630 int retval, saved_retval;
4653 LOG_ERROR(
"Fail reading CTRL/STAT register. Force reconnect");
4672 if ((ctrlstat & pwrmask) != pwrmask)
4675 return saved_retval;
4756 !prev_q->
ap_w.changes_csw_default) {
4822 !prev_q->
ap_w.changes_csw_default) {
4858 q->
ap_w.changes_csw_default =
true;
4861 q->
ap_w.changes_csw_default =
false;
4880 uint32_t bytes_remaining;
4891 buffer += bytes_remaining;
4892 addr += bytes_remaining;
4893 count -= bytes_remaining;
4903 uint32_t bytes_remaining;
4914 buffer += bytes_remaining;
4915 addr += bytes_remaining;
4916 count -= bytes_remaining;
4935 unsigned int *trace_freq,
unsigned int traceclkin_freq,
4936 uint16_t *prescaler)
4939 port_size, trace_freq, traceclkin_freq,
4955 "(maximum is %d pairs)", max_usb_ids);
4959 LOG_WARNING(
"incomplete vid_pid configuration directive");
4962 for (i = 0; i <
CMD_ARGC; i += 2) {
4977 bool use_stlink_tcp =
false;
4978 uint16_t stlink_tcp_port = 7184;
4982 else if (strcmp(
CMD_ARGV[0],
"usb") == 0) {
4986 }
else if (strcmp(
CMD_ARGV[0],
"tcp") == 0) {
4987 use_stlink_tcp =
true;
4999 #define BYTES_PER_LINE 16
5002 unsigned int rx_n, tx_n;
5017 for (
unsigned int i = 0; i < tx_n; i++) {
5029 for (
unsigned int i = 0; i < rx_n; i++)
5040 .handler = stlink_dap_vid_pid,
5042 .help =
"USB VID and PID of the adapter",
5043 .usage =
"(vid pid)+",
5047 .handler = &stlink_dap_backend_command,
5049 .help =
"select which ST-Link backend to use",
5050 .usage =
"usb | tcp [port]",
5054 .handler = stlink_dap_cmd_command,
5056 .help =
"send arbitrary command",
5057 .usage =
"rx_n (tx_byte)+",
5067 .help =
"perform st-link management",
5087 LOG_WARNING(
"\'srst_nogate\' reset_config option is required");
5107 LOG_ERROR(
"ST-Link version does not support DAP direct transport");
5124 LOG_DEBUG(
"stlink_dap_reset(%d)", req_srst);
5134 LOG_ERROR(
"RTCK not supported. Set nonzero adapter_khz.");
const char * adapter_get_required_serial(void)
Retrieves the serial number set with command 'adapter serial'.
bool transport_is_dapdirect_swd(void)
Returns true if the current debug session is using SWD as its transport.
bool transport_is_dapdirect_jtag(void)
Returns true if the current debug session is using JTAG as its transport.
static struct aice_port_param_s param
int dap_dp_init(struct adiv5_dap *dap)
Initialize a DAP.
void dap_invalidate_cache(struct adiv5_dap *dap)
Invalidate cached DP select and cached TAR and CSW of all APs.
This defines formats and data structures used to talk to ADIv5 entities.
#define ADIV5_MEM_AP_REG_DRW
#define ADIV5_MEM_AP_REG_BD3
#define CSW_ADDRINC_PACKED
#define ADIV5_MEM_AP_REG_BD2
#define ADIV5_MEM_AP_REG_BD1
#define ADIV5_MEM_AP_REG_CSW
static bool is_adiv6(const struct adiv5_dap *dap)
Check if DAP is ADIv6.
#define ADIV5_MEM_AP_REG_TAR
#define ADIV5_MEM_AP_REG_BD0
@ TPIU_PIN_PROTOCOL_ASYNC_UART
asynchronous output with NRZ coding
static const struct device_t * device
Support functions to access arbitrary bits in a byte array.
static uint32_t buf_get_u32(const uint8_t *_buffer, unsigned first, unsigned num)
Retrieves num bits from _buffer, starting at the first bit, returning the bits in a 32-bit word.
static void buf_set_u32(uint8_t *_buffer, unsigned first, unsigned num, uint32_t value)
Sets num bits in _buffer, starting at the first bit, using the bits in value.
static int test_bit(unsigned int nr, const volatile unsigned long *addr)
test_bit - Determine whether a bit is set
static void set_bit(unsigned int nr, volatile unsigned long *addr)
set_bit - Set a bit in memory
static void clear_bit(unsigned int nr, volatile unsigned long *addr)
clear_bit - Clear a bit in memory
void command_print_sameline(struct command_invocation *cmd, const char *format,...)
#define CMD
Use this macro to access the command being handled, rather than accessing the variable directly.
#define CMD_ARGV
Use this macro to access the arguments for the command being handled, rather than accessing the varia...
#define ERROR_COMMAND_SYNTAX_ERROR
#define ERROR_COMMAND_NOTFOUND
#define CMD_ARGC
Use this macro to access the number of arguments for the command being handled, rather than accessing...
#define COMMAND_PARSE_NUMBER(type, in, out)
parses the string in into out as a type, or prints a command error and passes the error code to the c...
#define COMMAND_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
#define TPIU_ACPR_MAX_SWOSCALER
void delay_us(uint16_t delay)
static uint16_t direction
static enum reset_types jtag_reset_config
enum reset_types jtag_get_reset_config(void)
int adapter_assert_reset(void)
#define ERROR_JTAG_NOT_IMPLEMENTED
int jtag_libusb_bulk_write(struct libusb_device_handle *dev, int ep, char *bytes, int size, int timeout, int *transferred)
int jtag_libusb_handle_events_completed(int *completed)
int jtag_libusb_get_pid(struct libusb_device *dev, uint16_t *pid)
int jtag_libusb_set_configuration(struct libusb_device_handle *devh, int configuration)
void jtag_libusb_close(struct libusb_device_handle *dev)
int jtag_libusb_bulk_read(struct libusb_device_handle *dev, int ep, char *bytes, int size, int timeout, int *transferred)
int jtag_libusb_open(const uint16_t vids[], const uint16_t pids[], struct libusb_device_handle **out, adapter_get_alternate_serial_fn adapter_get_alternate_serial)
#define LOG_DEBUG_IO(expr ...)
#define LOG_WARNING(expr ...)
#define LOG_ERROR(expr ...)
#define ERROR_TIMEOUT_REACHED
#define LOG_INFO(expr ...)
#define LOG_DEBUG(expr ...)
static int close_socket(int sock)
size_t size
Size of the control block search area.
#define STLINK_TCP_RECV_BUFFER_SIZE
#define STLINK_DEV_DEBUG_MODE
#define STLINK_DEBUG_APIV2_WRITEREG
static uint32_t stlink_max_block_size(uint32_t tar_autoincr_block, uint32_t address)
static int stlink_usb_read_trace(void *handle, const uint8_t *buf, int size)
#define STLINK_DEBUG_ENTER_SWD_NO_RESET
static int stlink_swim_writebytes(void *handle, uint32_t addr, uint32_t len, const uint8_t *data)
static int stlink_usb_mode_enter(void *handle, enum stlink_mode type)
static int stlink_usb_trace_enable(void *handle)
#define STLINK_CORE_RUNNING
#define STLINK_F_HAS_JTAG_SET_FREQ
#define STLINK_BAD_AP_ERROR
#define STLINK_READ_TIMEOUT
#define STLINK_SWD_AP_WDATA_ERROR
#define STLINK_DEBUG_WRITEMEM_32BIT_NO_ADDR_INC
static int stlink_usb_halt(void *handle)
#define RW_MISC_CMD_ADDRESS
static int stlink_speed_swd(void *handle, int khz, bool query)
static int stlink_usb_open(void *handle, struct hl_interface_param_s *param)
static int stlink_dap_open_ap(unsigned short apsel)
static int stlink_dap_check_reconnect(struct adiv5_dap *dap)
static int stlink_dap_ap_write(struct adiv5_ap *ap, unsigned int reg, uint32_t data)
#define STLINK_F_HAS_AP_INIT
#define CMD_MEM_AP_2_SIZE(cmd)
static char * stlink_usb_get_alternate_serial(struct libusb_device_handle *device, struct libusb_device_descriptor *dev_desc)
#define STLINK_DEBUG_APIV2_DRIVE_NRST
#define STLINK_TCP_CMD_OPEN_DEV
static int stlink_usb_close(void *handle)
#define RW_MISC_CMD_APNUM
#define STLINK_DEBUG_APIV2_STOP_TRACE_RX
#define STLINK_SWD_DP_ERROR
static const struct command_registration stlink_dap_command_handlers[]
#define STLINK_DEBUG_PORT_ACCESS
#define STLINK_F_FIX_CLOSE_AP
#define STLINK_APIV3_GET_VERSION_EX
static int stlink_swim_resync(void *handle)
static int stlink_usb_init_access_port(void *handle, unsigned char ap_num)
static int stlink_cmd_allow_retry(void *handle, const uint8_t *buf, int size)
Issue an STLINK command via USB transfer, with retries on any wait status responses.
#define STLINK_SWD_AP_ERROR
#define STLINK_SWIM_RESET
#define STLINK_SWD_AP_WAIT
#define STLINK_DEBUG_APIV2_RESETSYS
#define STLINK_F_HAS_DPBANKSEL
static int stlink_dap_dp_write(struct adiv5_dap *dap, unsigned int reg, uint32_t data)
static int stlink_usb_rw_misc_in(void *handle, uint32_t items, uint8_t *buffer)
struct adapter_driver stlink_dap_adapter_driver
#define STLINK_F_HAS_TARGET_VOLT
#define STLINK_CORE_HALTED
#define RW_MISC_CMD_WRITE
#define STLINK_SWIM_DATA_SIZE
static int stlink_dap_op_queue_dp_write(struct adiv5_dap *dap, unsigned int reg, uint32_t data)
static unsigned int stlink_usb_block(void *handle)
static int stlink_dap_op_queue_ap_read(struct adiv5_ap *ap, unsigned int reg, uint32_t *data)
static int stlink_usb_usb_close(void *handle)
#define STLINK_APIV3_GET_COM_FREQ
#define STLINK_DEBUG_APIV2_READ_IDCODES
static int stlink_usb_mode_leave(void *handle, enum stlink_mode type)
static int stlink_speed_jtag(void *handle, int khz, bool query)
static const struct swim_driver stlink_swim_ops
static int stlink_usb_step(void *handle)
#define STLINK_REGSEL_IS_FPU(x)
#define STLINK_F_HAS_MEM_RD_NO_INC
#define STLINK_TCP_REQUEST_WRITE
static int stlink_dap_speed_div(int speed, int *khz)
static int stlink_usb_read_mem8(void *handle, uint8_t ap_num, uint32_t csw, uint32_t addr, uint16_t len, uint8_t *buffer)
static void stlink_usb_set_cbw_transfer_datalength(void *handle, uint32_t size)
struct hl_layout_api_s stlink_usb_layout_api
static int stlink_dap_op_queue_dp_read(struct adiv5_dap *dap, unsigned int reg, uint32_t *data)
#define STLINK_DEBUG_APIV2_READREG
#define STLINK_DEBUG_APIV2_ENTER
static int stlink_usb_usb_xfer_noerrcheck(void *handle, const uint8_t *buf, int size)
#define STLINK_DEBUG_APIV2_SWD_SET_FREQ
#define STLINK_V3_USBLOADER_PID
#define STLINK_V2_RW_MISC_SIZE
#define STLINK_DEBUG_APIV1_RESETSYS
#define STLINK_SWIM_ASSERT_RESET
#define STLINK_WRITE_TIMEOUT
static int stlink_swim_status(void *handle)
#define STLINK_TRACE_SIZE
#define STLINK_DEBUG_APIV2_DRIVE_NRST_HIGH
static struct stlink_usb_handle_s * stlink_dap_handle
static int stlink_usb_read_regs(void *handle)
#define STLINK_TRACE_MAX_HZ
static const char *const stlink_dap_transport[]
static struct stlink_backend_s stlink_tcp_backend
static const struct speed_map stlink_khz_to_speed_map_jtag[]
#define STLINK_TCP_CMD_CLOSE_DEV
static int stlink_swim_op_write_mem(uint32_t addr, uint32_t size, uint32_t count, const uint8_t *buffer)
#define STLINK_V2_1_TRACE_EP
static int stlink_usb_rw_misc_out(void *handle, uint32_t items, const uint8_t *buffer)
#define STLINK_SWIM_ERR_OK
#define STLINK_DEBUG_APIV1_READALLREGS
static int stlink_usb_read_mem16(void *handle, uint8_t ap_num, uint32_t csw, uint32_t addr, uint16_t len, uint8_t *buffer)
#define STLINK_DEBUG_WRITEMEM_8BIT
#define STLINK_JTAG_WRITE_ERROR
static int stlink_usb_close_access_port(void *handle, unsigned char ap_num)
#define STLINK_V2_1_NO_MSD_PID
#define STLINK_TCP_CMD_REFRESH_DEVICE_LIST
#define STLINK_DEV_DFU_MODE
static enum target_state stlink_usb_v2_get_status(void *handle)
static const struct dap_ops stlink_dap_ops
#define STLINK_TCP_CMD_GET_SERVER_VERSION
static int stlink_usb_open_ap(void *handle, unsigned short apsel)
static int stlink_tcp_xfer_noerrcheck(void *handle, const uint8_t *buf, int size)
#define STLINK_HLA_AP_NUM
#define STLINK_F_HAS_DAP_REG
static int stlink_close(void *handle)
static void stlink_dap_op_quit(struct adiv5_dap *dap)
static int stlink_usb_write_mem32_noaddrinc(void *handle, uint8_t ap_num, uint32_t csw, uint32_t addr, uint16_t len, const uint8_t *buffer)
static int stlink_swim_enter(void *handle)
static int stlink_usb_hl_open(struct hl_interface_param_s *param, void **fd)
#define STLINK_SWIM_ENTER_SEQ
static int stlink_usb_error_check(void *handle)
Converts an STLINK status code held in the first byte of a response to an openocd error,...
#define STLINK_DEBUG_APIV2_RW_MISC_IN
COMMAND_HANDLER(stlink_dap_vid_pid)
static int stlink_usb_write_ap_mem(void *handle, uint8_t ap_num, uint32_t csw, uint32_t addr, uint32_t size, uint32_t count, const uint8_t *buffer)
#define STLINK_TCP_REQUEST_READ_SWO
static int stlink_match_speed_map(const struct speed_map *map, unsigned int map_size, int khz, bool query)
static int stlink_set_com_freq(void *handle, bool is_jtag, unsigned int frequency)
static void stlink_dump_speed_map(const struct speed_map *map, unsigned int map_size)
#define STLINK_DEBUG_APIV1_WRITEREG
#define STLINK_F_HAS_TRACE
#define STLINK_SWIM_READBUF
static void stlink_dap_run_internal(struct adiv5_dap *dap)
#define STLINK_F_HAS_MEM_16BIT
#define STLINK_SWD_DP_FAULT
#define STLINK_DEBUG_APIV2_READDEBUGREG
static int stlink_usb_usb_open(void *handle, struct hl_interface_param_s *param)
static int stlink_speed_swim(void *handle, int khz, bool query)
#define STLINK_DEBUG_READCOREID
static int stlink_swim_assert_reset(void *handle, int reset)
static int stlink_swim_op_read_mem(uint32_t addr, uint32_t size, uint32_t count, uint8_t *buffer)
#define STLINK_TCP_CMD_GET_DEV_INFO
#define STLINK_DEBUG_STEPCORE
static int stlink_dap_run_finalize(struct adiv5_dap *dap)
static void stlink_usb_trace_disable(void *handle)
static int stlink_dap_op_queue_ap_write(struct adiv5_ap *ap, unsigned int reg, uint32_t data)
#define STLINK_F_QUIRK_JTAG_DP_READ
static int stlink_usb_write_mem(void *handle, uint32_t addr, uint32_t size, uint32_t count, const uint8_t *buffer)
#define STLINK_V3_2VCP_PID
static int stlink_usb_xfer_errcheck(void *handle, const uint8_t *buf, int size)
static int stlink_usb_read_mem(void *handle, uint32_t addr, uint32_t size, uint32_t count, uint8_t *buffer)
static int stlink_get_com_freq(void *handle, bool is_jtag, struct speed_map *map)
#define STLINK_F_HAS_RW8_512BYTES
static int stlink_usb_trace_read(void *handle, uint8_t *buf, size_t *size)
#define STLINK_SWIM_DEASSERT_RESET
static int stlink_swim_op_reconnect(void)
static int stlink_usb_override_target(const char *targetname)
static const struct command_registration stlink_dap_subcommand_handlers[]
static int stlink_usb_check_voltage(void *handle, float *target_voltage)
#define STLINK_SWD_AP_STICKYORUN_ERROR
static void stlink_usb_xfer_v1_create_cmd(void *handle, uint8_t direction, uint32_t size)
#define STLINK_DEBUG_APIV2_JTAG_SET_FREQ
static enum stlink_mode stlink_get_mode(enum hl_transports t)
#define REQUEST_SENSE_LENGTH
#define STLINK_TCP_SS_TCP_BUSY
#define STLINK_V3_MAX_FREQ_NB
#define STLINK_SWIM_READMEM
#define STLINK_APIV3_SET_COM_FREQ
static int stlink_usb_run(void *handle)
static int stlink_dap_get_and_clear_error(void)
static void stlink_usb_init_buffer(void *handle, uint8_t direction, uint32_t size)
static int stlink_tcp_read_trace(void *handle, const uint8_t *buf, int size)
#define STLINK_TCP_SS_SIZE
static int stlink_usb_v2_read_debug_reg(void *handle, uint32_t addr, uint32_t *val)
#define STLINK_SWIM_READSTATUS
#define STLINK_GET_TARGET_VOLTAGE
static int stlink_dap_init(void)
static int stlink_usb_write_reg(void *handle, unsigned int regsel, uint32_t val)
#define STLINK_TCP_USB_CMD_SIZE
#define STLINK_TCP_SERIAL_SIZE
#define STLINK_DEV_BOOTLOADER_MODE
static int stlink_usb_xfer_v1_get_sense(void *handle)
#define STLINK_DEBUG_EXIT
static int stlink_usb_misc_rw_segment(void *handle, const struct dap_queue *q, unsigned int len, unsigned int items)
static int stlink_swim_readbytes(void *handle, uint32_t addr, uint32_t len, uint8_t *data)
static int stlink_speed(void *handle, int khz, bool query)
#define STLINK_DEBUG_APIV1_READREG
#define STLINK_SWIM_ENTER
#define STLINK_SWD_DP_PARITY_ERROR
static int stlink_usb_xfer_noerrcheck(void *handle, const uint8_t *buf, int size)
static int stlink_usb_read_mem32_noaddrinc(void *handle, uint8_t ap_num, uint32_t csw, uint32_t addr, uint16_t len, uint8_t *buffer)
#define STLINK_DEBUG_WRITEMEM_32BIT
static struct stlink_backend_s stlink_usb_backend
static int stlink_dap_dp_read(struct adiv5_dap *dap, unsigned int reg, uint32_t *data)
static int stlink_usb_version(void *handle)
static int stlink_dap_ap_read(struct adiv5_ap *ap, unsigned int reg, uint32_t *data)
static int stlink_speed_v3(void *handle, bool is_jtag, int khz, bool query)
#define STLINK_JTAG_WRITE_VERIF_ERROR
static int stlink_usb_mem_rw_queue(void *handle, const struct dap_queue *q, unsigned int len, unsigned int *skip)
#define STLINK_SWD_DP_WAIT
#define STLINK_DEBUG_APIV2_WRITE_DAP_REG
#define STLINK_DEBUG_APIV2_WRITEMEM_16BIT
static int stlink_dap_op_queue_run(struct adiv5_dap *dap)
static int stlink_tcp_send_cmd(void *handle, int send_size, int recv_size, bool check_tcp_status)
#define STLINK_DEBUG_APIV2_INIT_AP
#define STLINK_SWIM_GEN_RST
#define STLINK_DEBUG_APIV2_GET_TRACE_NB
static int stlink_usb_set_swdclk(void *handle, uint16_t clk_divisor)
static int stlink_usb_count_buf_rw_queue(const struct dap_queue *q, unsigned int len)
static int stlink_tcp_close(void *handle)
#define STLINK_SWD_AP_FAULT
static int stlink_open(struct hl_interface_param_s *param, enum stlink_mode mode, void **fd)
#define STLINK_DEBUG_READMEM_8BIT
#define STLINK_DEBUG_COMMAND
static int stlink_usb_read_ap_mem(void *handle, uint8_t ap_num, uint32_t csw, uint32_t addr, uint32_t size, uint32_t count, uint8_t *buffer)
#define STLINK_DEBUG_APIV1_ENTER
#define STLINK_DEBUG_ERR_OK
#define STLINK_DFU_COMMAND
#define STLINK_TCP_SEND_BUFFER_SIZE
#define STLINK_DEV_MASS_MODE
#define STLINK_SWD_AP_PARITY_ERROR
#define STLINK_DEBUG_APIV2_READMEM_16BIT
static int stlink_write_dap_register(void *handle, unsigned short dap_port, unsigned short addr, uint32_t val)
#define STLINK_TCP_REQUEST_READ
static int stlink_usb_idcode(void *handle, uint32_t *idcode)
static int stlink_dap_reset(int req_trst, int req_srst)
#define STLINK_F_HAS_GETLASTRWSTATUS2
#define STLINK_F_HAS_MEM_WR_NO_INC
static int stlink_usb_write_mem8(void *handle, uint8_t ap_num, uint32_t csw, uint32_t addr, uint16_t len, const uint8_t *buffer)
static int stlink_usb_write_mem16(void *handle, uint8_t ap_num, uint32_t csw, uint32_t addr, uint16_t len, const uint8_t *buffer)
static int stlink_usb_exit_mode(void *handle)
static int stlink_dap_get_error(void)
#define STLINK_DEBUG_APIV2_CLOSE_AP_DBG
#define STLINK_DEBUG_GETSTATUS
#define STLINK_GET_CURRENT_MODE
#define STLINK_V2_1_TX_EP
#define STLINK_MAX_RW16_32
static int stlink_usb_read_reg(void *handle, unsigned int regsel, uint32_t *val)
static int stlink_dap_op_send_sequence(struct adiv5_dap *dap, enum swd_special_seq seq)
#define STLINK_V3_TRACE_MAX_HZ
static int stlink_usb_reset(void *handle)
#define STLINK_SERIAL_LEN
static int stlink_usb_usb_read_trace(void *handle, const uint8_t *buf, int size)
#define STLINK_DEBUG_APIV2_READ_DAP_REG
#define STLINK_SWIM_SPEED
#define STLINK_DEBUG_APIV2_WRITEDEBUGREG
#define STLINK_DEBUG_READMEM_32BIT_NO_ADDR_INC
static int stlink_swim_generate_rst(void *handle)
#define STLINK_DEBUG_APIV1_WRITEDEBUGREG
#define STLINK_DEBUG_APIV2_START_TRACE_RX
#define STLINK_DEBUG_ENTER_JTAG_NO_RESET
#define STLINK_GET_VERSION
static struct hl_interface_param_s stlink_dap_param
static int stlink_usb_set_jtagclk(void *handle, uint16_t clk_divisor)
static int stlink_usb_count_misc_rw_queue(void *handle, const struct dap_queue *q, unsigned int len, unsigned int *pkt_items)
static int stlink_usb_init_mode(void *handle, bool connect_under_reset, int initial_interface_speed)
static int stlink_dap_speed(int speed)
static int stlink_dap_op_queue_ap_abort(struct adiv5_dap *dap, uint8_t *ack)
static DECLARE_BITMAP(opened_ap, DP_APSEL_MAX+1)
#define STLINK_SWIM_READ_CAP
static int stlink_dap_reinit_interface(void)
#define STLINK_F_HAS_FPU_REG
#define STLINK_CMD_SIZE_V2
static int stlink_dap_record_error(int error)
#define STLINK_DEBUG_APIV2_READALLREGS
static int stlink_usb_read_mem32(void *handle, uint8_t ap_num, uint32_t csw, uint32_t addr, uint16_t len, uint8_t *buffer)
#define OPENOCD_STLINK_TCP_API_VERSION
static const struct speed_map stlink_khz_to_speed_map_swd[]
static int stlink_usb_write_mem32(void *handle, uint8_t ap_num, uint32_t csw, uint32_t addr, uint16_t len, const uint8_t *buffer)
static int stlink_usb_get_rw_status(void *handle)
#define STLINK_DEBUG_APIV2_GETLASTRWSTATUS
static int stlink_usb_write_debug_reg(void *handle, uint32_t addr, uint32_t val)
static int stlink_swim_op_srst(void)
static int stlink_dap_op_connect(struct adiv5_dap *dap)
static int stlink_dap_quit(void)
static int stlink_tcp_open(void *handle, struct hl_interface_param_s *param)
#define STLINK_DEBUG_ERR_FAULT
#define STLINK_DEV_SWIM_MODE
#define STLINK_TCP_CMD_SEND_USB_CMD
#define STLINK_TCP_CMD_GET_NB_DEV
#define STLINK_SWIM_WRITEMEM
#define STLINK_DEBUG_READMEM_32BIT
static int stlink_dap_closeall_ap(void)
static int stlink_config_trace(void *handle, bool enabled, enum tpiu_pin_protocol pin_protocol, uint32_t port_size, unsigned int *trace_freq, unsigned int traceclkin_freq, uint16_t *prescaler)
static int stlink_dap_trace_read(uint8_t *buf, size_t *size)
static uint32_t last_csw_default[DP_APSEL_MAX+1]
#define STLINK_F_HAS_RW_MISC
static int stlink_dap_config_trace(bool enabled, enum tpiu_pin_protocol pin_protocol, uint32_t port_size, unsigned int *trace_freq, unsigned int traceclkin_freq, uint16_t *prescaler)
#define STLINK_SWIM_COMMAND
#define STLINK_DEBUG_APIV2_RW_MISC_OUT
#define STLINK_F_HAS_SWD_SET_FREQ
static int stlink_dap_khz(int khz, int *jtag_speed)
#define STLINK_V3_RW_MISC_SIZE
static int stlink_dap_error
#define STLINK_SWD_AP_STICKY_ERROR
#define STLINK_V3E_NO_MSD_PID
#define STLINK_JTAG_GET_IDCODE_ERROR
static int stlink_usb_xfer_v1_get_status(void *handle)
static enum target_state stlink_usb_state(void *handle)
#define STLINK_DEBUG_APIV2_GETLASTRWSTATUS2
static int stlink_usb_assert_srst(void *handle, int srst)
static int stlink_usb_current_mode(void *handle, uint8_t *mode)
static int stlink_swim_speed(void *handle, int speed)
#define STLINK_DEBUG_FORCEDEBUG
static int stlink_usb_buf_rw_segment(void *handle, const struct dap_queue *q, unsigned int count)
static int stlink_read_dap_register(void *handle, unsigned short dap_port, unsigned short addr, uint32_t *val)
static int stlink_usb_xfer_rw(void *handle, int cmdsize, const uint8_t *buf, int size)
#define STLINK_DEBUG_APIV2_DRIVE_NRST_LOW
#define STLINK_DEBUG_RUNCORE
Represents a driver for a debugging interface.
const char *const name
The name of the interface driver.
This represents an ARM Debug Interface (v5) Access Port (AP).
uint64_t ap_num
ADIv5: Number of this AP (0~255) ADIv6: Base address of this AP (4k aligned) TODO: to be more coheren...
struct adiv5_dap * dap
DAP this AP belongs to.
uint32_t csw_default
Default value for (MEM-AP) AP_REG_CSW register.
target_addr_t tar_value
Cache for (MEM-AP) AP_REG_TAR register value This is written to configure the address being read or w...
uint32_t csw_value
Cache for (MEM-AP) AP_REG_CSW register value.
This represents an ARM Debug Interface (v5) Debug Access Port (DAP).
struct adiv5_ap ap[DP_APSEL_MAX+1]
bool stlink_flush_ap_write
STLINK adapter need to know if last AP operation was read or write, and in case of write has to flush...
bool do_reconnect
Signals that an attempt to reestablish communication afresh should be performed before the next acces...
const char * usage
a string listing the options and arguments, required or optional
Transport-neutral representation of queued DAP transactions, supporting both JTAG and SWD transports.
int(* connect)(struct adiv5_dap *dap)
connect operation for SWD
struct dap_queue::@29::mem_ap mem_ap
struct dap_queue::@29::dp_r dp_r
struct dap_queue::@29::ap_r ap_r
struct dap_queue::@29::ap_w ap_w
struct dap_queue::@29::dp_w dp_w
uint16_t pid[HLA_MAX_USB_IDS+1]
List of recognised PIDs.
int initial_interface_speed
Initial interface clock clock speed.
uint16_t vid[HLA_MAX_USB_IDS+1]
List of recognised VIDs.
int(* open)(struct hl_interface_param_s *param, void **handle)
int(* read_trace)(void *handle, const uint8_t *buf, int size)
int(* xfer_noerrcheck)(void *handle, const uint8_t *buf, int size)
int(* close)(void *handle)
int(* open)(void *handle, struct hl_interface_param_s *param)
struct stlink_tcp_version version
unsigned int queue_index
first element available in the queue
bool enabled
whether SWO tracing is enabled or not
struct stlink_usb_priv_s usb_backend_priv
struct stlink_backend_s * backend
bool reconnect_pending
reconnect is needed next time we try to query the status
struct stlink_usb_version version
uint32_t source_hz
trace module source clock
struct dap_queue queue[MAX_QUEUE_DEPTH]
queue of dap_direct operations
struct stlink_tcp_priv_s tcp_backend_priv
struct stlink_usb_handle_s::@35 trace
struct libusb_device_handle * fd
struct libusb_transfer * trans
enum stlink_jtag_api_version jtag_api
jtag api version supported
uint32_t flags
one bit for each feature supported.
int(* srst)(void)
Send SRST (system reset) command to target.
bool transport_is_swim(void)
This file implements support for STMicroelectronics debug protocol SWIM (Single Wire Interface Module...
#define ERROR_TARGET_UNALIGNED_ACCESS
static void h_u32_to_be(uint8_t *buf, uint32_t val)
static void h_u16_to_be(uint8_t *buf, uint16_t val)
static uint16_t le_to_h_u16(const uint8_t *buf)
static void h_u32_to_le(uint8_t *buf, uint32_t val)
#define ARRAY_SIZE(x)
Compute the number of elements of a variable length array.
static void h_u24_to_le(uint8_t *buf, unsigned int val)
static uint16_t be_to_h_u16(const uint8_t *buf)
static void h_u16_to_le(uint8_t *buf, uint16_t val)
static uint32_t le_to_h_u32(const uint8_t *buf)