static void stlink_usb_init_buffer(void *handle, uint8_t direction, uint32_t size);
static int stlink_swim_status(void *handle);
+void stlink_dump_speed_map(const struct speed_map *map, unsigned int map_size);
+static int stlink_get_com_freq(void *handle, bool is_jtag, struct speed_map *map);
+static int stlink_speed(void *handle, int khz, bool query);
/** */
static unsigned int stlink_usb_block(void *handle)
}
/** */
-static int stlink_usb_init_mode(void *handle, bool connect_under_reset)
+static int stlink_usb_init_mode(void *handle, bool connect_under_reset, int initial_interface_speed)
{
int res;
uint8_t mode;
return ERROR_FAIL;
}
+ /* set the speed before entering the mode, as the chip discovery phase should be done at this speed too */
+ if (h->transport == HL_TRANSPORT_JTAG) {
+ if (h->version.flags & STLINK_F_HAS_JTAG_SET_FREQ) {
+ stlink_dump_speed_map(stlink_khz_to_speed_map_jtag, ARRAY_SIZE(stlink_khz_to_speed_map_jtag));
+ stlink_speed(h, initial_interface_speed, false);
+ }
+ } else if (h->transport == HL_TRANSPORT_SWD) {
+ if (h->version.flags & STLINK_F_HAS_SWD_SET_FREQ) {
+ stlink_dump_speed_map(stlink_khz_to_speed_map_swd, ARRAY_SIZE(stlink_khz_to_speed_map_swd));
+ stlink_speed(h, initial_interface_speed, false);
+ }
+ }
+
+ if (h->version.jtag_api == STLINK_JTAG_API_V3) {
+ struct speed_map map[STLINK_V3_MAX_FREQ_NB];
+
+ stlink_get_com_freq(h, (h->transport == HL_TRANSPORT_JTAG), map);
+ stlink_dump_speed_map(map, ARRAY_SIZE(map));
+ stlink_speed(h, initial_interface_speed, false);
+ }
+
/* preliminary SRST assert:
* We want SRST is asserted before activating debug signals (mode_enter).
* As the required mode has not been set, the adapter may not know what pin to use.
/** */
static int stlink_usb_idcode(void *handle, uint32_t *idcode)
{
- int res;
+ int res, offset;
struct stlink_usb_handle_s *h = handle;
assert(handle != NULL);
return ERROR_OK;
}
- stlink_usb_init_buffer(handle, h->rx_ep, 4);
+ stlink_usb_init_buffer(handle, h->rx_ep, 12);
h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
- h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_READCOREID;
+ if (h->version.jtag_api == STLINK_JTAG_API_V1) {
+ h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_READCOREID;
- res = stlink_usb_xfer_noerrcheck(handle, h->databuf, 4);
+ res = stlink_usb_xfer_noerrcheck(handle, h->databuf, 4);
+ offset = 0;
+ } else {
+ h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_READ_IDCODES;
+
+ res = stlink_usb_xfer_errcheck(handle, h->databuf, 12);
+ offset = 4;
+ }
if (res != ERROR_OK)
return res;
- *idcode = le_to_h_u32(h->databuf);
+ *idcode = le_to_h_u32(h->databuf + offset);
LOG_DEBUG("IDCODE: 0x%08" PRIX32, *idcode);
}
/* initialize the debug hardware */
- err = stlink_usb_init_mode(h, param->connect_under_reset);
+ 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)");
return ERROR_OK;
}
- if (h->transport == HL_TRANSPORT_JTAG) {
- if (h->version.flags & STLINK_F_HAS_JTAG_SET_FREQ) {
- stlink_dump_speed_map(stlink_khz_to_speed_map_jtag, ARRAY_SIZE(stlink_khz_to_speed_map_jtag));
- stlink_speed(h, param->initial_interface_speed, false);
- }
- } else if (h->transport == HL_TRANSPORT_SWD) {
- if (h->version.flags & STLINK_F_HAS_SWD_SET_FREQ) {
- stlink_dump_speed_map(stlink_khz_to_speed_map_swd, ARRAY_SIZE(stlink_khz_to_speed_map_swd));
- stlink_speed(h, param->initial_interface_speed, false);
- }
- }
-
- if (h->version.jtag_api == STLINK_JTAG_API_V3) {
- struct speed_map map[STLINK_V3_MAX_FREQ_NB];
-
- stlink_get_com_freq(h, (h->transport == HL_TRANSPORT_JTAG), map);
- stlink_dump_speed_map(map, ARRAY_SIZE(map));
- stlink_speed(h, param->initial_interface_speed, false);
- }
-
/* get cpuid, so we can determine the max page size
* start with a safe default */
h->max_mem_packet = (1 << 10);