+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;
+
+ while (count) {
+
+ bytes_remaining = (size == 4) ? \
+ stlink_max_block_size(h->max_mem_packet, addr) : STLINK_MAX_RW8;
+
+ if (count < bytes_remaining)
+ bytes_remaining = count;
+
+ /* the stlink only supports 8/32bit memory read/writes
+ * honour 32bit, all others will be handled as 8bit access */
+ if (size == 4) {
+
+ /* When in jtag mode the stlink uses the auto-increment functinality.
+ * 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 implementiong 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 % 4) {
+
+ uint32_t head_bytes = 4 - (addr % 4);
+ 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 % 4)
+ retval = stlink_usb_read_mem(handle, addr, 1, 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;
+
+ while (count) {
+
+ bytes_remaining = (size == 4) ? \
+ stlink_max_block_size(h->max_mem_packet, addr) : STLINK_MAX_RW8;
+
+ if (count < bytes_remaining)
+ bytes_remaining = count;
+
+ /* the stlink only supports 8/32bit memory read/writes
+ * honour 32bit, all others will be handled as 8bit access */
+ if (size == 4) {
+
+ /* When in jtag mode the stlink uses the auto-increment functinality.
+ * 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 implementiong 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 % 4) {
+
+ uint32_t head_bytes = 4 - (addr % 4);
+ 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 % 4)
+ retval = stlink_usb_write_mem(handle, addr, 1, 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(void *handle, int khz, bool query)
+{
+ unsigned i;
+ int speed_index = -1;
+ int speed_diff = INT_MAX;
+ struct stlink_usb_handle_s *h = handle;
+
+ /* only supported by stlink/v2 and for firmware >= 22 */
+ if (h && (h->version.stlink == 1 || h->version.jtag < 22))
+ return khz;
+
+ for (i = 0; i < ARRAY_SIZE(stlink_khz_to_speed_map); i++) {
+ if (khz == stlink_khz_to_speed_map[i].speed) {
+ speed_index = i;
+ break;
+ } else {
+ int current_diff = khz - stlink_khz_to_speed_map[i].speed;
+ /* get abs value for comparison */
+ current_diff = (current_diff > 0) ? current_diff : -current_diff;
+ if ((current_diff < speed_diff) && khz >= stlink_khz_to_speed_map[i].speed) {
+ speed_diff = current_diff;
+ speed_index = i;
+ }
+ }
+ }
+
+ bool match = true;
+
+ if (speed_index == -1) {
+ /* this will only be here if we cannot match the slow speed.
+ * use the slowest speed we support.*/
+ speed_index = ARRAY_SIZE(stlink_khz_to_speed_map) - 1;
+ match = false;
+ } else if (i == ARRAY_SIZE(stlink_khz_to_speed_map))
+ match = false;
+
+ if (!match && query) {
+ LOG_INFO("Unable to match requested speed %d kHz, using %d kHz", \
+ khz, stlink_khz_to_speed_map[speed_index].speed);
+ }