Sten <debian@sansys-electronic.com>: add support for ICEbear FDTI-based interface.
[openocd.git] / src / jtag / jlink.c
index 44a1ad6d723e402fbb15cbd6fd27b7c40cd18f15..d2e8947c12b635efe77f66eb5180cc4e9c745ebc 100644 (file)
@@ -31,6 +31,7 @@
 
 #include <usb.h>
 #include <string.h>
+#include <errno.h>
 
 #include "log.h"
 
@@ -44,7 +45,7 @@
 
 // See Section 1.3.2 of the Segger JLink USB protocol manual
 #define JLINK_IN_BUFFER_SIZE                   2048
-#define JLINK_OUT_BUFFER_SIZE                  2048
+#define JLINK_OUT_BUFFER_SIZE                  2*2048+4
 #define JLINK_EMU_RESULT_BUFFER_SIZE   64
 
 /* Global USB buffers */
@@ -53,14 +54,16 @@ static u8 usb_out_buffer[JLINK_OUT_BUFFER_SIZE];
 static u8 usb_emu_result_buffer[JLINK_EMU_RESULT_BUFFER_SIZE];
 
 /* Constants for JLink command */
-#define EMU_CMD_VERSION     0x01
-#define EMU_CMD_SET_SPEED   0x05
-#define EMU_CMD_GET_STATE   0x07
-#define EMU_CMD_HW_JTAG3    0xcf
-#define EMU_CMD_HW_RESET0   0xdc
-#define EMU_CMD_HW_RESET1   0xdd
-#define EMU_CMD_HW_TRST0    0xde
-#define EMU_CMD_HW_TRST1    0xdf
+#define EMU_CMD_VERSION                0x01
+#define EMU_CMD_SET_SPEED              0x05
+#define EMU_CMD_GET_STATE              0x07
+#define EMU_CMD_HW_JTAG3               0xcf
+#define EMU_CMD_GET_MAX_MEM_BLOCK   0xd4
+#define EMU_CMD_HW_RESET0              0xdc
+#define EMU_CMD_HW_RESET1              0xdd
+#define EMU_CMD_HW_TRST0               0xde
+#define EMU_CMD_HW_TRST1               0xdf
+#define EMU_CMD_GET_CAPS               0xe8
 
 /* max speed 12MHz v5.0 jlink */
 #define JLINK_MAX_SPEED 12000
@@ -114,6 +117,8 @@ static int jlink_get_version_info(void);
 static void jlink_debug_buffer(u8 *buffer, int length);
 #endif
 
+static enum tap_state jlink_last_state = TAP_RESET;
+
 static jlink_jtag_t* jlink_jtag_handle;
 
 /***************************************************************************/
@@ -329,6 +334,7 @@ static int jlink_init(void)
 
        jlink_reset(0, 0);
        jlink_tap_init();
+       jlink_speed(jtag_speed);
 
        return ERROR_OK;
 }
@@ -527,6 +533,7 @@ static int jlink_get_version_info(void)
 {
        int result;
        int len;
+       u32 jlink_caps, jlink_max_size;
 
        /* query hardware version */
        jlink_simple_command(EMU_CMD_VERSION);
@@ -534,8 +541,7 @@ static int jlink_get_version_info(void)
        result = jlink_usb_read(jlink_jtag_handle, 2);
        if (2 != result)
        {
-               LOG_ERROR("J-Link command EMU_CMD_VERSION failed (%d)\n",
-                               result);
+               LOG_ERROR("J-Link command EMU_CMD_VERSION failed (%d)\n", result);
                return ERROR_JTAG_DEVICE_ERROR;
        }
 
@@ -543,14 +549,41 @@ static int jlink_get_version_info(void)
        result = jlink_usb_read(jlink_jtag_handle, len);
        if (result != len)
        {
-               LOG_ERROR("J-Link command EMU_CMD_VERSION failed (%d)\n",
-                               result);
+               LOG_ERROR("J-Link command EMU_CMD_VERSION failed (%d)\n", result);
                return ERROR_JTAG_DEVICE_ERROR;
        }
 
        usb_in_buffer[result] = 0;
        LOG_INFO("%s", (char *)usb_in_buffer);
 
+       /* query hardware capabilities */
+       jlink_simple_command(EMU_CMD_GET_CAPS);
+
+       result = jlink_usb_read(jlink_jtag_handle, 4);
+       if (4 != result)
+       {
+               LOG_ERROR("J-Link command EMU_CMD_GET_CAPS failed (%d)\n", result);
+               return ERROR_JTAG_DEVICE_ERROR;
+       }
+
+       jlink_caps = buf_get_u32(usb_in_buffer, 0, 32);
+       LOG_INFO("JLink caps 0x%x", jlink_caps);
+
+
+       /* query hardware maximum memory block */
+       jlink_simple_command(EMU_CMD_GET_MAX_MEM_BLOCK);
+
+       result = jlink_usb_read(jlink_jtag_handle, 4);
+       if (4 != result)
+       {
+               LOG_ERROR("J-Link command EMU_CMD_GET_MAX_MEM_BLOCK failed (%d)\n", result);
+               return ERROR_JTAG_DEVICE_ERROR;
+       }
+
+       jlink_max_size = buf_get_u32(usb_in_buffer, 0, 32);
+       LOG_INFO("JLink max mem block %i", jlink_max_size);
+
+
        return ERROR_OK;
 }
 
@@ -589,8 +622,6 @@ typedef struct
 static int pending_scan_results_length;
 static pending_scan_result_t pending_scan_results_buffer[MAX_PENDING_SCAN_RESULTS];
 
