+ return stlink_usb_get_rw_status(handle);
+}
+
+/** */
+static int stlink_usb_read_mem32(void *handle, uint32_t addr, uint16_t len,
+ uint8_t *buffer)
+{
+ int res;
+ struct stlink_usb_handle_s *h = handle;
+
+ assert(handle != NULL);
+
+ /* data must be a multiple of 4 and word aligned */
+ if (len % 4 || addr % 4) {
+ LOG_DEBUG("Invalid data alignment");
+ return ERROR_TARGET_UNALIGNED_ACCESS;
+ }
+
+ stlink_usb_init_buffer(handle, h->rx_ep, len);
+
+ h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
+ h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_READMEM_32BIT;
+ h_u32_to_le(h->cmdbuf+h->cmdidx, addr);
+ h->cmdidx += 4;
+ h_u16_to_le(h->cmdbuf+h->cmdidx, len);
+ h->cmdidx += 2;
+
+ res = stlink_usb_xfer_noerrcheck(handle, h->databuf, len);
+
+ if (res != ERROR_OK)
+ return res;
+
+ memcpy(buffer, h->databuf, len);
+
+ return stlink_usb_get_rw_status(handle);
+}
+
+/** */
+static int stlink_usb_write_mem32(void *handle, uint32_t addr, uint16_t len,
+ const uint8_t *buffer)
+{
+ int res;
+ struct stlink_usb_handle_s *h = handle;
+
+ assert(handle != NULL);
+
+ /* data must be a multiple of 4 and word aligned */
+ if (len % 4 || addr % 4) {
+ LOG_DEBUG("Invalid data alignment");
+ return ERROR_TARGET_UNALIGNED_ACCESS;
+ }
+
+ stlink_usb_init_buffer(handle, h->tx_ep, len);
+
+ h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
+ h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_WRITEMEM_32BIT;
+ h_u32_to_le(h->cmdbuf+h->cmdidx, addr);
+ h->cmdidx += 4;
+ h_u16_to_le(h->cmdbuf+h->cmdidx, len);
+ h->cmdidx += 2;
+
+ res = stlink_usb_xfer_noerrcheck(handle, buffer, len);
+
+ if (res != ERROR_OK)
+ return res;
+
+ return stlink_usb_get_rw_status(handle);
+}
+
+static uint32_t stlink_max_block_size(uint32_t tar_autoincr_block, uint32_t address)
+{
+ uint32_t max_tar_block = (tar_autoincr_block - ((tar_autoincr_block - 1) & address));
+ if (max_tar_block == 0)
+ max_tar_block = 4;
+ return max_tar_block;
+}
+
+static int stlink_usb_read_mem(void *handle, uint32_t addr, uint32_t size,
+ uint32_t count, uint8_t *buffer)
+{
+ int retval = ERROR_OK;
+ uint32_t bytes_remaining;
+ int retries = 0;
+ struct stlink_usb_handle_s *h = handle;
+
+ /* calculate byte count */
+ count *= size;
+
+ /* switch to 8 bit if stlink does not support 16 bit memory read */
+ if (size == 2 && !(h->version.flags & STLINK_F_HAS_MEM_16BIT))
+ size = 1;
+
+ while (count) {
+
+ bytes_remaining = (size != 1) ?
+ stlink_max_block_size(h->max_mem_packet, addr) : stlink_usb_block(h);
+
+ if (count < bytes_remaining)
+ bytes_remaining = count;
+
+ if (h->transport == HL_TRANSPORT_SWIM) {
+ retval = stlink_swim_readbytes(handle, addr, bytes_remaining, buffer);
+ if (retval != ERROR_OK)
+ return retval;
+ } else
+ /*
+ * all stlink support 8/32bit memory read/writes and only from
+ * stlink V2J26 there is support for 16 bit memory read/write.
+ * Honour 32 bit and, if possible, 16 bit too. Otherwise, handle
+ * as 8bit access.
+ */
+ if (size != 1) {
+
+ /* When in jtag mode the stlink uses the auto-increment functionality.
+ * However it expects us to pass the data correctly, this includes
+ * alignment and any page boundaries. We already do this as part of the
+ * adi_v5 implementation, but the stlink is a hla adapter and so this
+ * needs implementing manually.
+ * currently this only affects jtag mode, according to ST they do single
+ * access in SWD mode - but this may change and so we do it for both modes */
+
+ /* we first need to check for any unaligned bytes */
+ if (addr & (size - 1)) {
+
+ uint32_t head_bytes = size - (addr & (size - 1));
+ retval = stlink_usb_read_mem8(handle, addr, head_bytes, buffer);
+ if (retval == ERROR_WAIT && retries < MAX_WAIT_RETRIES) {
+ usleep((1<<retries++) * 1000);
+ continue;
+ }
+ if (retval != ERROR_OK)
+ return retval;
+ buffer += head_bytes;
+ addr += head_bytes;
+ count -= head_bytes;
+ bytes_remaining -= head_bytes;
+ }
+
+ if (bytes_remaining & (size - 1))
+ retval = stlink_usb_read_mem(handle, addr, 1, bytes_remaining, buffer);
+ else if (size == 2)
+ retval = stlink_usb_read_mem16(handle, addr, bytes_remaining, buffer);
+ else
+ retval = stlink_usb_read_mem32(handle, addr, bytes_remaining, buffer);
+ } else
+ retval = stlink_usb_read_mem8(handle, addr, bytes_remaining, buffer);
+
+ if (retval == ERROR_WAIT && retries < MAX_WAIT_RETRIES) {
+ usleep((1<<retries++) * 1000);
+ continue;
+ }
+ if (retval != ERROR_OK)
+ return retval;
+
+ buffer += bytes_remaining;
+ addr += bytes_remaining;
+ count -= bytes_remaining;
+ }
+
+ return retval;
+}
+
+static int stlink_usb_write_mem(void *handle, uint32_t addr, uint32_t size,
+ uint32_t count, const uint8_t *buffer)
+{
+ int retval = ERROR_OK;
+ uint32_t bytes_remaining;
+ int retries = 0;
+ struct stlink_usb_handle_s *h = handle;
+
+ /* calculate byte count */
+ count *= size;
+
+ /* switch to 8 bit if stlink does not support 16 bit memory read */
+ if (size == 2 && !(h->version.flags & STLINK_F_HAS_MEM_16BIT))
+ size = 1;
+
+ while (count) {
+
+ bytes_remaining = (size != 1) ?
+ stlink_max_block_size(h->max_mem_packet, addr) : stlink_usb_block(h);
+
+ if (count < bytes_remaining)
+ bytes_remaining = count;
+
+ if (h->transport == HL_TRANSPORT_SWIM) {
+ retval = stlink_swim_writebytes(handle, addr, bytes_remaining, buffer);
+ if (retval != ERROR_OK)
+ return retval;
+ } else
+ /*
+ * all stlink support 8/32bit memory read/writes and only from
+ * stlink V2J26 there is support for 16 bit memory read/write.
+ * Honour 32 bit and, if possible, 16 bit too. Otherwise, handle
+ * as 8bit access.
+ */
+ if (size != 1) {
+
+ /* When in jtag mode the stlink uses the auto-increment functionality.
+ * However it expects us to pass the data correctly, this includes
+ * alignment and any page boundaries. We already do this as part of the
+ * adi_v5 implementation, but the stlink is a hla adapter and so this
+ * needs implementing manually.
+ * currently this only affects jtag mode, according to ST they do single
+ * access in SWD mode - but this may change and so we do it for both modes */
+
+ /* we first need to check for any unaligned bytes */
+ if (addr & (size - 1)) {
+
+ uint32_t head_bytes = size - (addr & (size - 1));
+ retval = stlink_usb_write_mem8(handle, addr, head_bytes, buffer);
+ if (retval == ERROR_WAIT && retries < MAX_WAIT_RETRIES) {
+ usleep((1<<retries++) * 1000);
+ continue;
+ }
+ if (retval != ERROR_OK)
+ return retval;
+ buffer += head_bytes;
+ addr += head_bytes;
+ count -= head_bytes;
+ bytes_remaining -= head_bytes;
+ }
+
+ if (bytes_remaining & (size - 1))
+ retval = stlink_usb_write_mem(handle, addr, 1, bytes_remaining, buffer);
+ else if (size == 2)
+ retval = stlink_usb_write_mem16(handle, addr, bytes_remaining, buffer);
+ else
+ retval = stlink_usb_write_mem32(handle, addr, bytes_remaining, buffer);
+
+ } else
+ retval = stlink_usb_write_mem8(handle, addr, bytes_remaining, buffer);
+ if (retval == ERROR_WAIT && retries < MAX_WAIT_RETRIES) {
+ usleep((1<<retries++) * 1000);
+ continue;
+ }
+ if (retval != ERROR_OK)
+ return retval;
+
+ buffer += bytes_remaining;
+ addr += bytes_remaining;
+ count -= bytes_remaining;
+ }
+
+ return retval;
+}
+
+/** */
+static int stlink_usb_override_target(const char *targetname)
+{
+ return !strcmp(targetname, "cortex_m");
+}
+
+static int stlink_speed_swim(void *handle, int khz, bool query)
+{
+ /*
+ we dont care what the khz rate is
+ we only have low and high speed...
+ before changing speed the SWIM_CSR HS bit
+ must be updated
+ */
+ if (khz == 0)
+ stlink_swim_speed(handle, 0);
+ else
+ stlink_swim_speed(handle, 1);
+ return khz;
+}
+
+static int stlink_match_speed_map(const struct speed_map *map, unsigned int map_size, int khz, bool query)
+{
+ unsigned int i;
+ int speed_index = -1;
+ int speed_diff = INT_MAX;
+ int last_valid_speed = -1;
+ bool match = true;
+
+ for (i = 0; i < map_size; i++) {
+ if (!map[i].speed)
+ continue;
+ last_valid_speed = i;
+ if (khz == map[i].speed) {
+ speed_index = i;
+ break;
+ } else {
+ int current_diff = khz - map[i].speed;
+ /* get abs value for comparison */
+ current_diff = (current_diff > 0) ? current_diff : -current_diff;
+ if ((current_diff < speed_diff) && khz >= map[i].speed) {
+ speed_diff = current_diff;
+ speed_index = i;
+ }
+ }
+ }
+
+ if (speed_index == -1) {
+ /* this will only be here if we cannot match the slow speed.
+ * use the slowest speed we support.*/
+ speed_index = last_valid_speed;
+ match = false;
+ } else if (i == map_size)
+ match = false;
+
+ if (!match && query) {
+ LOG_INFO("Unable to match requested speed %d kHz, using %d kHz",
+ khz, map[speed_index].speed);
+ }
+
+ return speed_index;
+}
+
+static int stlink_speed_swd(void *handle, int khz, bool query)
+{
+ int speed_index;
+ struct stlink_usb_handle_s *h = handle;
+
+ /* old firmware cannot change it */
+ if (!(h->version.flags & STLINK_F_HAS_SWD_SET_FREQ))
+ return khz;
+
+ speed_index = stlink_match_speed_map(stlink_khz_to_speed_map_swd,
+ ARRAY_SIZE(stlink_khz_to_speed_map_swd), khz, query);
+
+ if (!query) {
+ int result = stlink_usb_set_swdclk(h, stlink_khz_to_speed_map_swd[speed_index].speed_divisor);
+ if (result != ERROR_OK) {
+ LOG_ERROR("Unable to set adapter speed");
+ return khz;
+ }
+ }
+
+ return stlink_khz_to_speed_map_swd[speed_index].speed;
+}
+
+static int stlink_speed_jtag(void *handle, int khz, bool query)
+{
+ int speed_index;
+ struct stlink_usb_handle_s *h = handle;
+
+ /* old firmware cannot change it */
+ if (!(h->version.flags & STLINK_F_HAS_JTAG_SET_FREQ))
+ return khz;
+
+ speed_index = stlink_match_speed_map(stlink_khz_to_speed_map_jtag,
+ ARRAY_SIZE(stlink_khz_to_speed_map_jtag), khz, query);
+
+ if (!query) {
+ int result = stlink_usb_set_jtagclk(h, stlink_khz_to_speed_map_jtag[speed_index].speed_divisor);
+ if (result != ERROR_OK) {
+ LOG_ERROR("Unable to set adapter speed");
+ return khz;
+ }
+ }
+
+ return stlink_khz_to_speed_map_jtag[speed_index].speed;
+}
+
+void stlink_dump_speed_map(const struct speed_map *map, unsigned int map_size)
+{
+ unsigned int i;
+
+ LOG_DEBUG("Supported clock speeds are:");
+ for (i = 0; i < map_size; i++)
+ if (map[i].speed)
+ LOG_DEBUG("%d kHz", map[i].speed);
+}
+
+static int stlink_get_com_freq(void *handle, bool is_jtag, struct speed_map *map)
+{
+ struct stlink_usb_handle_s *h = handle;
+ int i;
+
+ if (h->version.jtag_api != STLINK_JTAG_API_V3) {
+ LOG_ERROR("Unknown command");
+ return 0;
+ }
+
+ stlink_usb_init_buffer(handle, h->rx_ep, 16);
+
+ h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
+ h->cmdbuf[h->cmdidx++] = STLINK_APIV3_GET_COM_FREQ;
+ h->cmdbuf[h->cmdidx++] = is_jtag ? 1 : 0;
+
+ int res = stlink_usb_xfer_errcheck(handle, h->databuf, 52);
+
+ int size = h->databuf[8];
+
+ if (size > STLINK_V3_MAX_FREQ_NB)
+ size = STLINK_V3_MAX_FREQ_NB;
+
+ for (i = 0; i < size; i++) {
+ map[i].speed = le_to_h_u32(&h->databuf[12 + 4 * i]);
+ map[i].speed_divisor = i;
+ }
+
+ /* set to zero all the next entries */
+ for (i = size; i < STLINK_V3_MAX_FREQ_NB; i++)
+ map[i].speed = 0;
+
+ return res;
+}
+
+static int stlink_set_com_freq(void *handle, bool is_jtag, unsigned int frequency)
+{
+ struct stlink_usb_handle_s *h = handle;
+
+ if (h->version.jtag_api != STLINK_JTAG_API_V3) {
+ LOG_ERROR("Unknown command");
+ return 0;
+ }
+
+ stlink_usb_init_buffer(handle, h->rx_ep, 16);
+
+ h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
+ h->cmdbuf[h->cmdidx++] = STLINK_APIV3_SET_COM_FREQ;
+ h->cmdbuf[h->cmdidx++] = is_jtag ? 1 : 0;
+ h->cmdbuf[h->cmdidx++] = 0;
+
+ h_u32_to_le(&h->cmdbuf[4], frequency);
+
+ return stlink_usb_xfer_errcheck(handle, h->databuf, 8);
+}
+
+static int stlink_speed_v3(void *handle, bool is_jtag, int khz, bool query)
+{
+ struct stlink_usb_handle_s *h = handle;
+ int speed_index;
+ struct speed_map map[STLINK_V3_MAX_FREQ_NB];
+
+ stlink_get_com_freq(h, is_jtag, map);
+
+ speed_index = stlink_match_speed_map(map, ARRAY_SIZE(map), khz, query);
+
+ if (!query) {
+ int result = stlink_set_com_freq(h, is_jtag, map[speed_index].speed);
+ if (result != ERROR_OK) {
+ LOG_ERROR("Unable to set adapter speed");
+ return khz;
+ }
+ }
+ return map[speed_index].speed;
+}
+
+static int stlink_speed(void *handle, int khz, bool query)
+{
+ struct stlink_usb_handle_s *h = handle;
+
+ if (!handle)
+ return khz;
+
+ switch (h->transport) {
+ case HL_TRANSPORT_SWIM:
+ return stlink_speed_swim(handle, khz, query);
+ case HL_TRANSPORT_SWD:
+ if (h->version.jtag_api == STLINK_JTAG_API_V3)
+ return stlink_speed_v3(handle, false, khz, query);
+ else
+ return stlink_speed_swd(handle, khz, query);
+ break;
+ case HL_TRANSPORT_JTAG:
+ if (h->version.jtag_api == STLINK_JTAG_API_V3)
+ return stlink_speed_v3(handle, true, khz, query);
+ else
+ return stlink_speed_jtag(handle, khz, query);
+ break;
+ default:
+ break;
+ }
+
+ return khz;
+}
+
+/** */
+static int stlink_usb_close(void *handle)
+{
+ int res;
+ uint8_t mode;
+ enum stlink_mode emode;
+ struct stlink_usb_handle_s *h = handle;
+
+ if (h && h->fd)
+ res = stlink_usb_current_mode(handle, &mode);
+ else
+ res = ERROR_FAIL;
+ /* do not exit if return code != ERROR_OK,
+ it prevents us from closing jtag_libusb */
+
+ if (res == ERROR_OK) {
+ /* try to exit current mode */
+ switch (mode) {
+ case STLINK_DEV_DFU_MODE:
+ emode = STLINK_MODE_DFU;
+ break;
+ case STLINK_DEV_DEBUG_MODE:
+ emode = STLINK_MODE_DEBUG_SWD;
+ break;
+ case STLINK_DEV_SWIM_MODE:
+ emode = STLINK_MODE_DEBUG_SWIM;
+ break;
+ case STLINK_DEV_BOOTLOADER_MODE:
+ case STLINK_DEV_MASS_MODE:
+ default:
+ emode = STLINK_MODE_UNKNOWN;
+ break;
+ }
+
+ if (emode != STLINK_MODE_UNKNOWN)
+ stlink_usb_mode_leave(handle, emode);
+ /* do not check return code, it prevent
+ us from closing jtag_libusb */
+ }
+
+ if (h && h->fd)
+ jtag_libusb_close(h->fd);
+
+ free(h);
+
+ return ERROR_OK;
+}
+
+/* Compute ST-Link serial number from the device descriptor
+ * this function will help to work-around a bug in old ST-Link/V2 DFU
+ * the buggy DFU returns an incorrect serial in the USB descriptor
+ * example for the following serial "57FF72067265575742132067"
+ * - the correct descriptor serial is:
+ * 0x32, 0x03, 0x35, 0x00, 0x37, 0x00, 0x46, 0x00, 0x46, 0x00, 0x37, 0x00, 0x32, 0x00 ...
+ * this contains the length (0x32 = 50), the type (0x3 = DT_STRING) and the serial in unicode format
+ * the serial part is: 0x0035, 0x0037, 0x0046, 0x0046, 0x0037, 0x0032 ... >> 57FF72 ...
+ * this format could be read correctly by 'libusb_get_string_descriptor_ascii'
+ * so this case is managed by libusb_helper::string_descriptor_equal
+ * - the buggy DFU is not doing any unicode conversion and returns a raw serial data in the descriptor
+ * 0x1a, 0x03, 0x57, 0x00, 0xFF, 0x00, 0x72, 0x00 ...
+ * >> 57 FF 72 ...
+ * based on the length (0x1a = 26) we could easily decide if we have to fixup the serial
+ * and then we have just to convert the raw data into printable characters using sprintf
+ */
+char *stlink_usb_get_alternate_serial(libusb_device_handle *device,
+ struct libusb_device_descriptor *dev_desc)
+{
+ int usb_retval;
+ unsigned char desc_serial[(STLINK_SERIAL_LEN + 1) * 2];
+
+ if (dev_desc->iSerialNumber == 0)
+ return NULL;
+
+ /* get the LANGID from String Descriptor Zero */
+ usb_retval = libusb_get_string_descriptor(device, 0, 0, desc_serial,
+ sizeof(desc_serial));
+
+ if (usb_retval < LIBUSB_SUCCESS) {
+ LOG_ERROR("libusb_get_string_descriptor() failed: %s(%d)",
+ libusb_error_name(usb_retval), usb_retval);
+ return NULL;
+ } else if (usb_retval < 4) {
+ /* the size should be least 4 bytes to contain a minimum of 1 supported LANGID */
+ LOG_ERROR("could not get the LANGID");
+ return NULL;
+ }
+
+ uint32_t langid = desc_serial[2] | (desc_serial[3] << 8);
+
+ /* get the serial */
+ usb_retval = libusb_get_string_descriptor(device, dev_desc->iSerialNumber,
+ langid, desc_serial, sizeof(desc_serial));
+
+ unsigned char len = desc_serial[0];
+
+ if (usb_retval < LIBUSB_SUCCESS) {
+ LOG_ERROR("libusb_get_string_descriptor() failed: %s(%d)",
+ libusb_error_name(usb_retval), usb_retval);
+ return NULL;
+ } else if (desc_serial[1] != LIBUSB_DT_STRING || len > usb_retval) {
+ LOG_ERROR("invalid string in ST-LINK USB serial descriptor");
+ return NULL;
+ }
+
+ if (len == ((STLINK_SERIAL_LEN + 1) * 2)) {
+ /* good ST-Link adapter, this case is managed by
+ * libusb::libusb_get_string_descriptor_ascii */
+ return NULL;
+ } else if (len != ((STLINK_SERIAL_LEN / 2 + 1) * 2)) {
+ LOG_ERROR("unexpected serial length (%d) in descriptor", len);
+ return NULL;
+ }
+
+ /* else (len == 26) => buggy ST-Link */
+
+ char *alternate_serial = malloc((STLINK_SERIAL_LEN + 1) * sizeof(char));
+ if (alternate_serial == NULL)
+ return NULL;
+
+ for (unsigned int i = 0; i < STLINK_SERIAL_LEN; i += 2)
+ sprintf(alternate_serial + i, "%02X", desc_serial[i + 2]);
+
+ alternate_serial[STLINK_SERIAL_LEN] = '\0';
+
+ return alternate_serial;
+}
+
+/** */
+static int stlink_usb_open(struct hl_interface_param_s *param, void **fd)
+{
+ int err, retry_count = 1;
+ struct stlink_usb_handle_s *h;
+
+ LOG_DEBUG("stlink_usb_open");
+
+ h = calloc(1, sizeof(struct stlink_usb_handle_s));
+
+ if (h == 0) {
+ LOG_DEBUG("malloc failed");
+ return ERROR_FAIL;
+ }
+
+ h->transport = param->transport;
+
+ for (unsigned i = 0; param->vid[i]; i++) {
+ LOG_DEBUG("transport: %d vid: 0x%04x pid: 0x%04x serial: %s",
+ param->transport, param->vid[i], param->pid[i],
+ param->serial ? param->serial : "");
+ }
+
+ /*
+ On certain host USB configurations(e.g. MacBook Air)
+ STLINKv2 dongle seems to have its FW in a funky state if,
+ after plugging it in, you try to use openocd with it more
+ then once (by launching and closing openocd). In cases like
+ that initial attempt to read the FW info via
+ stlink_usb_version will fail and the device has to be reset
+ in order to become operational.
+ */
+ do {
+ if (jtag_libusb_open(param->vid, param->pid, param->serial,
+ &h->fd, stlink_usb_get_alternate_serial) != ERROR_OK) {
+ LOG_ERROR("open failed");
+ goto error_open;
+ }
+
+ jtag_libusb_set_configuration(h->fd, 0);
+
+ if (libusb_claim_interface(h->fd, 0) != ERROR_OK) {
+ LOG_DEBUG("claim interface failed");
+ goto error_open;
+ }
+
+ /* RX EP is common for all versions */
+ h->rx_ep = STLINK_RX_EP;
+
+ uint16_t pid;
+ if (jtag_libusb_get_pid(libusb_get_device(h->fd), &pid) != ERROR_OK) {
+ LOG_DEBUG("libusb_get_pid failed");
+ goto error_open;
+ }
+
+ /* wrap version for first read */
+ switch (pid) {
+ case STLINK_V1_PID:
+ h->version.stlink = 1;
+ h->tx_ep = STLINK_TX_EP;
+ break;
+ case STLINK_V3_USBLOADER_PID:
+ case STLINK_V3E_PID:
+ case STLINK_V3S_PID:
+ case STLINK_V3_2VCP_PID:
+ h->version.stlink = 3;
+ h->tx_ep = STLINK_V2_1_TX_EP;
+ h->trace_ep = STLINK_V2_1_TRACE_EP;
+ break;
+ case STLINK_V2_1_PID:
+ case STLINK_V2_1_NO_MSD_PID:
+ h->version.stlink = 2;
+ h->tx_ep = STLINK_V2_1_TX_EP;
+ h->trace_ep = STLINK_V2_1_TRACE_EP;
+ break;
+ default:
+ /* fall through - we assume V2 to be the default version*/
+ case STLINK_V2_PID:
+ h->version.stlink = 2;
+ h->tx_ep = STLINK_TX_EP;
+ h->trace_ep = STLINK_TRACE_EP;
+ break;
+ }
+
+ /* get the device version */
+ err = stlink_usb_version(h);
+
+ if (err == ERROR_OK) {
+ break;
+ } else if (h->version.stlink == 1 ||
+ retry_count == 0) {
+ LOG_ERROR("read version failed");
+ goto error_open;
+ } else {
+ err = libusb_release_interface(h->fd, 0);
+ if (err != ERROR_OK) {
+ LOG_ERROR("release interface failed");
+ goto error_open;
+ }
+
+ err = libusb_reset_device(h->fd);
+ if (err != ERROR_OK) {
+ LOG_ERROR("reset device failed");
+ goto error_open;
+ }
+
+ jtag_libusb_close(h->fd);
+ /*
+ Give the device one second to settle down and
+ reenumerate.
+ */
+ usleep(1 * 1000 * 1000);
+ retry_count--;
+ }
+ } while (1);
+
+ /* check if mode is supported */
+ err = ERROR_OK;
+
+ switch (h->transport) {
+ case HL_TRANSPORT_SWD:
+ if (h->version.jtag_api == STLINK_JTAG_API_V1)
+ err = ERROR_FAIL;
+ /* fall-through */
+ case HL_TRANSPORT_JTAG:
+ if (h->version.jtag == 0)
+ err = ERROR_FAIL;
+ break;
+ case HL_TRANSPORT_SWIM:
+ if (h->version.swim == 0)
+ err = ERROR_FAIL;
+ break;
+ default:
+ err = ERROR_FAIL;
+ break;
+ }
+
+ if (err != ERROR_OK) {
+ LOG_ERROR("mode (transport) not supported by device");
+ goto error_open;
+ }
+
+ /* initialize the debug hardware */
+ err = stlink_usb_init_mode(h, param->connect_under_reset, param->initial_interface_speed);
+
+ if (err != ERROR_OK) {
+ LOG_ERROR("init mode failed (unable to connect to the target)");
+ goto error_open;
+ }
+
+ if (h->transport == HL_TRANSPORT_SWIM) {
+ err = stlink_swim_enter(h);
+ if (err != ERROR_OK) {
+ LOG_ERROR("stlink_swim_enter_failed (unable to connect to the target)");
+ goto error_open;
+ }
+ *fd = h;
+ h->max_mem_packet = STLINK_DATA_SIZE;
+ return ERROR_OK;
+ }
+
+ /* get cpuid, so we can determine the max page size
+ * start with a safe default */
+ h->max_mem_packet = (1 << 10);
+
+ uint8_t buffer[4];
+ err = stlink_usb_read_mem32(h, CPUID, 4, buffer);
+ if (err == ERROR_OK) {
+ uint32_t cpuid = le_to_h_u32(buffer);
+ int i = (cpuid >> 4) & 0xf;
+ if (i == 4 || i == 3) {
+ /* Cortex-M3/M4 has 4096 bytes autoincrement range */
+ h->max_mem_packet = (1 << 12);
+ }
+ }
+
+ LOG_DEBUG("Using TAR autoincrement: %" PRIu32, h->max_mem_packet);
+
+ *fd = h;
+
+ return ERROR_OK;
+
+error_open:
+ stlink_usb_close(h);
+
+ return ERROR_FAIL;
+}
+
+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)
+{
+ struct stlink_usb_handle_s *h = handle;
+ uint16_t presc;
+
+ if (enabled && (!(h->version.flags & STLINK_F_HAS_TRACE) ||
+ pin_protocol != TPIU_PIN_PROTOCOL_ASYNC_UART)) {
+ LOG_ERROR("The attached ST-LINK version doesn't support this trace mode");
+ return ERROR_FAIL;
+ }
+
+ if (!enabled) {
+ stlink_usb_trace_disable(h);
+ return ERROR_OK;
+ }
+
+ if (*trace_freq > STLINK_TRACE_MAX_HZ) {
+ LOG_ERROR("ST-LINK doesn't support SWO frequency higher than %u",
+ STLINK_TRACE_MAX_HZ);
+ return ERROR_FAIL;
+ }
+
+ stlink_usb_trace_disable(h);
+
+ if (!*trace_freq)
+ *trace_freq = STLINK_TRACE_MAX_HZ;
+
+ presc = traceclkin_freq / *trace_freq;
+
+ if (traceclkin_freq % *trace_freq > 0)
+ presc++;
+
+ if (presc > TPIU_ACPR_MAX_SWOSCALER) {
+ LOG_ERROR("SWO frequency is not suitable. Please choose a different "
+ "frequency.");
+ return ERROR_FAIL;
+ }
+
+ *prescaler = presc;
+ h->trace.source_hz = *trace_freq;
+
+ return stlink_usb_trace_enable(h);
+}
+
+/** */
+static int stlink_usb_init_access_port(void *handle, unsigned char ap_num)
+{
+ struct stlink_usb_handle_s *h = handle;
+
+ assert(handle != NULL);
+
+ if (!(h->version.flags & STLINK_F_HAS_AP_INIT))
+ return ERROR_COMMAND_NOTFOUND;
+
+ LOG_DEBUG_IO("init ap_num = %d", ap_num);
+ stlink_usb_init_buffer(handle, h->rx_ep, 16);
+ h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
+ h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_INIT_AP;
+ h->cmdbuf[h->cmdidx++] = ap_num;
+
+ return stlink_usb_xfer_errcheck(handle, h->databuf, 2);
+}
+
+/** */
+static int stlink_usb_close_access_port(void *handle, unsigned char ap_num)
+{
+ struct stlink_usb_handle_s *h = handle;
+
+ assert(handle != NULL);
+
+ if (!(h->version.flags & STLINK_F_HAS_AP_INIT))
+ return ERROR_COMMAND_NOTFOUND;
+
+ LOG_DEBUG_IO("close ap_num = %d", ap_num);
+ stlink_usb_init_buffer(handle, h->rx_ep, 16);
+ h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
+ h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_CLOSE_AP_DBG;
+ h->cmdbuf[h->cmdidx++] = ap_num;
+
+ return stlink_usb_xfer_errcheck(handle, h->databuf, 2);
+}
+
+/** */
+static int stlink_read_dap_register(void *handle, unsigned short dap_port,
+ unsigned short addr, uint32_t *val)
+{
+ struct stlink_usb_handle_s *h = handle;
+ int retval;
+
+ assert(handle != NULL);
+
+ if (!(h->version.flags & STLINK_F_HAS_DAP_REG))
+ return ERROR_COMMAND_NOTFOUND;
+
+ stlink_usb_init_buffer(handle, h->rx_ep, 16);
+ h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
+ h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_READ_DAP_REG;
+ h_u16_to_le(&h->cmdbuf[2], dap_port);
+ h_u16_to_le(&h->cmdbuf[4], addr);
+
+ retval = stlink_usb_xfer_errcheck(handle, h->databuf, 8);
+ *val = le_to_h_u32(h->databuf + 4);
+ LOG_DEBUG_IO("dap_port_read = %d, addr = 0x%x, value = 0x%x", dap_port, addr, *val);
+ return retval;
+}
+
+/** */
+static int stlink_write_dap_register(void *handle, unsigned short dap_port,
+ unsigned short addr, uint32_t val)
+{
+ struct stlink_usb_handle_s *h = handle;
+
+ assert(handle != NULL);
+
+ if (!(h->version.flags & STLINK_F_HAS_DAP_REG))
+ return ERROR_COMMAND_NOTFOUND;
+
+ LOG_DEBUG_IO("dap_write port = %d, addr = 0x%x, value = 0x%x", dap_port, addr, val);
+ stlink_usb_init_buffer(handle, h->rx_ep, 16);
+ h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
+ h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_WRITE_DAP_REG;
+ h_u16_to_le(&h->cmdbuf[2], dap_port);
+ h_u16_to_le(&h->cmdbuf[4], addr);
+ h_u32_to_le(&h->cmdbuf[6], val);
+ return stlink_usb_xfer_errcheck(handle, h->databuf, 2);
+}
+
+/** */
+struct hl_layout_api_s stlink_usb_layout_api = {
+ /** */
+ .open = stlink_usb_open,
+ /** */
+ .close = stlink_usb_close,
+ /** */
+ .idcode = stlink_usb_idcode,
+ /** */
+ .state = stlink_usb_state,
+ /** */
+ .reset = stlink_usb_reset,
+ /** */
+ .assert_srst = stlink_usb_assert_srst,
+ /** */
+ .run = stlink_usb_run,
+ /** */
+ .halt = stlink_usb_halt,
+ /** */
+ .step = stlink_usb_step,
+ /** */
+ .read_regs = stlink_usb_read_regs,
+ /** */
+ .read_reg = stlink_usb_read_reg,
+ /** */
+ .write_reg = stlink_usb_write_reg,
+ /** */
+ .read_mem = stlink_usb_read_mem,
+ /** */
+ .write_mem = stlink_usb_write_mem,
+ /** */
+ .write_debug_reg = stlink_usb_write_debug_reg,
+ /** */
+ .override_target = stlink_usb_override_target,
+ /** */
+ .speed = stlink_speed,
+ /** */
+ .config_trace = stlink_config_trace,
+ /** */
+ .poll_trace = stlink_usb_trace_read,
+};
+
+/*****************************************************************************
+ * DAP direct interface
+ */
+
+static struct stlink_usb_handle_s *stlink_dap_handle;
+static struct hl_interface_param_s stlink_dap_param;
+static DECLARE_BITMAP(opened_ap, DP_APSEL_MAX + 1);
+static int stlink_dap_error = ERROR_OK;