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)
87 #define STLINK_V3P_USBLOADER_PID (0x3755)
88 #define STLINK_V3P_PID (0x3757)
100 #define STLINK_MAX_RW8 (64)
101 #define STLINKV3_MAX_RW8 (512)
102 #define STLINK_MAX_RW16_32 STLINK_DATA_SIZE
103 #define STLINK_SWIM_DATA_SIZE STLINK_DATA_SIZE
108 #define MAX_WAIT_RETRIES 8
111 #define STLINK_HLA_AP_NUM 0
112 #define STLINK_HLA_CSW 0
145 struct libusb_device_handle *
fd;
187 #define MAX_QUEUE_DEPTH (4096)
209 #define CMD_MEM_AP_2_SIZE(cmd) ((cmd) & 7)
316 #define STLINK_SWIM_ERR_OK 0x00
317 #define STLINK_SWIM_BUSY 0x01
318 #define STLINK_DEBUG_ERR_OK 0x80
319 #define STLINK_DEBUG_ERR_FAULT 0x81
320 #define STLINK_SWD_AP_WAIT 0x10
321 #define STLINK_SWD_AP_FAULT 0x11
322 #define STLINK_SWD_AP_ERROR 0x12
323 #define STLINK_SWD_AP_PARITY_ERROR 0x13
324 #define STLINK_JTAG_GET_IDCODE_ERROR 0x09
325 #define STLINK_JTAG_WRITE_ERROR 0x0c
326 #define STLINK_JTAG_WRITE_VERIF_ERROR 0x0d
327 #define STLINK_SWD_DP_WAIT 0x14
328 #define STLINK_SWD_DP_FAULT 0x15
329 #define STLINK_SWD_DP_ERROR 0x16
330 #define STLINK_SWD_DP_PARITY_ERROR 0x17
332 #define STLINK_SWD_AP_WDATA_ERROR 0x18
333 #define STLINK_SWD_AP_STICKY_ERROR 0x19
334 #define STLINK_SWD_AP_STICKYORUN_ERROR 0x1a
336 #define STLINK_BAD_AP_ERROR 0x1d
338 #define STLINK_CORE_RUNNING 0x80
339 #define STLINK_CORE_HALTED 0x81
340 #define STLINK_CORE_STAT_UNKNOWN -1
342 #define STLINK_GET_VERSION 0xF1
343 #define STLINK_DEBUG_COMMAND 0xF2
344 #define STLINK_DFU_COMMAND 0xF3
345 #define STLINK_SWIM_COMMAND 0xF4
346 #define STLINK_GET_CURRENT_MODE 0xF5
347 #define STLINK_GET_TARGET_VOLTAGE 0xF7
349 #define STLINK_DEV_DFU_MODE 0x00
350 #define STLINK_DEV_MASS_MODE 0x01
351 #define STLINK_DEV_DEBUG_MODE 0x02
352 #define STLINK_DEV_SWIM_MODE 0x03
353 #define STLINK_DEV_BOOTLOADER_MODE 0x04
354 #define STLINK_DEV_UNKNOWN_MODE -1
356 #define STLINK_DFU_EXIT 0x07
375 #define STLINK_SWIM_ENTER 0x00
376 #define STLINK_SWIM_EXIT 0x01
377 #define STLINK_SWIM_READ_CAP 0x02
378 #define STLINK_SWIM_SPEED 0x03
379 #define STLINK_SWIM_ENTER_SEQ 0x04
380 #define STLINK_SWIM_GEN_RST 0x05
381 #define STLINK_SWIM_RESET 0x06
382 #define STLINK_SWIM_ASSERT_RESET 0x07
383 #define STLINK_SWIM_DEASSERT_RESET 0x08
384 #define STLINK_SWIM_READSTATUS 0x09
385 #define STLINK_SWIM_WRITEMEM 0x0a
386 #define STLINK_SWIM_READMEM 0x0b
387 #define STLINK_SWIM_READBUF 0x0c
389 #define STLINK_DEBUG_GETSTATUS 0x01
390 #define STLINK_DEBUG_FORCEDEBUG 0x02
391 #define STLINK_DEBUG_APIV1_RESETSYS 0x03
392 #define STLINK_DEBUG_APIV1_READALLREGS 0x04
393 #define STLINK_DEBUG_APIV1_READREG 0x05
394 #define STLINK_DEBUG_APIV1_WRITEREG 0x06
395 #define STLINK_DEBUG_READMEM_32BIT 0x07
396 #define STLINK_DEBUG_WRITEMEM_32BIT 0x08
397 #define STLINK_DEBUG_RUNCORE 0x09
398 #define STLINK_DEBUG_STEPCORE 0x0a
399 #define STLINK_DEBUG_APIV1_SETFP 0x0b
400 #define STLINK_DEBUG_READMEM_8BIT 0x0c
401 #define STLINK_DEBUG_WRITEMEM_8BIT 0x0d
402 #define STLINK_DEBUG_APIV1_CLEARFP 0x0e
403 #define STLINK_DEBUG_APIV1_WRITEDEBUGREG 0x0f
404 #define STLINK_DEBUG_APIV1_SETWATCHPOINT 0x10
406 #define STLINK_DEBUG_ENTER_JTAG_RESET 0x00
407 #define STLINK_DEBUG_ENTER_SWD_NO_RESET 0xa3
408 #define STLINK_DEBUG_ENTER_JTAG_NO_RESET 0xa4
410 #define STLINK_DEBUG_APIV1_ENTER 0x20
411 #define STLINK_DEBUG_EXIT 0x21
412 #define STLINK_DEBUG_READCOREID 0x22
414 #define STLINK_DEBUG_APIV2_ENTER 0x30
415 #define STLINK_DEBUG_APIV2_READ_IDCODES 0x31
416 #define STLINK_DEBUG_APIV2_RESETSYS 0x32
417 #define STLINK_DEBUG_APIV2_READREG 0x33
418 #define STLINK_DEBUG_APIV2_WRITEREG 0x34
419 #define STLINK_DEBUG_APIV2_WRITEDEBUGREG 0x35
420 #define STLINK_DEBUG_APIV2_READDEBUGREG 0x36
422 #define STLINK_DEBUG_APIV2_READALLREGS 0x3A
423 #define STLINK_DEBUG_APIV2_GETLASTRWSTATUS 0x3B
424 #define STLINK_DEBUG_APIV2_DRIVE_NRST 0x3C
426 #define STLINK_DEBUG_APIV2_GETLASTRWSTATUS2 0x3E
428 #define STLINK_DEBUG_APIV2_START_TRACE_RX 0x40
429 #define STLINK_DEBUG_APIV2_STOP_TRACE_RX 0x41
430 #define STLINK_DEBUG_APIV2_GET_TRACE_NB 0x42
431 #define STLINK_DEBUG_APIV2_SWD_SET_FREQ 0x43
432 #define STLINK_DEBUG_APIV2_JTAG_SET_FREQ 0x44
433 #define STLINK_DEBUG_APIV2_READ_DAP_REG 0x45
434 #define STLINK_DEBUG_APIV2_WRITE_DAP_REG 0x46
435 #define STLINK_DEBUG_APIV2_READMEM_16BIT 0x47
436 #define STLINK_DEBUG_APIV2_WRITEMEM_16BIT 0x48
438 #define STLINK_DEBUG_APIV2_INIT_AP 0x4B
439 #define STLINK_DEBUG_APIV2_CLOSE_AP_DBG 0x4C
441 #define STLINK_DEBUG_WRITEMEM_32BIT_NO_ADDR_INC 0x50
442 #define STLINK_DEBUG_APIV2_RW_MISC_OUT 0x51
443 #define STLINK_DEBUG_APIV2_RW_MISC_IN 0x52
445 #define STLINK_DEBUG_READMEM_32BIT_NO_ADDR_INC 0x54
447 #define STLINK_APIV3_SET_COM_FREQ 0x61
448 #define STLINK_APIV3_GET_COM_FREQ 0x62
450 #define STLINK_APIV3_GET_VERSION_EX 0xFB
452 #define STLINK_DEBUG_APIV2_DRIVE_NRST_LOW 0x00
453 #define STLINK_DEBUG_APIV2_DRIVE_NRST_HIGH 0x01
454 #define STLINK_DEBUG_APIV2_DRIVE_NRST_PULSE 0x02
456 #define STLINK_DEBUG_PORT_ACCESS 0xffff
458 #define STLINK_TRACE_SIZE 4096
459 #define STLINK_TRACE_MAX_HZ 2250000
460 #define STLINK_V3_TRACE_MAX_HZ 24000000
462 #define STLINK_V3_MAX_FREQ_NB 10
464 #define REQUEST_SENSE 0x03
465 #define REQUEST_SENSE_LENGTH 18
468 #define STLINK_TCP_CMD_REFRESH_DEVICE_LIST 0x00
469 #define STLINK_TCP_CMD_GET_NB_DEV 0x01
470 #define STLINK_TCP_CMD_GET_DEV_INFO 0x02
471 #define STLINK_TCP_CMD_OPEN_DEV 0x03
472 #define STLINK_TCP_CMD_CLOSE_DEV 0x04
473 #define STLINK_TCP_CMD_SEND_USB_CMD 0x05
474 #define STLINK_TCP_CMD_GET_SERVER_VERSION 0x06
475 #define STLINK_TCP_CMD_GET_NB_OF_DEV_CLIENTS 0x07
478 #define OPENOCD_STLINK_TCP_API_VERSION 1
479 #define STLINK_TCP_REQUEST_WRITE 0
480 #define STLINK_TCP_REQUEST_READ 1
481 #define STLINK_TCP_REQUEST_READ_SWO 3
482 #define STLINK_TCP_SS_SIZE 4
483 #define STLINK_TCP_USB_CMD_SIZE 32
484 #define STLINK_TCP_SERIAL_SIZE 32
485 #define STLINK_TCP_SEND_BUFFER_SIZE 10240
486 #define STLINK_TCP_RECV_BUFFER_SIZE 10240
489 #define STLINK_TCP_SS_OK 0x00000001
490 #define STLINK_TCP_SS_MEMORY_PROBLEM 0x00001000
491 #define STLINK_TCP_SS_TIMEOUT 0x00001001
492 #define STLINK_TCP_SS_BAD_PARAMETER 0x00001002
493 #define STLINK_TCP_SS_OPEN_ERR 0x00001003
494 #define STLINK_TCP_SS_TRUNCATED_DATA 0x00001052
495 #define STLINK_TCP_SS_CMD_NOT_AVAILABLE 0x00001053
496 #define STLINK_TCP_SS_TCP_ERROR 0x00002001
497 #define STLINK_TCP_SS_TCP_CANT_CONNECT 0x00002002
498 #define STLINK_TCP_SS_TCP_CLOSE_ERROR 0x00002003
499 #define STLINK_TCP_SS_TCP_BUSY 0x00002004
500 #define STLINK_TCP_SS_WIN32_ERROR 0x00010000
506 #define STLINK_F_HAS_TRACE BIT(0)
507 #define STLINK_F_HAS_GETLASTRWSTATUS2 BIT(1)
508 #define STLINK_F_HAS_SWD_SET_FREQ BIT(2)
509 #define STLINK_F_HAS_JTAG_SET_FREQ BIT(3)
510 #define STLINK_F_QUIRK_JTAG_DP_READ BIT(4)
511 #define STLINK_F_HAS_DAP_REG BIT(5)
512 #define STLINK_F_HAS_MEM_16BIT BIT(6)
513 #define STLINK_F_HAS_AP_INIT BIT(7)
514 #define STLINK_F_FIX_CLOSE_AP BIT(8)
515 #define STLINK_F_HAS_DPBANKSEL BIT(9)
516 #define STLINK_F_HAS_RW8_512BYTES BIT(10)
519 #define STLINK_F_HAS_TARGET_VOLT STLINK_F_HAS_TRACE
520 #define STLINK_F_HAS_FPU_REG STLINK_F_HAS_GETLASTRWSTATUS2
521 #define STLINK_F_HAS_MEM_WR_NO_INC STLINK_F_HAS_MEM_16BIT
522 #define STLINK_F_HAS_MEM_RD_NO_INC STLINK_F_HAS_DPBANKSEL
523 #define STLINK_F_HAS_RW_MISC STLINK_F_HAS_DPBANKSEL
524 #define STLINK_F_HAS_CSW STLINK_F_HAS_DPBANKSEL
526 #define STLINK_REGSEL_IS_FPU(x) ((x) > 0x1F)
564 static int stlink_speed(
void *handle,
int khz,
bool query);
580 #ifdef USE_LIBUSB_ASYNCIO
582 static LIBUSB_CALL void sync_transfer_cb(
struct libusb_transfer *transfer)
584 int *completed = transfer->user_data;
590 static void sync_transfer_wait_for_completion(
struct libusb_transfer *transfer)
592 int r, *completed = transfer->user_data;
594 while (!*completed) {
597 if (r == LIBUSB_ERROR_INTERRUPTED)
599 libusb_cancel_transfer(transfer);
606 static int transfer_error_status(
const struct libusb_transfer *transfer)
610 switch (transfer->status) {
611 case LIBUSB_TRANSFER_COMPLETED:
614 case LIBUSB_TRANSFER_TIMED_OUT:
615 r = LIBUSB_ERROR_TIMEOUT;
617 case LIBUSB_TRANSFER_STALL:
618 r = LIBUSB_ERROR_PIPE;
620 case LIBUSB_TRANSFER_OVERFLOW:
621 r = LIBUSB_ERROR_OVERFLOW;
623 case LIBUSB_TRANSFER_NO_DEVICE:
624 r = LIBUSB_ERROR_NO_DEVICE;
626 case LIBUSB_TRANSFER_ERROR:
627 case LIBUSB_TRANSFER_CANCELLED:
631 r = LIBUSB_ERROR_OTHER;
645 size_t transfer_size;
646 struct libusb_transfer *transfer;
649 static int jtag_libusb_bulk_transfer_n(
650 struct libusb_device_handle *dev_handle,
651 struct jtag_xfer *transfers,
659 for (
size_t i = 0; i < n_transfers; ++i) {
660 transfers[i].retval = 0;
661 transfers[i].completed = 0;
662 transfers[i].transfer_size = 0;
663 transfers[i].transfer = libusb_alloc_transfer(0);
665 if (!transfers[i].transfer) {
666 for (
size_t j = 0; j < i; ++j)
667 libusb_free_transfer(transfers[j].transfer);
669 LOG_DEBUG(
"ERROR, failed to alloc usb transfers");
670 for (
size_t k = 0; k < n_transfers; ++k)
671 transfers[k].retval = LIBUSB_ERROR_NO_MEM;
676 for (
size_t i = 0; i < n_transfers; ++i) {
677 libusb_fill_bulk_transfer(
678 transfers[i].transfer,
680 transfers[i].ep, transfers[i].buf, transfers[i].
size,
681 sync_transfer_cb, &transfers[i].completed,
timeout);
682 transfers[i].transfer->type = LIBUSB_TRANSFER_TYPE_BULK;
684 retval = libusb_submit_transfer(transfers[i].transfer);
686 LOG_DEBUG(
"ERROR, failed to submit transfer %zu, error %d", i, retval);
691 for (
size_t j = i; j < n_transfers; ++j)
692 transfers[j].retval = retval;
701 for (
size_t i = 0; i < n_transfers; ++i) {
702 if (transfers[i].retval == 0) {
703 sync_transfer_wait_for_completion(transfers[i].transfer);
705 retval = transfer_error_status(transfers[i].transfer);
708 transfers[i].retval = retval;
709 LOG_DEBUG(
"ERROR, transfer %zu failed, error %d", i, retval);
713 transfers[i].transfer_size = transfers[i].transfer->actual_length;
717 libusb_free_transfer(transfers[i].transfer);
718 transfers[i].transfer =
NULL;
748 if (
t1 != 0x53425355)
762 #ifdef USE_LIBUSB_ASYNCIO
769 size_t n_transfers = 0;
770 struct jtag_xfer transfers[2];
772 memset(transfers, 0,
sizeof(transfers));
774 transfers[0].ep = h->
tx_ep;
775 transfers[0].buf = h->
cmdbuf;
776 transfers[0].size = cmdsize;
781 transfers[1].ep = h->
tx_ep;
782 transfers[1].buf = (uint8_t *)buf;
783 transfers[1].size =
size;
787 transfers[1].ep = h->
rx_ep;
788 transfers[1].buf = (uint8_t *)buf;
789 transfers[1].size =
size;
794 return jtag_libusb_bulk_transfer_n(
810 if (ret || tr != cmdsize)
816 if (ret || tr !=
size) {
823 if (ret || tr !=
size) {
868 if (ret || tr !=
size) {
924 if (sent_size != send_size) {
927 LOG_DEBUG(
"socket send error: %s (errno %d)", strerror(errno), errno);
929 LOG_DEBUG(
"sent size %d (expected %d)", sent_size, send_size);
935 int remaining_bytes = recv_size;
939 while (remaining_bytes > 0) {
941 LOG_DEBUG(
"received size %d (expected %d)", recv_size - remaining_bytes, recv_size);
949 if (received == -1) {
950 LOG_DEBUG(
"socket recv error: %s (errno %d)", strerror(errno), errno);
955 recv_buf += received;
956 remaining_bytes -= received;
960 LOG_ERROR(
"failed to receive USB CMD response");
964 if (check_tcp_status) {
972 LOG_ERROR(
"TCP error status 0x%X", tcp_ss);
1013 LOG_ERROR(
"STLINK_TCP command buffer overflow");
1020 LOG_ERROR(
"STLINK_TCP data buffer overflow");
1088 LOG_DEBUG(
"STLINK_JTAG_GET_IDCODE_ERROR");
1094 LOG_DEBUG(
"Write verify error, ignoring");
1108 LOG_DEBUG(
"STLINK_SWD_AP_PARITY_ERROR");
1117 LOG_DEBUG(
"STLINK_SWD_DP_PARITY_ERROR");
1123 LOG_DEBUG(
"STLINK_SWD_AP_STICKY_ERROR");
1126 LOG_DEBUG(
"STLINK_SWD_AP_STICKYORUN_ERROR");
1182 unsigned int delay_us = (1<<retries++) * 1000;
1183 LOG_DEBUG(
"stlink_cmd_allow_retry ERROR_WAIT, retry %d, delaying %u microseconds", retries,
delay_us);
1219 strcpy((
char *)h->
cmdbuf,
"USBC");
1256 uint8_t v, x, y, jtag, swim, msd, bridge = 0;
1257 char v_str[5 * (1 + 3) + 1];
1283 if ((x <= 22 && y == 7) || (x >= 25 && y >= 7 && y <= 12)) {
1303 if (v >= 3 && x == 0 && y == 0) {
1461 p += sprintf(p,
"V%d", v);
1463 p += sprintf(p,
"J%d", jtag);
1465 p += sprintf(p,
"M%d", msd);
1467 p += sprintf(p,
"B%d", bridge);
1469 sprintf(p,
"S%d", swim);
1471 LOG_INFO(
"STLINK %s (API v%d) VID:PID %04X:%04X",
1483 uint32_t adc_results[2];
1502 *target_voltage = 0;
1505 *target_voltage = 2 * ((float)adc_results[1]) * (float)(1.2 / adc_results[0]);
1507 LOG_INFO(
"Target voltage: %f", (
double)*target_voltage);
1747 float target_voltage;
1759 if (target_voltage < 1.5)
1760 LOG_ERROR(
"target voltage may be too low for reliable debugging");
1770 LOG_ERROR(
"selected mode (transport) not supported");
1813 if (connect_under_reset) {
1850 static int stlink_swim_cap(
void *handle, uint8_t *cap)
1963 unsigned int datalen = 0;
1979 for (i = 0; i < len; i++) {
1980 if (h->
cmdidx == cmdsize)
1981 h->
databuf[datalen++] = *(data++);
2057 LOG_DEBUG(
"IDCODE: 0x%08" PRIX32, *idcode);
2164 LOG_INFO(
"Previous state query failed, trying to reconnect");
2267 LOG_ERROR(
"Tracing is not supported by this version.");
2489 uint32_t
addr, uint16_t len, uint8_t *
buffer)
2492 uint16_t read_len = len;
2534 uint32_t
addr, uint16_t len,
const uint8_t *
buffer)
2572 uint32_t
addr, uint16_t len, uint8_t *
buffer)
2591 if (len % 2 ||
addr % 2) {
2620 uint32_t
addr, uint16_t len,
const uint8_t *
buffer)
2639 if (len % 2 ||
addr % 2) {
2666 uint32_t
addr, uint16_t len, uint8_t *
buffer)
2682 if (len % 4 ||
addr % 4) {
2711 uint32_t
addr, uint16_t len,
const uint8_t *
buffer)
2727 if (len % 4 ||
addr % 4) {
2753 uint32_t
addr, uint16_t len, uint8_t *
buffer)
2768 if (len % 4 ||
addr % 4) {
2795 uint32_t
addr, uint16_t len,
const uint8_t *
buffer)
2810 if (len % 4 ||
addr % 4) {
2836 uint32_t max_tar_block = (tar_autoincr_block - ((tar_autoincr_block - 1) & address));
2837 if (max_tar_block == 0)
2839 return max_tar_block;
2846 uint32_t bytes_remaining;
2858 bytes_remaining = (
size != 1) ?
2861 if (
count < bytes_remaining)
2862 bytes_remaining =
count;
2884 usleep((1 << retries++) * 1000);
2891 count -= head_bytes;
2892 bytes_remaining -= head_bytes;
2895 if (bytes_remaining & (
size - 1))
2906 usleep((1 << retries++) * 1000);
2912 buffer += bytes_remaining;
2913 addr += bytes_remaining;
2914 count -= bytes_remaining;
2931 uint32_t bytes_remaining;
2944 bytes_remaining = (
size != 1) ?
2947 if (
count < bytes_remaining)
2948 bytes_remaining =
count;
2972 usleep((1<<retries++) * 1000);
2979 count -= head_bytes;
2980 bytes_remaining -= head_bytes;
2983 if (bytes_remaining & (
size - 1))
2993 usleep((1<<retries++) * 1000);
2999 buffer += bytes_remaining;
3000 addr += bytes_remaining;
3001 count -= bytes_remaining;
3017 return !strcmp(targetname,
"cortex_m");
3032 LOG_ERROR(
"Unable to set adapter speed");
3041 int speed_index = -1;
3042 int speed_diff = INT_MAX;
3043 int last_valid_speed = -1;
3046 for (i = 0; i < map_size; i++) {
3049 last_valid_speed = i;
3050 if (khz == map[i].speed) {
3054 int current_diff = khz - map[i].
speed;
3056 current_diff = (current_diff > 0) ? current_diff : -current_diff;
3057 if ((current_diff < speed_diff) && khz >= map[i].
speed) {
3058 speed_diff = current_diff;
3064 if (speed_index == -1) {
3067 speed_index = last_valid_speed;
3069 }
else if (i == map_size)
3072 if (!match && query) {
3073 LOG_INFO(
"Unable to match requested speed %d kHz, using %d kHz",
3074 khz, map[speed_index].speed);
3095 LOG_ERROR(
"Unable to set adapter speed");
3118 LOG_ERROR(
"Unable to set adapter speed");
3130 LOG_DEBUG(
"Supported clock speeds are:");
3131 for (i = 0; i < map_size; i++)
3159 for (i = 0; i <
size; i++) {
3205 LOG_ERROR(
"Unable to set adapter speed");
3209 return map[speed_index].
speed;
3285 LOG_ERROR(
"error closing the socket, errno: %s", strerror(errno));
3325 struct libusb_device_descriptor *dev_desc)
3330 if (dev_desc->iSerialNumber == 0)
3334 usb_retval = libusb_get_string_descriptor(
device, 0, 0, desc_serial,
3335 sizeof(desc_serial));
3337 if (usb_retval < LIBUSB_SUCCESS) {
3338 LOG_ERROR(
"libusb_get_string_descriptor() failed: %s(%d)",
3339 libusb_error_name(usb_retval), usb_retval);
3341 }
else if (usb_retval < 4) {
3347 uint32_t langid = desc_serial[2] | (desc_serial[3] << 8);
3350 usb_retval = libusb_get_string_descriptor(
device, dev_desc->iSerialNumber,
3351 langid, desc_serial,
sizeof(desc_serial));
3353 unsigned char len = desc_serial[0];
3355 if (usb_retval < LIBUSB_SUCCESS) {
3356 LOG_ERROR(
"libusb_get_string_descriptor() failed: %s(%d)",
3357 libusb_error_name(usb_retval), usb_retval);
3359 }
else if (desc_serial[1] != LIBUSB_DT_STRING || len > usb_retval) {
3360 LOG_ERROR(
"invalid string in ST-LINK USB serial descriptor");
3369 LOG_ERROR(
"unexpected serial length (%d) in descriptor", len);
3376 if (!alternate_serial)
3380 sprintf(alternate_serial + i,
"%02X", desc_serial[i + 2]);
3384 return alternate_serial;
3391 int err, retry_count = 1;
3490 usleep(1 * 1000 * 1000);
3506 LOG_ERROR(
"stlink-server does not support SWIM mode");
3530 LOG_ERROR(
"error creating the socket, errno: %s", strerror(errno));
3534 struct sockaddr_in serv;
3535 memset(&serv, 0,
sizeof(
struct sockaddr_in));
3536 serv.sin_family = AF_INET;
3538 serv.sin_addr.s_addr = inet_addr(
"127.0.0.1");
3543 if (setsockopt(h->
tcp_backend_priv.
fd, IPPROTO_TCP, TCP_NODELAY, (
const void *)&optval,
sizeof(
int)) == -1) {
3544 LOG_ERROR(
"cannot set sock option 'TCP_NODELAY', errno: %s", strerror(errno));
3549 if (setsockopt(h->
tcp_backend_priv.
fd, SOL_SOCKET, SO_RCVBUF, (
const void *)&optval,
sizeof(
int)) == -1) {
3550 LOG_ERROR(
"cannot set sock option 'SO_RCVBUF', errno: %s", strerror(errno));
3555 if (setsockopt(h->
tcp_backend_priv.
fd, SOL_SOCKET, SO_SNDBUF, (
const void *)&optval,
sizeof(
int)) == -1) {
3556 LOG_ERROR(
"cannot set sock option 'SO_SNDBUF', errno: %s", strerror(errno));
3560 if (connect(h->
tcp_backend_priv.
fd, (
const struct sockaddr *)&serv,
sizeof(serv)) == -1) {
3561 LOG_ERROR(
"cannot connect to stlink server, errno: %s", strerror(errno));
3567 LOG_INFO(
"connected to stlink-server");
3575 LOG_ERROR(
"cannot get the stlink-server version");
3583 LOG_INFO(
"stlink-server API v%d, version %d.%d.%d",
3609 if (connected_stlinks == 0) {
3614 LOG_DEBUG(
"%d ST-LINK detected", connected_stlinks);
3616 if (connected_stlinks > 255) {
3617 LOG_WARNING(
"STLink server cannot handle more than 255 ST-LINK connected");
3618 connected_stlinks = 255;
3623 uint8_t stlink_used;
3624 bool stlink_id_matched =
false;
3626 bool stlink_serial_matched = !adapter_serial;
3628 for (uint32_t stlink_id = 0; stlink_id < connected_stlinks; stlink_id++) {
3645 for (
int i = 0; param->
vid[i]; i++) {
3646 if (param->
vid[i] == h->
vid && param->
pid[i] == h->
pid) {
3647 stlink_id_matched =
true;
3652 if (!stlink_id_matched)
3656 if (adapter_serial) {
3665 sprintf(fixed_serial + i,
"%02X", adapter_serial[i / 2]);
3669 stlink_serial_matched = strcmp(fixed_serial,
serial) == 0;
3671 stlink_serial_matched = strcmp(adapter_serial,
serial) == 0;
3675 if (!stlink_serial_matched)
3676 LOG_DEBUG(
"Device serial number '%s' doesn't match requested serial '%s'",
3682 if (!stlink_id_matched) {
3683 LOG_ERROR(
"ST-LINK open failed (vid/pid mismatch)");
3687 if (!stlink_serial_matched) {
3688 LOG_ERROR(
"ST-LINK open failed (serial mismatch)");
3694 LOG_ERROR(
"the selected device is already used");
3743 for (
unsigned int i = 0; param->
vid[i]; i++) {
3744 LOG_DEBUG(
"transport: %d vid: 0x%04x pid: 0x%04x serial: %s",
3779 LOG_ERROR(
"mode (transport) not supported by device");
3787 LOG_ERROR(
"init mode failed (unable to connect to the target)");
3794 LOG_ERROR(
"stlink_swim_enter_failed (unable to connect to the target)");
3813 int i = (cpuid >> 4) & 0xf;
3814 if (i == 4 || i == 3) {
3839 unsigned int *trace_freq,
unsigned int traceclkin_freq,
3840 uint16_t *prescaler)
3845 LOG_ERROR(
"The attached ST-LINK version doesn't support trace");
3858 LOG_ERROR(
"The attached ST-LINK version doesn't support this trace mode");
3866 if (*trace_freq > max_trace_freq) {
3867 LOG_ERROR(
"ST-LINK doesn't support SWO frequency higher than %u",
3873 *trace_freq = max_trace_freq;
3875 unsigned int presc = (traceclkin_freq + *trace_freq / 2) / *trace_freq;
3877 LOG_ERROR(
"SWO frequency is not suitable. Please choose a different "
3883 unsigned int max_deviation = (traceclkin_freq * 3) / 100;
3884 if (presc * *trace_freq < traceclkin_freq - max_deviation ||
3885 presc * *trace_freq > traceclkin_freq + max_deviation) {
3886 LOG_ERROR(
"SWO frequency is not suitable. Please choose a different "
3946 unsigned int buflen =
ALIGN_UP(items, 4) + 4 * items;
3967 unsigned int buflen = 2 * 4 * items;
3992 unsigned short addr, uint32_t *val)
4010 LOG_DEBUG_IO(
"dap_port_read = %d, addr = 0x%x, value = 0x%" PRIx32, dap_port,
addr, *val);
4016 unsigned short addr, uint32_t val)
4025 LOG_DEBUG_IO(
"dap_write port = %d, addr = 0x%x, value = 0x%" PRIx32, dap_port,
addr, val);
4183 for (
unsigned int apsel = 0; apsel <=
DP_APSEL_MAX; apsel++)
4207 LOG_WARNING(
"\'srst_nogate\' reset_config option is required");
4262 if (
reg & 0x000000F0) {
4263 LOG_ERROR(
"Banked DP registers not supported in current STLink FW");
4267 data = data ? data : &
dummy;
4290 if (
reg & 0x000000F0) {
4291 LOG_ERROR(
"Banked DP registers not supported in current STLink FW");
4297 LOG_DEBUG(
"Ignoring DPBANKSEL while write SELECT");
4318 static bool error_flagged;
4320 LOG_ERROR(
"ADIv6 dap not supported by stlink dap-direct mode");
4321 error_flagged =
true;
4330 data = data ? data : &
dummy;
4344 static bool error_flagged;
4346 LOG_ERROR(
"ADIv6 dap not supported by stlink dap-direct mode");
4347 error_flagged =
true;
4368 #define RW_MISC_CMD_ADDRESS 1
4369 #define RW_MISC_CMD_WRITE 2
4370 #define RW_MISC_CMD_READ 3
4371 #define RW_MISC_CMD_APNUM 5
4375 uint8_t buf[2 * 4 * items];
4377 LOG_DEBUG(
"Queue: %u commands in %u items", len, items);
4381 unsigned int val_index =
ALIGN_UP(items, 4);
4382 for (
unsigned int i = 0; i < len; i++) {
4384 ap_num = q[i].
mem_ap.ap->ap_num;
4423 unsigned int err_index = 4 * items;
4424 for (
unsigned int i = 0; i < len; i++) {
4427 LOG_ERROR(
"unknown/unexpected STLINK status code 0x%x", errcode);
4431 ap_num = q[i].
mem_ap.ap->ap_num;
4436 LOG_ERROR(
"unknown/unexpected STLINK status code 0x%x", errcode);
4448 LOG_ERROR(
"unknown/unexpected STLINK status code 0x%x", errcode);
4462 uint8_t buf[bufsize];
4463 uint8_t ap_num = q[0].
mem_ap.ap->ap_num;
4465 uint32_t csw = q[0].
mem_ap.csw;
4473 for (
unsigned int i = 0; i <
count; i++)
4474 buf[i] = q[i].
mem_ap.data >> 8 * (q[i].
mem_ap.addr & 3);
4478 for (
unsigned int i = 0; i <
count; i++)
4483 for (
unsigned int i = 0; i <
count; i++)
4493 for (
unsigned int i = 0; i <
count; i++)
4494 *q[i].
mem_ap.p_data = buf[i] << 8 * (q[i].
mem_ap.addr & 3);
4500 for (
unsigned int i = 0; i <
count; i++)
4510 for (
unsigned int i = 0; i <
count; i++)
4520 #define STLINK_V2_RW_MISC_SIZE (64)
4521 #define STLINK_V3_RW_MISC_SIZE (1227)
4524 unsigned int *pkt_items)
4527 unsigned int i, items = 0;
4542 for (i = 0; i < len; i++) {
4545 unsigned int count = 1;
4548 ap_num = q[i].
mem_ap.ap->ap_num;
4552 if (items +
count > misc_max_items)
4565 unsigned int len_max;
4573 if (len > 1 && incr == 4 && q[0].
mem_ap.addr == q[1].
mem_ap.addr)
4579 for (
unsigned int i = 1; i < len; i++)
4580 if (q[i].
cmd != q[0].
cmd ||
4591 unsigned int count, misc_items = 0;
4597 if (count_misc > count_buf) {
4624 unsigned int skip = 1;
4653 LOG_ERROR(
"ST-Link: Unknown queue command %d", q->
cmd);
4668 uint32_t ctrlstat, pwrmask;
4669 int retval, saved_retval;
4692 LOG_ERROR(
"Fail reading CTRL/STAT register. Force reconnect");
4711 if ((ctrlstat & pwrmask) != pwrmask)
4714 return saved_retval;
4795 !prev_q->
ap_w.changes_csw_default) {
4861 !prev_q->
ap_w.changes_csw_default) {
4897 q->
ap_w.changes_csw_default =
true;
4900 q->
ap_w.changes_csw_default =
false;
4919 uint32_t bytes_remaining;
4930 buffer += bytes_remaining;
4931 addr += bytes_remaining;
4932 count -= bytes_remaining;
4942 uint32_t bytes_remaining;
4953 buffer += bytes_remaining;
4954 addr += bytes_remaining;
4955 count -= bytes_remaining;
4974 unsigned int *trace_freq,
unsigned int traceclkin_freq,
4975 uint16_t *prescaler)
4978 port_size, trace_freq, traceclkin_freq,
4994 "(maximum is %d pairs)", max_usb_ids);
4998 LOG_WARNING(
"incomplete vid_pid configuration directive");
5001 for (i = 0; i <
CMD_ARGC; i += 2) {
5016 bool use_stlink_tcp =
false;
5017 uint16_t stlink_tcp_port = 7184;
5021 else if (strcmp(
CMD_ARGV[0],
"usb") == 0) {
5025 }
else if (strcmp(
CMD_ARGV[0],
"tcp") == 0) {
5026 use_stlink_tcp =
true;
5038 #define BYTES_PER_LINE 16
5041 unsigned int rx_n, tx_n;
5056 for (
unsigned int i = 0; i < tx_n; i++) {
5068 for (
unsigned int i = 0; i < rx_n; i++)
5079 .handler = stlink_dap_vid_pid,
5081 .help =
"USB VID and PID of the adapter",
5082 .usage =
"(vid pid)+",
5086 .handler = &stlink_dap_backend_command,
5088 .help =
"select which ST-Link backend to use",
5089 .usage =
"usb | tcp [port]",
5093 .handler = stlink_dap_cmd_command,
5095 .help =
"send arbitrary command",
5096 .usage =
"rx_n (tx_byte)+",
5106 .help =
"perform st-link management",
5126 LOG_WARNING(
"\'srst_nogate\' reset_config option is required");
5146 LOG_ERROR(
"ST-Link version does not support DAP direct transport");
5163 LOG_DEBUG(
"stlink_dap_reset(%d)", req_srst);
5173 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.
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 int first, unsigned int 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 int first, unsigned int 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_open(const uint16_t vids[], const uint16_t pids[], const char *product, struct libusb_device_handle **out, adapter_get_alternate_serial_fn adapter_get_alternate_serial)
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)
#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)
target_addr_t addr
Start address to search for the control block.
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_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
static int stlink_usb_open(void *handle, struct hl_interface_param *param)
#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)
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 int stlink_usb_read_regs(void *handle)
#define STLINK_TRACE_MAX_HZ
static const char *const stlink_dap_transport[]
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
static int stlink_open(struct hl_interface_param *param, enum stlink_mode mode, void **fd)
#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
static int stlink_tcp_open(void *handle, struct hl_interface_param *param)
#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)
#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)
static struct stlink_backend stlink_tcp_backend
#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_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
struct hl_layout_api stlink_usb_layout_api
#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 struct hl_interface_param stlink_dap_param
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 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_hl_open(struct hl_interface_param *param, void **fd)
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
#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
static struct stlink_usb_handle * stlink_dap_handle
#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_V3P_USBLOADER_PID
#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 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)
#define STLINK_DEBUG_ERR_FAULT
#define STLINK_DEV_SWIM_MODE
#define STLINK_TCP_CMD_SEND_USB_CMD
static int stlink_usb_usb_open(void *handle, struct hl_interface_param *param)
#define STLINK_TCP_CMD_GET_NB_DEV
static struct stlink_backend stlink_usb_backend
#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::@33::dp_w dp_w
struct dap_queue::@33::ap_w ap_w
struct dap_queue::@33::mem_ap mem_ap
struct dap_queue::@33::dp_r dp_r
struct dap_queue::@33::ap_r ap_r
enum hl_transports transport
uint16_t pid[HLA_MAX_USB_IDS+1]
List of recognised PIDs.
uint16_t vid[HLA_MAX_USB_IDS+1]
List of recognised VIDs.
int initial_interface_speed
Initial interface clock clock speed.
int(* open)(struct hl_interface_param *param, void **handle)
int(* open)(void *handle, struct hl_interface_param *param)
int(* xfer_noerrcheck)(void *handle, const uint8_t *buf, int size)
int(* close)(void *handle)
int(* read_trace)(void *handle, const uint8_t *buf, int size)
struct stlink_tcp_version version
unsigned int queue_index
first element available in the queue
struct stlink_backend * backend
bool reconnect_pending
reconnect is needed next time we try to query the status
struct stlink_usb_version version
struct stlink_tcp_priv tcp_backend_priv
uint32_t source_hz
trace module source clock
struct dap_queue queue[MAX_QUEUE_DEPTH]
queue of dap_direct operations
bool enabled
whether SWO tracing is enabled or not
struct stlink_usb_handle::@39 trace
struct stlink_usb_priv usb_backend_priv
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)