-static int last_tms;
-
 static void jlink_tap_init(void)
 {
        tap_length = 0;
@@ -610,7 +641,6 @@ static void jlink_tap_ensure_space(int scans, int bits)
 
 static void jlink_tap_append_step(int tms, int tdi)
 {
-       last_tms = tms;
        int index = tap_length / 8;
 
        if (index >= JLINK_TAP_BUFFER_SIZE)
@@ -622,6 +652,10 @@ static void jlink_tap_append_step(int tms, int tdi)
        int bit_index = tap_length % 8;
        u8 bit = 1 << bit_index;
 
+       // we do not pad TMS, so be sure to initialize all bits
+       if (0 == bit_index)
+               tms_buffer[index] = tdi_buffer[index] = 0;
+
        if (tms)
                tms_buffer[index] |= bit;
        else
@@ -660,52 +694,34 @@ static void jlink_tap_append_scan(int length, u8 *buffer, scan_command_t *comman
 static int jlink_tap_execute(void)
 {
        int byte_length;
-       int tms_offset;
-       int tdi_offset;
        int i;
        int result;
 
        if (!tap_length)
                return ERROR_OK;
 
-       /* Pad last byte so that tap_length is divisible by 8 */
-       while (tap_length % 8 != 0)
-       {
-               /* More of the last TMS value keeps us in the same state,
-                * analogous to free-running JTAG interfaces. */
-               jlink_tap_append_step(last_tms, 0);
-       }
-
-       byte_length = tap_length / 8;
+       // number of full bytes (plus one if some would be left over)
+       byte_length = TAP_SCAN_BYTES(tap_length);
 
        usb_out_buffer[0] = EMU_CMD_HW_JTAG3;
        usb_out_buffer[1] = 0;
        usb_out_buffer[2] = (tap_length >> 0) & 0xff;
        usb_out_buffer[3] = (tap_length >> 8) & 0xff;
+       memcpy(usb_out_buffer + 4, tms_buffer, byte_length);
+       memcpy(usb_out_buffer + 4 + byte_length, tdi_buffer, byte_length);
 
-       tms_offset = 4;
-       for (i = 0; i < byte_length; i++)
-       {
-               usb_out_buffer[tms_offset + i] = tms_buffer[i];
-       }
-
-       tdi_offset = tms_offset + byte_length;
-       for (i = 0; i < byte_length; i++)
-       {
-               usb_out_buffer[tdi_offset + i] = tdi_buffer[i];
-       }
+       jlink_last_state = jtag_debug_state_machine(tms_buffer, tdi_buffer,
+                       tap_length, jlink_last_state);
 
        result = jlink_usb_message(jlink_jtag_handle, 4 + 2 * byte_length, byte_length);
-
        if (result != byte_length)
        {
-               LOG_ERROR("jlink_tap_execute, wrong result %d (expected %d)",
-                               result, byte_length);
+               LOG_ERROR("jlink_tap_execute, wrong result %d (expected %d)", result, byte_length);
+               jlink_tap_init();
                return ERROR_JTAG_QUEUE_FAILED;
        }
 
-       for (i = 0; i < byte_length; i++)
-               tdo_buffer[i] = usb_in_buffer[i];
+       memcpy(tdo_buffer, usb_in_buffer, byte_length);
 
        for (i = 0; i < pending_scan_results_length; i++)
        {
@@ -737,7 +753,6 @@ static int jlink_tap_execute(void)
        }
 
        jlink_tap_init();
-
        return ERROR_OK;
 }
 
@@ -811,7 +826,7 @@ static int jlink_usb_message(jlink_jtag_t *jlink_jtag, int out_length, int in_le
        }
 
        result = jlink_usb_read(jlink_jtag, in_length);
-       if ((result != in_length) && (result != in_length + 1))
+       if ((result != in_length) && (result != (in_length + 1)))
        {
                LOG_ERROR("usb_bulk_read failed (requested=%d, result=%d)",
                                in_length, result);
@@ -853,28 +868,33 @@ static int usb_bulk_with_retries(
                usb_dev_handle *dev, int ep,
                char *bytes, int size, int timeout)
 {
-       int rc = 0, tries = 3, this_size;
+       int tries = 3, count = 0;
 
-       while (tries && size) {
-
-               this_size = f(dev, ep, bytes, size, timeout);
-               if (this_size > 0) {
-                       
-                       size -= this_size;
-                       rc += this_size;
-                       bytes += this_size;
-
-               } else
-                       tries --;
+       while (tries && (count < size))
+       {
+               int result = f(dev, ep, bytes + count, size - count, timeout);
+               if (result > 0)
+                       count += result;
+               else if ((-ETIMEDOUT != result) || !--tries)
+                       return result;
        }
-       return rc;
+       return count;
+}
+
+static int wrap_usb_bulk_write(usb_dev_handle *dev, int ep,
+                              char *buff, int size, int timeout)
+{
+       /* usb_bulk_write() takes const char *buff */
+       return usb_bulk_write(dev, ep, buff, size, timeout);
 }
+
 static inline int usb_bulk_write_ex(usb_dev_handle *dev, int ep,
                char *bytes, int size, int timeout)
 {
-       return usb_bulk_with_retries(&usb_bulk_write,
+       return usb_bulk_with_retries(&wrap_usb_bulk_write,
                        dev, ep, bytes, size, timeout);
 }
+
 static inline int usb_bulk_read_ex(usb_dev_handle *dev, int ep,
                char *bytes, int size, int timeout)
 {

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)