retire ERROR_INVALID_ARGUMENTS and replace with ERROR_COMMAND_SYNTAX_ERROR
[openocd.git] / src / jtag / core.c
index 1068681964ee379a858078f434c31174cd4f1f82..a0feed1dc0cdf1cc98a0fea0b1c4665c5dc12012 100644 (file)
@@ -33,7 +33,7 @@
 
 #include "jtag.h"
 #include "interface.h"
-#include "transport.h"
+#include <transport/transport.h>
 
 #ifdef HAVE_STRINGS_H
 #include <strings.h>
@@ -124,11 +124,14 @@ static struct jtag_event_callback *jtag_event_callbacks;
 static int speed_khz = 0;
 /* speed to fallback to when RCLK is requested but not supported */
 static int rclk_fallback_speed_khz = 0;
-static enum {CLOCK_MODE_SPEED, CLOCK_MODE_KHZ, CLOCK_MODE_RCLK} clock_mode;
+static enum {CLOCK_MODE_UNSELECTED, CLOCK_MODE_KHZ, CLOCK_MODE_RCLK} clock_mode;
 static int jtag_speed = 0;
 
 static struct jtag_interface *jtag = NULL;
 
+
+const struct swd_driver *swd = NULL;
+
 /* configuration */
 struct jtag_interface *jtag_interface = NULL;
 
@@ -213,7 +216,7 @@ void jtag_tap_add(struct jtag_tap *t)
 }
 
 /* returns a pointer to the n-th device in the scan chain */
-static inline struct jtag_tap *jtag_tap_by_position(unsigned n)
+struct jtag_tap *jtag_tap_by_position(unsigned n)
 {
        struct jtag_tap *t = jtag_all_taps();
 
@@ -276,7 +279,7 @@ int jtag_register_event_callback(jtag_event_handler_t callback, void *priv)
 
        if (callback == NULL)
        {
-               return ERROR_INVALID_ARGUMENTS;
+               return ERROR_COMMAND_SYNTAX_ERROR;
        }
 
        if (*callbacks_p)
@@ -296,28 +299,24 @@ int jtag_register_event_callback(jtag_event_handler_t callback, void *priv)
 
 int jtag_unregister_event_callback(jtag_event_handler_t callback, void *priv)
 {
-       struct jtag_event_callback **callbacks_p;
-       struct jtag_event_callback **next;
+       struct jtag_event_callback **p = &jtag_event_callbacks, *temp;
 
        if (callback == NULL)
        {
-               return ERROR_INVALID_ARGUMENTS;
+               return ERROR_COMMAND_SYNTAX_ERROR;
        }
 
-       for (callbacks_p = &jtag_event_callbacks;
-                       *callbacks_p != NULL;
-                       callbacks_p = next)
+       while (*p)
        {
-               next = &((*callbacks_p)->next);
-
-               if ((*callbacks_p)->priv != priv)
-                       continue;
-
-               if ((*callbacks_p)->callback == callback)
+               if (((*p)->priv != priv) || ((*p)->callback != callback))
                {
-                       free(*callbacks_p);
-                       *callbacks_p = *next;
+                       p = &(*p)->next;
+                       continue;
                }
+
+               temp = *p;
+               *p = (*p)->next;
+               free(temp);
        }
 
        return ERROR_OK;
@@ -356,11 +355,6 @@ static void jtag_prelude(tap_state_t state)
        cmd_queue_cur_state = state;
 }
 
-void jtag_alloc_in_value32(struct scan_field *field)
-{
-       interface_jtag_alloc_in_value32(field);
-}
-
 void jtag_add_ir_scan_noverify(struct jtag_tap *active, const struct scan_field *in_fields,
                tap_state_t state)
 {
@@ -376,6 +370,7 @@ static void jtag_add_ir_scan_noverify_callback(struct jtag_tap *active, int dumm
        jtag_add_ir_scan_noverify(active, in_fields, state);
 }
 
+/* If fields->in_value is filled out, then the captured IR value will be checked */
 void jtag_add_ir_scan(struct jtag_tap *active, struct scan_field *in_fields, tap_state_t state)
 {
        assert(state != TAP_RESET);
@@ -420,17 +415,6 @@ static int jtag_check_value_mask_callback(jtag_callback_data_t data0, jtag_callb
 static void jtag_add_scan_check(struct jtag_tap *active, void (*jtag_add_scan)(struct jtag_tap *active, int in_num_fields, const struct scan_field *in_fields, tap_state_t state),
                int in_num_fields, struct scan_field *in_fields, tap_state_t state)
 {
-       for (int i = 0; i < in_num_fields; i++)
-       {
-               struct scan_field *field = &in_fields[i];
-               field->allocated = 0;
-               field->modified = 0;
-               if (field->check_value || field->in_value)
-                       continue;
-               interface_jtag_add_scan_check_alloc(field);
-               field->modified = 1;
-       }
-
        jtag_add_scan(active, in_num_fields, in_fields, state);
 
        for (int i = 0; i < in_num_fields; i++)
@@ -443,14 +427,6 @@ static void jtag_add_scan_check(struct jtag_tap *active, void (*jtag_add_scan)(s
                                (jtag_callback_data_t)in_fields[i].check_mask,
                                (jtag_callback_data_t)in_fields[i].num_bits);
                }
-               if (in_fields[i].allocated)
-               {
-                       free(in_fields[i].in_value);
-               }
-               if (in_fields[i].modified)
-               {
-                       in_fields[i].in_value = NULL;
-               }
        }
 }
 
@@ -696,7 +672,7 @@ void jtag_add_reset(int req_tlr_or_trst, int req_srst)
                        retval = jtag_execute_queue();
 
                if (retval != ERROR_OK) {
-                       LOG_ERROR("TRST/SRST error %d", retval);
+                       LOG_ERROR("TRST/SRST error");
                        return;
                }
        }
@@ -872,9 +848,16 @@ static int jtag_reset_callback(enum jtag_event event, void *priv)
        return ERROR_OK;
 }
 
+/* sleep at least us microseconds. When we sleep more than 1000ms we
+ * do an alive sleep, i.e. keep GDB alive. Note that we could starve
+ * GDB if we slept for <1000ms many times.
+ */
 void jtag_sleep(uint32_t us)
 {
-       alive_sleep(us/1000);
+       if (us < 1000)
+               usleep(us);
+       else
+               alive_sleep((us+999)/1000);
 }
 
 /* Maximum number of enabled JTAG devices we expect in the scan chain,
@@ -1362,12 +1345,13 @@ int adapter_init(struct command_context *cmd_ctx)
                return ERROR_JTAG_INVALID_INTERFACE;
        }
 
-       jtag = jtag_interface;
-       if (jtag_interface->init() != ERROR_OK)
+       int retval;
+       retval = jtag_interface->init();
+       if (retval != ERROR_OK)
        {
-               jtag = NULL;
-               return ERROR_JTAG_INIT_FAILED;
+               return retval;
        }
+       jtag = jtag_interface;
 
        /* LEGACY SUPPORT ... adapter drivers  must declare what
         * transports they allow.  Until they all do so, assume
@@ -1377,16 +1361,30 @@ int adapter_init(struct command_context *cmd_ctx)
                LOG_ERROR("Adapter driver '%s' did not declare "
                        "which transports it allows; assuming "
                        "JTAG-only", jtag->name);
-               int retval = allow_transports(cmd_ctx, jtag_only);
+               retval = allow_transports(cmd_ctx, jtag_only);
                if (retval != ERROR_OK)
                        return retval;
        }
 
+       if (CLOCK_MODE_UNSELECTED == clock_mode)
+       {
+               LOG_ERROR("An adapter speed is not selected in the init script."
+                       " Insert a call to adapter_khz or jtag_rclk to proceed.");
+               return ERROR_JTAG_INIT_FAILED;
+       }
+
        int requested_khz = jtag_get_speed_khz();
        int actual_khz = requested_khz;
-       int retval = jtag_get_speed_readable(&actual_khz);
+       int jtag_speed_var = 0;
+       retval = jtag_get_speed(&jtag_speed_var);
+       if (retval != ERROR_OK)
+               return retval;
+       retval = jtag->speed(jtag_speed_var);
+       if (retval != ERROR_OK)
+               return retval;
+       retval = jtag_get_speed_readable(&actual_khz);
        if (ERROR_OK != retval)
-               LOG_INFO("adapter-specific clock speed value %d", jtag_get_speed());
+               LOG_INFO("adapter-specific clock speed value %d", jtag_speed_var);
        else if (actual_khz)
        {
                /* Adaptive clocking -- JTAG-specific */
@@ -1445,17 +1443,19 @@ int jtag_init_inner(struct command_context *cmd_ctx)
        case ERROR_OK:
                /* complete success */
                break;
-       case ERROR_JTAG_INIT_SOFT_FAIL:
+       default:
                /* For backward compatibility reasons, try coping with
                 * configuration errors involving only ID mismatches.
                 * We might be able to talk to the devices.
+                *
+                * Also the device might be powered down during startup.
+                *
+                * After OpenOCD starts, we can try to power on the device
+                * and run a reset.
                 */
                LOG_ERROR("Trying to use configured scan chain anyway...");
                issue_setup = false;
                break;
-       default:
-               /* some hard error; already issued diagnostics */
-               return retval;
        }
 
        /* Now look at IR values.  Problems here will prevent real
@@ -1466,7 +1466,13 @@ int jtag_init_inner(struct command_context *cmd_ctx)
         */
        retval = jtag_validate_ircapture();
        if (retval != ERROR_OK)
