cmd: add missing usage var
[fw/openocd] / src / jtag / tcl.c
index 7dc7fb7c043e1bec865dbb4c1420c2874be05199..8808666f4273dd6e734c764e2f2321574b4002e1 100644 (file)
 #include "minidriver.h"
 #include "interface.h"
 #include "interfaces.h"
+#include "tcl.h"
 
 #ifdef HAVE_STRINGS_H
 #include <strings.h>
 #endif
 
+#include <helper/time_support.h>
+
 /**
  * @file
  * Holds support for accessing JTAG-specific mechanisms from TCl scripts.
@@ -63,7 +66,7 @@ struct jtag_tap *jtag_tap_by_jim_obj(Jim_Interp *interp, Jim_Obj *o)
        if (NULL == cp)
                cp = "(unknown)";
        if (NULL == t)
-               Jim_SetResult_sprintf(interp, "Tap '%s' could not be found", cp);
+               Jim_SetResultFormatted(interp, "Tap '%s' could not be found", cp);
        return t;
 }
 
@@ -145,7 +148,7 @@ static int Jim_Command_drscan(Jim_Interp *interp, int argc, Jim_Obj *const *args
                        endstate = tap_state_by_name(cp);
                        if (endstate < 0) {
                                /* update the error message */
-                               Jim_SetResult_sprintf(interp,"endstate: %s invalid", cp);
+                               Jim_SetResultFormatted(interp,"endstate: %s invalid", cp);
                        } else {
                                if (!scan_is_safe(endstate))
                                        LOG_WARNING("drscan with unsafe "
@@ -164,12 +167,15 @@ static int Jim_Command_drscan(Jim_Interp *interp, int argc, Jim_Obj *const *args
                }
        } /* validate args */
 
+       assert(e == JIM_OK);
+
        tap = jtag_tap_by_jim_obj(interp, args[1]);
        if (tap == NULL) {
                return JIM_ERR;
        }
 
        num_fields = (argc-2)/2;
+       assert(num_fields > 0);
        fields = malloc(sizeof(struct scan_field) * num_fields);
        for (i = 2; i < argc; i += 2)
        {
@@ -242,7 +248,7 @@ static int Jim_Command_pathmove(Jim_Interp *interp, int argc, Jim_Obj *const *ar
                if (states[i] < 0)
                {
                        /* update the error message */
-                       Jim_SetResult_sprintf(interp,"endstate: %s invalid", cp);
+                       Jim_SetResultFormatted(interp,"endstate: %s invalid", cp);
                        return JIM_ERR;
                }
        }
@@ -419,7 +425,7 @@ static int jtag_tap_configure_cmd(Jim_GetOptInfo *goi, struct jtag_tap * tap)
                                return e;
                        break;
                default:
-                       Jim_SetResult_sprintf(goi->interp, "unknown event: %s", n->name);
+                       Jim_SetResultFormatted(goi->interp, "unknown event: %s", n->name);
                        return JIM_ERR;
                }
        }
