stlink: Use callback to increase frequency of trace data sampling
[openocd.git] / src / jtag / drivers / stlink_usb.c
index 597df6d00e8b26e47f6b9af977ee5a265af76a29..06ec4b7028ef3cf788cca9be452c077fd9684e26 100644 (file)
@@ -738,7 +738,7 @@ static int stlink_usb_idcode(void *handle, uint32_t *idcode)
 
        *idcode = le_to_h_u32(h->databuf);
 
-       LOG_DEBUG("IDCODE: 0x%08X", *idcode);
+       LOG_DEBUG("IDCODE: 0x%08" PRIX32, *idcode);
 
        return ERROR_OK;
 }
@@ -830,6 +830,12 @@ static void stlink_usb_trace_read(void *handle)
        }
 }
 
+static int stlink_usb_trace_read_callback(void *handle)
+{
+       stlink_usb_trace_read(handle);
+       return ERROR_OK;
+}
+
 static enum target_state stlink_usb_v2_get_status(void *handle)
 {
        int result;
@@ -1006,8 +1012,10 @@ static void stlink_usb_trace_disable(void *handle)
        h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_STOP_TRACE_RX;
        res = stlink_usb_xfer(handle, h->databuf, 2);
 
-       if (res == ERROR_OK)
+       if (res == ERROR_OK) {
                h->trace.enabled = false;
+               target_unregister_timer_callback(stlink_usb_trace_read_callback, handle);
+       }
 }
 
 
@@ -1043,7 +1051,15 @@ static int stlink_usb_trace_enable(void *handle)
 
                if (res == ERROR_OK)  {
                        h->trace.enabled = true;
-                       LOG_DEBUG("Tracing: recording at %uHz\n", trace_hz);
+                       LOG_DEBUG("Tracing: recording at %" PRIu32 "Hz\n", trace_hz);
+                       /* We need the trace read function to be called at a
+                        * high-enough frequency to ensure reasonable
+                        * "timeliness" in processing ITM/DWT data.
+                        * TODO: An alternative could be using the asynchronous
+                        * features of the libusb-1.0 API to queue up one or more
+                        * reads in advance and requeue them once they are
+                        * completed. */
+                       target_register_timer_callback(stlink_usb_trace_read_callback, 1, 1, handle);
                }
        } else {
                LOG_ERROR("Tracing is not supported by this version.");
@@ -1065,7 +1081,7 @@ static int stlink_usb_run(void *handle)
                res = stlink_usb_write_debug_reg(handle, DCB_DHCSR, DBGKEY|C_DEBUGEN);
 
                /* Try to start tracing, if requested */
-               if (res == ERROR_OK && h->trace.source_hz) {
+               if (res == ERROR_OK && h->trace.source_hz && !h->trace.enabled) {
                        if (stlink_usb_trace_enable(handle) == ERROR_OK)
                                LOG_DEBUG("Tracing: enabled\n");
                        else

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)