arm_adi_v5: replace dap_lookup_cs_component()
[openocd.git] / src / target / armv7m_trace.c
index 62f0f8e61db7a1a47de4877b509e323d50c496d6..74ffaf5a4b100a025c868552aab5b49d1eb2da50 100644 (file)
 #include <target/cortex_m.h>
 #include <target/armv7m_trace.h>
 #include <jtag/interface.h>
+#include <helper/time_support.h>
 
-#define TRACE_BUF_SIZE 4096
-
-static int armv7m_poll_trace(void *target)
-{
-       struct armv7m_common *armv7m = target_to_armv7m(target);
-       uint8_t buf[TRACE_BUF_SIZE];
-       size_t size = sizeof(buf);
-       int retval;
-
-       retval = adapter_poll_trace(buf, &size);
-       if (retval != ERROR_OK || !size)
-               return retval;
-
-       target_call_trace_callbacks(target, size, buf);
-
-       if (armv7m->trace_config.trace_file != NULL) {
-               if (fwrite(buf, 1, size, armv7m->trace_config.trace_file) == size)
-                       fflush(armv7m->trace_config.trace_file);
-               else {
-                       LOG_ERROR("Error writing to the trace destination file");
-                       return ERROR_FAIL;
-               }
-       }
-
-       return ERROR_OK;
-}
-
-int armv7m_trace_tpiu_config(struct target *target)
+int armv7m_trace_itm_config(struct target *target)
 {
        struct armv7m_common *armv7m = target_to_armv7m(target);
        struct armv7m_trace_config *trace_config = &armv7m->trace_config;
-       int prescaler;
        int retval;
 
-       target_unregister_timer_callback(armv7m_poll_trace, target);
-
-
-       retval = adapter_config_trace(trace_config->config_type == TRACE_CONFIG_TYPE_INTERNAL,
-                                     trace_config->pin_protocol,
-                                     trace_config->port_size,
-                                     &trace_config->trace_freq);
-       if (retval != ERROR_OK)
-               return retval;
-
-       if (!trace_config->trace_freq) {
-               LOG_ERROR("Trace port frequency is 0, can't enable TPIU");
-               return ERROR_FAIL;
-       }
-
-       prescaler = trace_config->traceclkin_freq / trace_config->trace_freq;
-
-       if (trace_config->traceclkin_freq % trace_config->trace_freq) {
-               prescaler++;
-               int trace_freq = trace_config->traceclkin_freq / prescaler;
-               LOG_INFO("Can not obtain %u trace port frequency from %u TRACECLKIN frequency, using %u instead",
-                         trace_config->trace_freq, trace_config->traceclkin_freq,
-                         trace_freq);
-               trace_config->trace_freq = trace_freq;
-               retval = adapter_config_trace(trace_config->config_type == TRACE_CONFIG_TYPE_INTERNAL,
-                                             trace_config->pin_protocol,
-                                             trace_config->port_size,
-                                             &trace_config->trace_freq);
-               if (retval != ERROR_OK)
-                       return retval;
-       }
-
-       retval = target_write_u32(target, TPIU_CSPSR, 1 << trace_config->port_size);
-       if (retval != ERROR_OK)
-               return retval;
-
-       retval = target_write_u32(target, TPIU_ACPR, prescaler - 1);
-       if (retval != ERROR_OK)
-               return retval;
-
-       retval = target_write_u32(target, TPIU_SPPR, trace_config->pin_protocol);
+       retval = target_write_u32(target, ITM_LAR, ITM_LAR_KEY);
        if (retval != ERROR_OK)
                return retval;
 
-       uint32_t ffcr;
-       retval = target_read_u32(target, TPIU_FFCR, &ffcr);
+       /* pg315 of CoreSight Components
+        * It is recommended that the ITMEn bit is cleared and waits for the
+        * ITMBusy bit to be cleared, before changing any fields in the
+        * Control Register, otherwise the behavior can be unpredictable.
+        */
+       uint32_t itm_tcr;
+       retval = target_read_u32(target, ITM_TCR, &itm_tcr);
        if (retval != ERROR_OK)
                return retval;
-       if (trace_config->formatter)
-               ffcr |= (1 << 1);
-       else
-               ffcr &= ~(1 << 1);
-       retval = target_write_u32(target, TPIU_FFCR, ffcr);
+       retval = target_write_u32(target,
+                       ITM_TCR,
+                       itm_tcr & ~ITM_TCR_ITMENA_BIT
+                       );
        if (retval != ERROR_OK)
                return retval;
 
-       if (trace_config->config_type == TRACE_CONFIG_TYPE_INTERNAL)
-               target_register_timer_callback(armv7m_poll_trace, 1, 1, target);
-
-       target_call_event_callbacks(target, TARGET_EVENT_TRACE_CONFIG);
-
-       return ERROR_OK;
-}
-
-int armv7m_trace_itm_config(struct target *target)
-{
-       struct armv7m_common *armv7m = target_to_armv7m(target);
-       struct armv7m_trace_config *trace_config = &armv7m->trace_config;
-       int retval;
-
-       retval = target_write_u32(target, ITM_LAR, ITM_LAR_KEY);
-       if (retval != ERROR_OK)
-               return retval;
+       int64_t then = timeval_ms() + 1000;
+       do {
+               retval = target_read_u32(target, ITM_TCR, &itm_tcr);
+               if (retval != ERROR_OK)
+                       return retval;
+               if (timeval_ms() > then) {
+                       LOG_ERROR("timeout waiting for ITM_TCR_BUSY_BIT");
+                       return ERROR_FAIL;
+               }
+       } while (itm_tcr & ITM_TCR_BUSY_BIT);
 
        /* Enable ITM, TXENA, set TraceBusID and other parameters */
        retval = target_write_u32(target, ITM_TCR, (1 << 0) | (1 << 3) |
@@ -153,108 +83,6 @@ int armv7m_trace_itm_config(struct target *target)
        return ERROR_OK;
 }
 
-static void close_trace_file(struct armv7m_common *armv7m)
-{
-       if (armv7m->trace_config.trace_file)
-               fclose(armv7m->trace_config.trace_file);
-       armv7m->trace_config.trace_file = NULL;
-}
-
-COMMAND_HANDLER(handle_tpiu_config_command)
-{
-       struct target *target = get_current_target(CMD_CTX);
-       struct armv7m_common *armv7m = target_to_armv7m(target);
-
-       unsigned int cmd_idx = 0;
-
-       if (CMD_ARGC == cmd_idx)
-               return ERROR_COMMAND_SYNTAX_ERROR;
-       if (!strcmp(CMD_ARGV[cmd_idx], "disable")) {
-               if (CMD_ARGC == cmd_idx + 1) {
-                       close_trace_file(armv7m);
-
-                       armv7m->trace_config.config_type = TRACE_CONFIG_TYPE_DISABLED;
-                       if (CMD_CTX->mode == COMMAND_EXEC)
-                               return armv7m_trace_tpiu_config(target);
-                       else
-                               return ERROR_OK;
-               }
-       } else if (!strcmp(CMD_ARGV[cmd_idx], "external") ||
-                  !strcmp(CMD_ARGV[cmd_idx], "internal")) {
-               close_trace_file(armv7m);
-
-               armv7m->trace_config.config_type = TRACE_CONFIG_TYPE_EXTERNAL;
-               if (!strcmp(CMD_ARGV[cmd_idx], "internal")) {
-                       cmd_idx++;
-                       if (CMD_ARGC == cmd_idx)
-                               return ERROR_COMMAND_SYNTAX_ERROR;
-
-                       armv7m->trace_config.config_type = TRACE_CONFIG_TYPE_INTERNAL;
-
-                       if (strcmp(CMD_ARGV[cmd_idx], "-") != 0) {
-                               armv7m->trace_config.trace_file = fopen(CMD_ARGV[cmd_idx], "ab");
-                               if (!armv7m->trace_config.trace_file) {
-                                       LOG_ERROR("Can't open trace destination file");
-                                       return ERROR_FAIL;
-                               }
-                       }
-               }
-               cmd_idx++;
-               if (CMD_ARGC == cmd_idx)
-                       return ERROR_COMMAND_SYNTAX_ERROR;
-
-               if (!strcmp(CMD_ARGV[cmd_idx], "sync")) {
-                       armv7m->trace_config.pin_protocol = TPIU_PIN_PROTOCOL_SYNC;
-
-                       cmd_idx++;
-                       if (CMD_ARGC == cmd_idx)
-                               return ERROR_COMMAND_SYNTAX_ERROR;
-
-                       COMMAND_PARSE_NUMBER(u32, CMD_ARGV[cmd_idx], armv7m->trace_config.port_size);
-               } else {
-                       if (!strcmp(CMD_ARGV[cmd_idx], "manchester"))
-                               armv7m->trace_config.pin_protocol = TPIU_PIN_PROTOCOL_ASYNC_MANCHESTER;
-                       else if (!strcmp(CMD_ARGV[cmd_idx], "uart"))
-                               armv7m->trace_config.pin_protocol = TPIU_PIN_PROTOCOL_ASYNC_UART;
-                       else
-                               return ERROR_COMMAND_SYNTAX_ERROR;
-
-                       cmd_idx++;
-                       if (CMD_ARGC == cmd_idx)
-                               return ERROR_COMMAND_SYNTAX_ERROR;
-
-                       COMMAND_PARSE_ON_OFF(CMD_ARGV[cmd_idx], armv7m->trace_config.formatter);
-               }
-
-               cmd_idx++;
-               if (CMD_ARGC == cmd_idx)
-                       return ERROR_COMMAND_SYNTAX_ERROR;
-
-               COMMAND_PARSE_NUMBER(uint, CMD_ARGV[cmd_idx], armv7m->trace_config.traceclkin_freq);
-
-               cmd_idx++;
-               if (CMD_ARGC != cmd_idx) {
-                       COMMAND_PARSE_NUMBER(uint, CMD_ARGV[cmd_idx], armv7m->trace_config.trace_freq);
-                       cmd_idx++;
-               } else {
-                       if (armv7m->trace_config.config_type != TRACE_CONFIG_TYPE_INTERNAL) {
-                               LOG_ERROR("Trace port frequency can't be omitted in external capture mode");
-                               return ERROR_COMMAND_SYNTAX_ERROR;
-                       }
-                       armv7m->trace_config.trace_freq = 0;
-               }
-
-               if (CMD_ARGC == cmd_idx) {
-                       if (CMD_CTX->mode == COMMAND_EXEC)
-                               return armv7m_trace_tpiu_config(target);
-                       else
-                               return ERROR_OK;
-               }
-       }
-
-       return ERROR_COMMAND_SYNTAX_ERROR;
-}
-
 COMMAND_HANDLER(handle_itm_port_command)
 {
        struct target *target = get_current_target(CMD_CTX);
@@ -277,8 +105,9 @@ COMMAND_HANDLER(handle_itm_port_command)
 
        if (CMD_CTX->mode == COMMAND_EXEC)
                return armv7m_trace_itm_config(target);
-       else
-               return ERROR_OK;
+
+       armv7m->trace_config.itm_deferred_config = true;
+       return ERROR_OK;
 }
 
 COMMAND_HANDLER(handle_itm_ports_command)
@@ -296,23 +125,10 @@ COMMAND_HANDLER(handle_itm_ports_command)
 
        if (CMD_CTX->mode == COMMAND_EXEC)
                return armv7m_trace_itm_config(target);
-       else
-               return ERROR_OK;
-}
 
-static const struct command_registration tpiu_command_handlers[] = {
-       {
-               .name = "config",
-               .handler = handle_tpiu_config_command,
-               .mode = COMMAND_ANY,
-               .help = "Configure TPIU features",
-               .usage = "(disable | "
-               "((external | internal <filename>) "
-               "(sync <port width> | ((manchester | uart) <formatter enable>)) "
-               "<TRACECLKIN freq> [<trace freq>]))",
-       },
-       COMMAND_REGISTRATION_DONE
-};
+       armv7m->trace_config.itm_deferred_config = true;
+       return ERROR_OK;
+}
 
 static const struct command_registration itm_command_handlers[] = {
        {
@@ -333,13 +149,6 @@ static const struct command_registration itm_command_handlers[] = {
 };
 
 const struct command_registration armv7m_trace_command_handlers[] = {
-       {
-               .name = "tpiu",
-               .mode = COMMAND_ANY,
-               .help = "tpiu command group",
-               .usage = "",
-               .chain = tpiu_command_handlers,
-       },
        {
                .name = "itm",
                .mode = COMMAND_ANY,

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)