@@ -443,7 +449,7 @@ static int jim_newtap_expected_id(Jim_Nvp *n, Jim_GetOptInfo *goi,
        jim_wide w;
        int e = Jim_GetOpt_Wide(goi, &w);
        if (e != JIM_OK) {
-               Jim_SetResult_sprintf(goi->interp, "option: %s bad parameter", n->name);
+               Jim_SetResultFormatted(goi->interp, "option: %s bad parameter", n->name);
                return e;
        }
 
@@ -451,7 +457,7 @@ static int jim_newtap_expected_id(Jim_Nvp *n, Jim_GetOptInfo *goi,
        uint32_t *new_expected_ids = malloc(expected_len + sizeof(uint32_t));
        if (new_expected_ids == NULL)
        {
-               Jim_SetResult_sprintf(goi->interp, "no memory");
+               Jim_SetResultFormatted(goi->interp, "no memory");
                return JIM_ERR;
        }
 
@@ -481,7 +487,7 @@ static int jim_newtap_ir_param(Jim_Nvp *n, Jim_GetOptInfo *goi,
        int e = Jim_GetOpt_Wide(goi, &w);
        if (e != JIM_OK)
        {
-               Jim_SetResult_sprintf(goi->interp,
+               Jim_SetResultFormatted(goi->interp,
                                "option: %s bad parameter", n->name);
                free((void *)pTap->dotted_name);
                return e;
@@ -545,7 +551,7 @@ static int jim_newtap_cmd(Jim_GetOptInfo *goi)
 
        pTap = calloc(1, sizeof(struct jtag_tap));
        if (!pTap) {
-               Jim_SetResult_sprintf(goi->interp, "no memory");
+               Jim_SetResultFormatted(goi->interp, "no memory");
                return JIM_ERR;
        }
 
@@ -553,7 +559,7 @@ static int jim_newtap_cmd(Jim_GetOptInfo *goi)
         * we expect CHIP + TAP + OPTIONS
         * */
        if (goi->argc < 3) {
-               Jim_SetResult_sprintf(goi->interp, "Missing CHIP TAP OPTIONS ....");
+               Jim_SetResultFormatted(goi->interp, "Missing CHIP TAP OPTIONS ....");
                free(pTap);
                return JIM_ERR;
        }
@@ -631,7 +637,7 @@ static int jim_newtap_cmd(Jim_GetOptInfo *goi)
                return JIM_OK;
        }
 
-       Jim_SetResult_sprintf(goi->interp,
+       Jim_SetResultFormatted(goi->interp,
                        "newtap: %s missing IR length",
                        pTap->dotted_name);
        jtag_tap_free(pTap);
@@ -654,7 +660,8 @@ static void jtag_tap_handle_event(struct jtag_tap *tap, enum jtag_event e)
 
                if (Jim_EvalObj(jteap->interp, jteap->body) != JIM_OK)
                {
-                       Jim_PrintErrorMessage(jteap->interp);
+                       Jim_MakeErrorMessage(jteap->interp);
+                       LOG_USER("%s", Jim_GetString(Jim_GetResult(jteap->interp), NULL));
                        continue;
                }
 
@@ -684,10 +691,12 @@ static int jim_jtag_arp_init(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
                Jim_WrongNumArgs(goi.interp, 1, goi.argv-1, "(no params)");
                return JIM_ERR;
        }
-       struct command_context *context = Jim_GetAssocData(interp, "context");
+       struct command_context *context = current_command_context(interp);
        int e = jtag_init_inner(context);
        if (e != ERROR_OK) {
-               Jim_SetResult_sprintf(goi.interp, "error: %d", e);
+               Jim_Obj *eObj = Jim_NewIntObj(goi.interp, e);
+               Jim_SetResultFormatted(goi.interp, "error: %#s", eObj);
+               Jim_FreeNewObj(goi.interp, eObj);
                return JIM_ERR;
        }
        return JIM_OK;
@@ -701,16 +710,18 @@ static int jim_jtag_arp_init_reset(Jim_Interp *interp, int argc, Jim_Obj *const
                Jim_WrongNumArgs(goi.interp, 1, goi.argv-1, "(no params)");
                return JIM_ERR;
        }
-       struct command_context *context = Jim_GetAssocData(interp, "context");
+       struct command_context *context = current_command_context(interp);
        int e = jtag_init_reset(context);
        if (e != ERROR_OK) {
-               Jim_SetResult_sprintf(goi.interp, "error: %d", e);
+               Jim_Obj *eObj = Jim_NewIntObj(goi.interp, e);
+               Jim_SetResultFormatted(goi.interp, "error: %#s", eObj);
+               Jim_FreeNewObj(goi.interp, eObj);
                return JIM_ERR;
        }
        return JIM_OK;
 }
 
-static int jim_jtag_newtap(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+int jim_jtag_newtap(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
 {
        Jim_GetOptInfo goi;
        Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
@@ -748,13 +759,13 @@ static bool jtag_tap_disable(struct jtag_tap *t)
        return true;
 }
 
-static int jim_jtag_tap_enabler(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+int jim_jtag_tap_enabler(Jim_Interp *interp, int argc, Jim_Obj * const *argv)
 {
        const char *cmd_name = Jim_GetString(argv[0], NULL);
        Jim_GetOptInfo goi;
        Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
        if (goi.argc != 1) {
-               Jim_SetResult_sprintf(goi.interp, "usage: %s <name>", cmd_name);
+               Jim_SetResultFormatted(goi.interp, "usage: %s <name>", cmd_name);
                return JIM_ERR;
        }
 
@@ -767,11 +778,15 @@ static int jim_jtag_tap_enabler(Jim_Interp *interp, int argc, Jim_Obj *const *ar
        if (strcasecmp(cmd_name, "tapisenabled") == 0) {
                // do nothing, just return the value
        } else if (strcasecmp(cmd_name, "tapenable") == 0) {
-               if (!jtag_tap_enable(t))
-                       LOG_WARNING("failed to disable tap");
+               if (!jtag_tap_enable(t)){
+                       LOG_WARNING("failed to enable tap %s", t->dotted_name);
+                        return JIM_ERR;
+                }
        } else if (strcasecmp(cmd_name, "tapdisable") == 0) {
-               if (!jtag_tap_disable(t))
-                       LOG_WARNING("failed to disable tap");
+               if (!jtag_tap_disable(t)){
+                       LOG_WARNING("failed to disable tap %s", t->dotted_name);
+                        return JIM_ERR;
+                }
        } else {
                LOG_ERROR("command '%s' unknown", cmd_name);
                return JIM_ERR;
@@ -781,7 +796,7 @@ static int jim_jtag_tap_enabler(Jim_Interp *interp, int argc, Jim_Obj *const *ar
        return JIM_OK;
 }
 
-static int jim_jtag_configure(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+int jim_jtag_configure(Jim_Interp *interp, int argc, Jim_Obj * const *argv)
 {
        const char *cmd_name = Jim_GetString(argv[0], NULL);
        Jim_GetOptInfo goi;
@@ -1165,7 +1180,8 @@ COMMAND_HANDLER(handle_irscan_command)
                retval = parse_u32(CMD_ARGV[i * 2 + 1], &value);
                if (ERROR_OK != retval)
                        goto error_return;
-               buf_set_u32((void *)fields[i].out_value, 0, field_size, value);
+void *v = (void *)fields[i].out_value;
+               buf_set_u32(v, 0, field_size, value);
                fields[i].in_value = NULL;
        }
 
@@ -1247,7 +1263,71 @@ COMMAND_HANDLER(handle_tms_sequence_command)
        return ERROR_OK;
 }
 
+COMMAND_HANDLER(handle_jtag_flush_queue_sleep)
+{
+       if (CMD_ARGC != 1)
+               return ERROR_COMMAND_SYNTAX_ERROR;
+
+       int sleep_ms;
+       COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], sleep_ms);
+
+       jtag_set_flush_queue_sleep(sleep_ms);
+
+       return ERROR_OK;
+}
+
+COMMAND_HANDLER(handle_wait_srst_deassert)
+{
+       if (CMD_ARGC != 1)
+               return ERROR_COMMAND_SYNTAX_ERROR;
+
+       int timeout_ms;
+       COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], timeout_ms);
+       if ((timeout_ms <= 0) || (timeout_ms > 100000))
+       {
+               LOG_ERROR("Timeout must be an integer between 0 and 100000");
+               return ERROR_FAIL;
+       }
+
+       LOG_USER("Waiting for srst assert + deassert for at most %dms", timeout_ms);
+       int asserted_yet;
+       long long then = timeval_ms();
+       while (jtag_srst_asserted(&asserted_yet) == ERROR_OK)
+       {
+               if ((timeval_ms() - then) > timeout_ms)
+               {
+                       LOG_ERROR("Timed out");
+                       return ERROR_FAIL;
+               }
+               if (asserted_yet)
+                       break;
+       }
+       while (jtag_srst_asserted(&asserted_yet) == ERROR_OK)
+       {
+               if ((timeval_ms() - then) > timeout_ms)
+               {
+                       LOG_ERROR("Timed out");
+                       return ERROR_FAIL;
+               }
+               if (!asserted_yet)
+                       break;
+       }
+
+       return ERROR_OK;
+}
+
+
+
 static const struct command_registration jtag_command_handlers[] = {
+
+       {
+               .name = "jtag_flush_queue_sleep",
+               .handler = handle_jtag_flush_queue_sleep,
+               .mode = COMMAND_ANY,
+               .help = "For debug purposes(simulate long delays of interface) "
+                               "to test performance or change in behavior. Default 0ms.",
+               .usage = "[sleep in ms]",
+       },
        {
                .name = "jtag_rclk",
                .handler = handle_jtag_rclk_command,
@@ -1327,6 +1407,15 @@ static const struct command_registration jtag_command_handlers[] = {
                        /* Specifically for working around DRIVER bugs... */
                .usage = "['short'|'long']",
        },
+       {
+               .name = "wait_srst_deassert",
+               .handler = handle_wait_srst_deassert,
+               .mode = COMMAND_ANY,
+               .help = "Wait for an SRST deassert. "
+                       "Useful for cases where you need something to happen within ms "
+                       "of an srst deassert. Timeout in ms ",
+               .usage = "ms",
+       },
        {
                .name = "jtag",
                .mode = COMMAND_ANY,