-               return retval;
+       {
+               /* The target might be powered down. The user
+                * can power it up and reset it after firing
+                * up OpenOCD.
+                */
+               issue_setup = false;
+       }
 
        if (issue_setup)
                jtag_notify_event(JTAG_TAP_EVENT_SETUP);
@@ -1624,31 +1630,30 @@ int jtag_config_rclk(unsigned fallback_speed_khz)
        return (ERROR_OK != retval) ? retval : jtag_set_speed(speed);
 }
 
-int jtag_get_speed(void)
+int jtag_get_speed(int *speed)
 {
-       int speed;
        switch(clock_mode)
        {
-               case CLOCK_MODE_SPEED:
-                       speed = jtag_speed;
-                       break;
                case CLOCK_MODE_KHZ:
-                       adapter_khz_to_speed(jtag_get_speed_khz(), &speed);
+                       adapter_khz_to_speed(jtag_get_speed_khz(), speed);
                        break;
                case CLOCK_MODE_RCLK:
-                       jtag_rclk_to_speed(rclk_fallback_speed_khz, &speed);
+                       jtag_rclk_to_speed(rclk_fallback_speed_khz, speed);
                        break;
                default:
                        LOG_ERROR("BUG: unknown jtag clock mode");
-                       speed = 0;
-                       break;
+                       return ERROR_FAIL;
        }
-       return speed;
+       return ERROR_OK;
 }
 
 int jtag_get_speed_readable(int *khz)
 {
-       return jtag ? jtag->speed_div(jtag_get_speed(), khz) : ERROR_OK;
+       int jtag_speed_var = 0;
+       int retval = jtag_get_speed(&jtag_speed_var);
+       if (retval != ERROR_OK)
+               return retval;
+       return jtag ? jtag->speed_div(jtag_speed_var, khz) : ERROR_OK;
 }
 
 void jtag_set_verify(bool enable)

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)