stlink_v2; Force version 1 API for now
authorKeith Packard <keithp@keithp.com>
Sun, 25 Jan 2015 00:45:08 +0000 (16:45 -0800)
committerKeith Packard <keithp@keithp.com>
Sun, 25 Jan 2015 00:45:08 +0000 (16:45 -0800)
I'm not sure what this does, but I've forced an older version of the
API, which seems to help a bit. sometimes.

Signed-off-by: Keith Packard <keithp@keithp.com>
src/jtag/drivers/stlink_usb.c

index 47a9b1233fd91f3addbe029a18e8e41309688b4b..c6a7a92ca3b088c5049c52e668e41fe241ce40d8 100644 (file)
@@ -382,7 +382,7 @@ static const char * const stlink_usb_error_msg[] = {
 };
 
 /** */
-static int stlink_usb_error_check(void *handle)
+static int stlink_usb_error_check(void *handle, int api)
 {
        int res;
        const char *err_msg = 0;
@@ -391,7 +391,7 @@ static int stlink_usb_error_check(void *handle)
        assert(handle != NULL);
 
        /* TODO: no error checking yet on api V1 */
-       if (h->jtag_api == STLINK_JTAG_API_V1)
+       if (api == STLINK_JTAG_API_V1)
                h->databuf[0] = STLINK_DEBUG_ERR_OK;
 
        switch (h->databuf[0]) {
@@ -516,6 +516,9 @@ static int stlink_usb_mode_enter(void *handle, enum stlink_mode type)
        int res;
        int rx_size = 0;
        struct stlink_usb_handle_s *h = handle;
+       int api = h->jtag_api;
+
+       api = 1;
 
        assert(handle != NULL);
 
@@ -523,44 +526,50 @@ static int stlink_usb_mode_enter(void *handle, enum stlink_mode type)
         * status
         * TODO: we need the test on api V1 too
         */
-       if (h->jtag_api == STLINK_JTAG_API_V2)
+       if (api == STLINK_JTAG_API_V2)
                rx_size = 2;
 
        stlink_usb_init_buffer(handle, STLINK_RX_EP, rx_size);
 
        switch (type) {
-               case STLINK_MODE_DEBUG_JTAG:
-                       h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
-                       if (h->jtag_api == STLINK_JTAG_API_V1)
-                               h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_ENTER;
-                       else
-                               h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_ENTER;
-                       h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_ENTER_JTAG;
-                       break;
-               case STLINK_MODE_DEBUG_SWD:
-                       h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
-                       if (h->jtag_api == STLINK_JTAG_API_V1)
-                               h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_ENTER;
-                       else
-                               h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_ENTER;
-                       h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_ENTER_SWD;
-                       break;
-               case STLINK_MODE_DEBUG_SWIM:
-                       h->cmdbuf[h->cmdidx++] = STLINK_SWIM_COMMAND;
-                       h->cmdbuf[h->cmdidx++] = STLINK_SWIM_ENTER;
-                       break;
-               case STLINK_MODE_DFU:
-               case STLINK_MODE_MASS:
-               default:
-                       return ERROR_FAIL;
+       case STLINK_MODE_DEBUG_JTAG:
+               h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
+               if (api == STLINK_JTAG_API_V1)
+                       h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_ENTER;
+               else
+                       h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_ENTER;
+               h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_ENTER_JTAG;
+               break;
+       case STLINK_MODE_DEBUG_SWD:
+               h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
+               if (api == STLINK_JTAG_API_V1)
+                       h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_ENTER;
+               else
+                       h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_ENTER;
+               h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_ENTER_SWD;
+               break;
+       case STLINK_MODE_DEBUG_SWIM:
+               h->cmdbuf[h->cmdidx++] = STLINK_SWIM_COMMAND;
+               h->cmdbuf[h->cmdidx++] = STLINK_SWIM_ENTER;
+               break;
+       case STLINK_MODE_DFU:
+       case STLINK_MODE_MASS:
+       default:
+               return ERROR_FAIL;
        }
 
+       LOG_DEBUG("mode enter command: (%d bytes)", h->cmdidx);
+       for (res = 0; res < h->cmdidx; res++)
+               LOG_DEBUG("\t%02x", h->cmdbuf[res]);
        res = stlink_usb_xfer(handle, h->databuf, rx_size);
+       LOG_DEBUG("mode enter result: (%d bytes)", rx_size);
+       for (res = 0; res < rx_size; res++)
+               LOG_DEBUG("\t%02x", h->databuf[res]);
 
        if (res != ERROR_OK)
                return res;
 
-       res = stlink_usb_error_check(h);
+       res = stlink_usb_error_check(h, api);
 
        return res;
 }
@@ -674,8 +683,6 @@ static int stlink_usb_init_mode(void *handle, bool connect_under_reset)
                }
        }
 
-       LOG_DEBUG("MODE: 0x%02X", mode);
-
        /* set selected mode */
        switch (h->transport) {
                case HL_TRANSPORT_SWD:
@@ -692,6 +699,8 @@ static int stlink_usb_init_mode(void *handle, bool connect_under_reset)
                        break;
        }
 
+       LOG_DEBUG("MODE: 0x%02X -> 0x%02x under reset %d", mode, emode, connect_under_reset);
+
        if (emode == STLINK_MODE_UNKNOWN) {
                LOG_ERROR("selected mode (transport) not supported");
                return ERROR_FAIL;
@@ -1604,9 +1613,10 @@ static int stlink_usb_open(struct hl_interface_param_s *param, void **fd)
                                LOG_DEBUG("mode %d", mode);
                                break;
                        }
-                       LOG_ERROR("current mode failed");
-                       if (++times == 3)
+                       if (++times == 3) {
+                               LOG_ERROR("current mode failed");
                                goto error_open;
+                       }
                }
 
                enum stlink_mode emode;