X-Git-Url: https://review.openocd.org/gitweb?p=openocd.git;a=blobdiff_plain;f=src%2Fjtag%2Ftcl.c;h=e7a0f67db32cfba4221bea5eec60d944d41c4dbe;hp=4da8838d375f2be801087100addf10700ec820f7;hb=c4992c6d863d0ead91d84d19bbfe1643d720b205;hpb=5b6df55a1e5e4c0f531bc336691bc7c9a6a0df87 diff --git a/src/jtag/tcl.c b/src/jtag/tcl.c index 4da8838d37..e7a0f67db3 100644 --- a/src/jtag/tcl.c +++ b/src/jtag/tcl.c @@ -49,7 +49,7 @@ static const Jim_Nvp nvp_jtag_tap_event[] = { { .name = NULL, .value = -1 } }; -extern jtag_interface_t *jtag_interface; +extern struct jtag_interface *jtag_interface; enum jtag_tap_cfg_param { JCFG_EVENT @@ -61,7 +61,7 @@ static Jim_Nvp nvp_config_opts[] = { { .name = NULL, .value = -1 } }; -static int jtag_tap_configure_cmd(Jim_GetOptInfo *goi, jtag_tap_t * tap) +static int jtag_tap_configure_cmd(Jim_GetOptInfo *goi, struct jtag_tap * tap) { Jim_Nvp *n; Jim_Obj *o; @@ -103,7 +103,7 @@ static int jtag_tap_configure_cmd(Jim_GetOptInfo *goi, jtag_tap_t * tap) } { - jtag_tap_event_action_t *jteap; + struct jtag_tap_event_action *jteap; jteap = tap->event_action; /* replace existing? */ @@ -165,7 +165,7 @@ static int is_bad_irval(int ir_length, jim_wide w) static int jim_newtap_cmd(Jim_GetOptInfo *goi) { - jtag_tap_t *pTap; + struct jtag_tap *pTap; jim_wide w; int x; int e; @@ -187,7 +187,7 @@ static int jim_newtap_cmd(Jim_GetOptInfo *goi) { .name = NULL , .value = -1 }, }; - pTap = calloc(1, sizeof(jtag_tap_t)); + pTap = calloc(1, sizeof(struct jtag_tap)); if (!pTap) { Jim_SetResult_sprintf(goi->interp, "no memory"); return JIM_ERR; @@ -335,9 +335,9 @@ static int jim_newtap_cmd(Jim_GetOptInfo *goi) return JIM_ERR; } -static void jtag_tap_handle_event(jtag_tap_t *tap, enum jtag_event e) +static void jtag_tap_handle_event(struct jtag_tap *tap, enum jtag_event e) { - jtag_tap_event_action_t * jteap; + struct jtag_tap_event_action * jteap; for (jteap = tap->event_action; jteap != NULL; jteap = jteap->next) { if (jteap->event == e) { @@ -366,213 +366,244 @@ static void jtag_tap_handle_event(jtag_tap_t *tap, enum jtag_event e) } } - -static int jim_jtag_command(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +static int jim_jtag_interface(Jim_Interp *interp, int argc, Jim_Obj *const *argv) { Jim_GetOptInfo goi; - int e; - Jim_Nvp *n; - Jim_Obj *o; - struct command_context_s *context; - - enum { - JTAG_CMD_INTERFACE, - JTAG_CMD_INIT, - JTAG_CMD_INIT_RESET, - JTAG_CMD_NEWTAP, - JTAG_CMD_TAPENABLE, - JTAG_CMD_TAPDISABLE, - JTAG_CMD_TAPISENABLED, - JTAG_CMD_CONFIGURE, - JTAG_CMD_CGET, - JTAG_CMD_NAMES, - }; - - const Jim_Nvp jtag_cmds[] = { - { .name = "interface" , .value = JTAG_CMD_INTERFACE }, - { .name = "arp_init" , .value = JTAG_CMD_INIT }, - { .name = "arp_init-reset", .value = JTAG_CMD_INIT_RESET }, - { .name = "newtap" , .value = JTAG_CMD_NEWTAP }, - { .name = "tapisenabled" , .value = JTAG_CMD_TAPISENABLED }, - { .name = "tapenable" , .value = JTAG_CMD_TAPENABLE }, - { .name = "tapdisable" , .value = JTAG_CMD_TAPDISABLE }, - { .name = "configure" , .value = JTAG_CMD_CONFIGURE }, - { .name = "cget" , .value = JTAG_CMD_CGET }, - { .name = "names" , .value = JTAG_CMD_NAMES }, - - { .name = NULL, .value = -1 }, - }; - - context = Jim_GetAssocData(interp, "context"); - /* go past the command */ Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1); - e = Jim_GetOpt_Nvp(&goi, jtag_cmds, &n); - if (e != JIM_OK) { - Jim_GetOpt_NvpUnknown(&goi, jtag_cmds, 0); - return e; + /* return the name of the interface */ + /* TCL code might need to know the exact type... */ + /* FUTURE: we allow this as a means to "set" the interface. */ + if (goi.argc != 0) { + Jim_WrongNumArgs(goi.interp, 1, goi.argv-1, "(no params)"); + return JIM_ERR; } - Jim_SetEmptyResult(goi.interp); - switch (n->value) { - case JTAG_CMD_INTERFACE: - /* return the name of the interface */ - /* TCL code might need to know the exact type... */ - /* FUTURE: we allow this as a means to "set" the interface. */ - if (goi.argc != 0) { - Jim_WrongNumArgs(goi.interp, 1, goi.argv-1, "(no params)"); - return JIM_ERR; - } - const char *name = jtag_interface ? jtag_interface->name : NULL; - Jim_SetResultString(goi.interp, name ? : "undefined", -1); - return JIM_OK; - case JTAG_CMD_INIT: - if (goi.argc != 0) { - Jim_WrongNumArgs(goi.interp, 1, goi.argv-1, "(no params)"); - return JIM_ERR; - } - e = jtag_init_inner(context); - if (e != ERROR_OK) { - Jim_SetResult_sprintf(goi.interp, "error: %d", e); - return JIM_ERR; - } - return JIM_OK; - case JTAG_CMD_INIT_RESET: - if (goi.argc != 0) { - Jim_WrongNumArgs(goi.interp, 1, goi.argv-1, "(no params)"); - return JIM_ERR; - } - e = jtag_init_reset(context); - if (e != ERROR_OK) { - Jim_SetResult_sprintf(goi.interp, "error: %d", e); - return JIM_ERR; - } - return JIM_OK; - case JTAG_CMD_NEWTAP: - return jim_newtap_cmd(&goi); - break; - case JTAG_CMD_TAPISENABLED: - case JTAG_CMD_TAPENABLE: - case JTAG_CMD_TAPDISABLE: - if (goi.argc != 1) { - Jim_SetResultString(goi.interp, "Too many parameters",-1); - return JIM_ERR; - } - - { - jtag_tap_t *t; + const char *name = jtag_interface ? jtag_interface->name : NULL; + Jim_SetResultString(goi.interp, name ? : "undefined", -1); + return JIM_OK; +} - t = jtag_tap_by_jim_obj(goi.interp, goi.argv[0]); - if (t == NULL) - return JIM_ERR; +static int jim_jtag_arp_init(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + Jim_GetOptInfo goi; + Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1); + if (goi.argc != 0) { + Jim_WrongNumArgs(goi.interp, 1, goi.argv-1, "(no params)"); + return JIM_ERR; + } + struct command_context *context = Jim_GetAssocData(interp, "context"); + int e = jtag_init_inner(context); + if (e != ERROR_OK) { + Jim_SetResult_sprintf(goi.interp, "error: %d", e); + return JIM_ERR; + } + return JIM_OK; +} - switch (n->value) { - case JTAG_CMD_TAPISENABLED: - break; - case JTAG_CMD_TAPENABLE: - if (t->enabled) - break; - jtag_tap_handle_event(t, JTAG_TAP_EVENT_ENABLE); - if (!t->enabled) - break; - - /* FIXME add JTAG sanity checks, w/o TLR - * - scan chain length grew by one (this) - * - IDs and IR lengths are as expected - */ +static int jim_jtag_arp_init_reset(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + Jim_GetOptInfo goi; + Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1); + if (goi.argc != 0) { + Jim_WrongNumArgs(goi.interp, 1, goi.argv-1, "(no params)"); + return JIM_ERR; + } + struct command_context *context = Jim_GetAssocData(interp, "context"); + int e = jtag_init_reset(context); + if (e != ERROR_OK) { + Jim_SetResult_sprintf(goi.interp, "error: %d", e); + return JIM_ERR; + } + return JIM_OK; +} - jtag_call_event_callbacks(JTAG_TAP_EVENT_ENABLE); - break; - case JTAG_CMD_TAPDISABLE: - if (!t->enabled) - break; - jtag_tap_handle_event(t, JTAG_TAP_EVENT_DISABLE); - if (t->enabled) - break; - - /* FIXME add JTAG sanity checks, w/o TLR - * - scan chain length shrank by one (this) - * - IDs and IR lengths are as expected - */ +static 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); + return jim_newtap_cmd(&goi); +} - jtag_call_event_callbacks(JTAG_TAP_EVENT_DISABLE); - break; - } - e = t->enabled; - Jim_SetResult(goi.interp, Jim_NewIntObj(goi.interp, e)); - return JIM_OK; - } - break; +static bool jtag_tap_enable(struct jtag_tap *t) +{ + if (t->enabled) + return false; + jtag_tap_handle_event(t, JTAG_TAP_EVENT_ENABLE); + if (!t->enabled) + return false; - case JTAG_CMD_CGET: - if (goi.argc < 2) { - Jim_WrongNumArgs(goi.interp, 0, NULL, - "cget tap_name queryparm"); - return JIM_ERR; - } + /* FIXME add JTAG sanity checks, w/o TLR + * - scan chain length grew by one (this) + * - IDs and IR lengths are as expected + */ + jtag_call_event_callbacks(JTAG_TAP_EVENT_ENABLE); + return true; +} +static bool jtag_tap_disable(struct jtag_tap *t) +{ + if (!t->enabled) + return false; + jtag_tap_handle_event(t, JTAG_TAP_EVENT_DISABLE); + if (t->enabled) + return false; - { - jtag_tap_t *t; + /* FIXME add JTAG sanity checks, w/o TLR + * - scan chain length shrank by one (this) + * - IDs and IR lengths are as expected + */ + jtag_call_event_callbacks(JTAG_TAP_EVENT_DISABLE); + return true; +} - Jim_GetOpt_Obj(&goi, &o); - t = jtag_tap_by_jim_obj(goi.interp, o); - if (t == NULL) { - return JIM_ERR; - } +static 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 ", cmd_name); + return JIM_ERR; + } - goi.isconfigure = 0; - return jtag_tap_configure_cmd(&goi, t); - } - break; + struct jtag_tap *t; - case JTAG_CMD_CONFIGURE: - if (goi.argc < 3) { - Jim_WrongNumArgs(goi.interp, 0, NULL, - "configure tap_name attribute value ..."); - return JIM_ERR; - } + t = jtag_tap_by_jim_obj(goi.interp, goi.argv[0]); + if (t == NULL) + return JIM_ERR; - { - jtag_tap_t *t; + 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"); + } else if (strcasecmp(cmd_name, "tapdisable") == 0) { + if (!jtag_tap_disable(t)) + LOG_WARNING("failed to disable tap"); + } else { + LOG_ERROR("command '%s' unknown", cmd_name); + return JIM_ERR; + } + bool e = t->enabled; + Jim_SetResult(goi.interp, Jim_NewIntObj(goi.interp, e)); + return JIM_OK; +} - Jim_GetOpt_Obj(&goi, &o); - t = jtag_tap_by_jim_obj(goi.interp, o); - if (t == NULL) { - return JIM_ERR; - } +static 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; + Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1); + goi.isconfigure = !strcmp(cmd_name, "configure"); + if (goi.argc < 2 + goi.isconfigure) { + Jim_WrongNumArgs(goi.interp, 0, NULL, + " ..."); + return JIM_ERR; + } - goi.isconfigure = 1; - return jtag_tap_configure_cmd(&goi, t); - } - break; + struct jtag_tap *t; - case JTAG_CMD_NAMES: - if (goi.argc != 0) { - Jim_WrongNumArgs(goi.interp, 1, goi.argv, "Too many parameters"); - return JIM_ERR; - } - Jim_SetResult(goi.interp, Jim_NewListObj(goi.interp, NULL, 0)); - { - jtag_tap_t *tap; + Jim_Obj *o; + Jim_GetOpt_Obj(&goi, &o); + t = jtag_tap_by_jim_obj(goi.interp, o); + if (t == NULL) { + return JIM_ERR; + } - for (tap = jtag_all_taps(); tap; tap = tap->next_tap) { - Jim_ListAppendElement(goi.interp, - Jim_GetResult(goi.interp), - Jim_NewStringObj(goi.interp, - tap->dotted_name, -1)); - } - return JIM_OK; - } - break; + return jtag_tap_configure_cmd(&goi, t); +} +static int jim_jtag_names(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + Jim_GetOptInfo goi; + Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1); + if (goi.argc != 0) { + Jim_WrongNumArgs(goi.interp, 1, goi.argv, "Too many parameters"); + return JIM_ERR; } - - return JIM_ERR; + Jim_SetResult(goi.interp, Jim_NewListObj(goi.interp, NULL, 0)); + struct jtag_tap *tap; + + for (tap = jtag_all_taps(); tap; tap = tap->next_tap) { + Jim_ListAppendElement(goi.interp, + Jim_GetResult(goi.interp), + Jim_NewStringObj(goi.interp, + tap->dotted_name, -1)); + } + return JIM_OK; } +static const struct command_registration jtag_subcommand_handlers[] = { + { + .name = "interface", + .mode = COMMAND_ANY, + .jim_handler = &jim_jtag_interface, + .help = "Returns the selected interface", + }, + { + .name = "arp_init", + .mode = COMMAND_ANY, + .jim_handler = &jim_jtag_arp_init, + }, + { + .name = "arp_init-reset", + .mode = COMMAND_ANY, + .jim_handler = &jim_jtag_arp_init_reset, + }, + { + .name = "newtap", + .mode = COMMAND_CONFIG, + .jim_handler = &jim_jtag_newtap, + .help = "Create a new TAP instance", + .usage = " -irlen [-ircapture ] " + "[-irmask ] [-enable|-disable]", + }, + { + .name = "tapisenabled", + .mode = COMMAND_EXEC, + .jim_handler = &jim_jtag_tap_enabler, + .help = "Returns a integer indicating TAP state (0/1)", + .usage = "", + }, + { + .name = "tapenable", + .mode = COMMAND_EXEC, + .jim_handler = &jim_jtag_tap_enabler, + .help = "Enable the specified TAP", + .usage = "", + }, + { + .name = "tapdisable", + .mode = COMMAND_EXEC, + .jim_handler = &jim_jtag_tap_enabler, + .help = "Enable the specified TAP", + .usage = "", + }, + { + .name = "configure", + .mode = COMMAND_EXEC, + .jim_handler = &jim_jtag_configure, + .help = "Enable the specified TAP", + .usage = " [ ...]", + }, + { + .name = "cget", + .mode = COMMAND_EXEC, + .jim_handler = &jim_jtag_configure, + .help = "Enable the specified TAP", + .usage = " [ ...]", + }, + { + .name = "names", + .mode = COMMAND_ANY, + .jim_handler = &jim_jtag_names, + .help = "Returns list of all JTAG tap names", + }, + COMMAND_REGISTRATION_DONE +}; void jtag_notify_event(enum jtag_event event) { - jtag_tap_t *tap; + struct jtag_tap *tap; for (tap = jtag_all_taps(); tap; tap = tap->next_tap) jtag_tap_handle_event(tap, event); @@ -605,14 +636,14 @@ static int default_srst_asserted(int *srst_asserted) COMMAND_HANDLER(handle_interface_list_command) { - if (strcmp(cmd, "interface_list") == 0 && argc > 0) + if (strcmp(CMD_NAME, "interface_list") == 0 && CMD_ARGC > 0) return ERROR_COMMAND_SYNTAX_ERROR; - command_print(cmd_ctx, "The following JTAG interfaces are available:"); + command_print(CMD_CTX, "The following JTAG interfaces are available:"); for (unsigned i = 0; NULL != jtag_interfaces[i]; i++) { const char *name = jtag_interfaces[i]->name; - command_print(cmd_ctx, "%u: %s", i + 1, name); + command_print(CMD_CTX, "%u: %s", i + 1, name); } return ERROR_OK; @@ -628,17 +659,21 @@ COMMAND_HANDLER(handle_interface_command) } /* interface name is a mandatory argument */ - if (argc != 1 || args[0][0] == '\0') + if (CMD_ARGC != 1 || CMD_ARGV[0][0] == '\0') return ERROR_COMMAND_SYNTAX_ERROR; for (unsigned i = 0; NULL != jtag_interfaces[i]; i++) { - if (strcmp(args[0], jtag_interfaces[i]->name) != 0) + if (strcmp(CMD_ARGV[0], jtag_interfaces[i]->name) != 0) continue; - int retval = jtag_interfaces[i]->register_commands(cmd_ctx); - if (ERROR_OK != retval) + if (NULL != jtag_interfaces[i]->commands) + { + int retval = register_commands(CMD_CTX, NULL, + jtag_interfaces[i]->commands); + if (ERROR_OK != retval) return retval; + } jtag_interface = jtag_interfaces[i]; @@ -657,18 +692,18 @@ COMMAND_HANDLER(handle_interface_command) /* no valid interface was found (i.e. the configuration option, * didn't match one of the compiled-in interfaces */ - LOG_ERROR("The specified JTAG interface was not found (%s)", args[0]); + LOG_ERROR("The specified JTAG interface was not found (%s)", CMD_ARGV[0]); CALL_COMMAND_HANDLER(handle_interface_list_command); return ERROR_JTAG_INVALID_INTERFACE; } COMMAND_HANDLER(handle_scan_chain_command) { - jtag_tap_t *tap; + struct jtag_tap *tap; tap = jtag_all_taps(); - command_print(cmd_ctx, " TapName | Enabled | IdCode Expected IrLen IrCap IrMask Instr "); - command_print(cmd_ctx, "---|--------------------|---------|------------|------------|------|------|------|---------"); + command_print(CMD_CTX, " TapName | Enabled | IdCode Expected IrLen IrCap IrMask Instr "); + command_print(CMD_CTX, "---|--------------------|---------|------------|------------|------|------|------|---------"); while (tap) { uint32_t expected, expected_mask, cur_instr, ii; @@ -676,7 +711,7 @@ COMMAND_HANDLER(handle_scan_chain_command) expected_mask = buf_get_u32(tap->expected_mask, 0, tap->ir_length); cur_instr = buf_get_u32(tap->cur_instr, 0, tap->ir_length); - command_print(cmd_ctx, + command_print(CMD_CTX, "%2d | %-18s | %c | 0x%08x | 0x%08x | 0x%02x | 0x%02x | 0x%02x | 0x%02x", tap->abs_chain_position, tap->dotted_name, @@ -689,7 +724,7 @@ COMMAND_HANDLER(handle_scan_chain_command) (unsigned int)(cur_instr)); for (ii = 1; ii < tap->expected_ids_cnt; ii++) { - command_print(cmd_ctx, " | | | | 0x%08x | | | | ", + command_print(CMD_CTX, " | | | | 0x%08x | | | | ", (unsigned int)(tap->expected_ids[ii])); } @@ -711,21 +746,21 @@ COMMAND_HANDLER(handle_reset_config_command) * Here we don't care about the order, and only change values * which have been explicitly specified. */ - for (; argc; argc--, args++) { + for (; CMD_ARGC; CMD_ARGC--, CMD_ARGV++) { int tmp = 0; int m; /* gating */ m = RESET_SRST_NO_GATING; - if (strcmp(*args, "srst_gates_jtag") == 0) + if (strcmp(*CMD_ARGV, "srst_gates_jtag") == 0) /* default: don't use JTAG while SRST asserted */; - else if (strcmp(*args, "srst_nogate") == 0) + else if (strcmp(*CMD_ARGV, "srst_nogate") == 0) tmp = RESET_SRST_NO_GATING; else m = 0; if (mask & m) { LOG_ERROR("extra reset_config %s spec (%s)", - "gating", *args); + "gating", *CMD_ARGV); return ERROR_INVALID_ARGUMENTS; } if (m) @@ -733,19 +768,19 @@ COMMAND_HANDLER(handle_reset_config_command) /* signals */ m = RESET_HAS_TRST | RESET_HAS_SRST; - if (strcmp(*args, "none") == 0) + if (strcmp(*CMD_ARGV, "none") == 0) tmp = RESET_NONE; - else if (strcmp(*args, "trst_only") == 0) + else if (strcmp(*CMD_ARGV, "trst_only") == 0) tmp = RESET_HAS_TRST; - else if (strcmp(*args, "srst_only") == 0) + else if (strcmp(*CMD_ARGV, "srst_only") == 0) tmp = RESET_HAS_SRST; - else if (strcmp(*args, "trst_and_srst") == 0) + else if (strcmp(*CMD_ARGV, "trst_and_srst") == 0) tmp = RESET_HAS_TRST | RESET_HAS_SRST; else m = 0; if (mask & m) { LOG_ERROR("extra reset_config %s spec (%s)", - "signal", *args); + "signal", *CMD_ARGV); return ERROR_INVALID_ARGUMENTS; } if (m) @@ -753,19 +788,19 @@ COMMAND_HANDLER(handle_reset_config_command) /* combination (options for broken wiring) */ m = RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST; - if (strcmp(*args, "separate") == 0) + if (strcmp(*CMD_ARGV, "separate") == 0) /* separate reset lines - default */; - else if (strcmp(*args, "srst_pulls_trst") == 0) + else if (strcmp(*CMD_ARGV, "srst_pulls_trst") == 0) tmp |= RESET_SRST_PULLS_TRST; - else if (strcmp(*args, "trst_pulls_srst") == 0) + else if (strcmp(*CMD_ARGV, "trst_pulls_srst") == 0) tmp |= RESET_TRST_PULLS_SRST; - else if (strcmp(*args, "combined") == 0) + else if (strcmp(*CMD_ARGV, "combined") == 0) tmp |= RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST; else m = 0; if (mask & m) { LOG_ERROR("extra reset_config %s spec (%s)", - "combination", *args); + "combination", *CMD_ARGV); return ERROR_INVALID_ARGUMENTS; } if (m) @@ -773,15 +808,15 @@ COMMAND_HANDLER(handle_reset_config_command) /* trst_type (NOP without HAS_TRST) */ m = RESET_TRST_OPEN_DRAIN; - if (strcmp(*args, "trst_open_drain") == 0) + if (strcmp(*CMD_ARGV, "trst_open_drain") == 0) tmp |= RESET_TRST_OPEN_DRAIN; - else if (strcmp(*args, "trst_push_pull") == 0) + else if (strcmp(*CMD_ARGV, "trst_push_pull") == 0) /* push/pull from adapter - default */; else m = 0; if (mask & m) { LOG_ERROR("extra reset_config %s spec (%s)", - "trst_type", *args); + "trst_type", *CMD_ARGV); return ERROR_INVALID_ARGUMENTS; } if (m) @@ -789,22 +824,22 @@ COMMAND_HANDLER(handle_reset_config_command) /* srst_type (NOP without HAS_SRST) */ m |= RESET_SRST_PUSH_PULL; - if (strcmp(*args, "srst_push_pull") == 0) + if (strcmp(*CMD_ARGV, "srst_push_pull") == 0) tmp |= RESET_SRST_PUSH_PULL; - else if (strcmp(*args, "srst_open_drain") == 0) + else if (strcmp(*CMD_ARGV, "srst_open_drain") == 0) /* open drain from adapter - default */; else m = 0; if (mask & m) { LOG_ERROR("extra reset_config %s spec (%s)", - "srst_type", *args); + "srst_type", *CMD_ARGV); return ERROR_INVALID_ARGUMENTS; } if (m) goto next; /* caller provided nonsense; fail */ - LOG_ERROR("unknown reset_config flag (%s)", *args); + LOG_ERROR("unknown reset_config flag (%s)", *CMD_ARGV); return ERROR_INVALID_ARGUMENTS; next: @@ -888,7 +923,7 @@ next: modes[4] = ""; } - command_print(cmd_ctx, "%s %s%s%s%s", + command_print(CMD_CTX, "%s %s%s%s%s", modes[0], modes[1], modes[2], modes[3], modes[4]); @@ -897,74 +932,74 @@ next: COMMAND_HANDLER(handle_jtag_nsrst_delay_command) { - if (argc > 1) + if (CMD_ARGC > 1) return ERROR_COMMAND_SYNTAX_ERROR; - if (argc == 1) + if (CMD_ARGC == 1) { unsigned delay; - COMMAND_PARSE_NUMBER(uint, args[0], delay); + COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], delay); jtag_set_nsrst_delay(delay); } - command_print(cmd_ctx, "jtag_nsrst_delay: %u", jtag_get_nsrst_delay()); + command_print(CMD_CTX, "jtag_nsrst_delay: %u", jtag_get_nsrst_delay()); return ERROR_OK; } COMMAND_HANDLER(handle_jtag_ntrst_delay_command) { - if (argc > 1) + if (CMD_ARGC > 1) return ERROR_COMMAND_SYNTAX_ERROR; - if (argc == 1) + if (CMD_ARGC == 1) { unsigned delay; - COMMAND_PARSE_NUMBER(uint, args[0], delay); + COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], delay); jtag_set_ntrst_delay(delay); } - command_print(cmd_ctx, "jtag_ntrst_delay: %u", jtag_get_ntrst_delay()); + command_print(CMD_CTX, "jtag_ntrst_delay: %u", jtag_get_ntrst_delay()); return ERROR_OK; } COMMAND_HANDLER(handle_jtag_nsrst_assert_width_command) { - if (argc > 1) + if (CMD_ARGC > 1) return ERROR_COMMAND_SYNTAX_ERROR; - if (argc == 1) + if (CMD_ARGC == 1) { unsigned delay; - COMMAND_PARSE_NUMBER(uint, args[0], delay); + COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], delay); jtag_set_nsrst_assert_width(delay); } - command_print(cmd_ctx, "jtag_nsrst_assert_width: %u", jtag_get_nsrst_assert_width()); + command_print(CMD_CTX, "jtag_nsrst_assert_width: %u", jtag_get_nsrst_assert_width()); return ERROR_OK; } COMMAND_HANDLER(handle_jtag_ntrst_assert_width_command) { - if (argc > 1) + if (CMD_ARGC > 1) return ERROR_COMMAND_SYNTAX_ERROR; - if (argc == 1) + if (CMD_ARGC == 1) { unsigned delay; - COMMAND_PARSE_NUMBER(uint, args[0], delay); + COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], delay); jtag_set_ntrst_assert_width(delay); } - command_print(cmd_ctx, "jtag_ntrst_assert_width: %u", jtag_get_ntrst_assert_width()); + command_print(CMD_CTX, "jtag_ntrst_assert_width: %u", jtag_get_ntrst_assert_width()); return ERROR_OK; } COMMAND_HANDLER(handle_jtag_khz_command) { - if (argc > 1) + if (CMD_ARGC > 1) return ERROR_COMMAND_SYNTAX_ERROR; int retval = ERROR_OK; - if (argc == 1) + if (CMD_ARGC == 1) { unsigned khz = 0; - COMMAND_PARSE_NUMBER(uint, args[0], khz); + COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], khz); retval = jtag_config_khz(khz); if (ERROR_OK != retval) @@ -977,23 +1012,23 @@ COMMAND_HANDLER(handle_jtag_khz_command) return retval; if (cur_speed) - command_print(cmd_ctx, "%d kHz", cur_speed); + command_print(CMD_CTX, "%d kHz", cur_speed); else - command_print(cmd_ctx, "RCLK - adaptive"); + command_print(CMD_CTX, "RCLK - adaptive"); return retval; } COMMAND_HANDLER(handle_jtag_rclk_command) { - if (argc > 1) + if (CMD_ARGC > 1) return ERROR_COMMAND_SYNTAX_ERROR; int retval = ERROR_OK; - if (argc == 1) + if (CMD_ARGC == 1) { unsigned khz = 0; - COMMAND_PARSE_NUMBER(uint, args[0], khz); + COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], khz); retval = jtag_config_rclk(khz); if (ERROR_OK != retval) @@ -1006,35 +1041,35 @@ COMMAND_HANDLER(handle_jtag_rclk_command) return retval; if (cur_khz) - command_print(cmd_ctx, "RCLK not supported - fallback to %d kHz", cur_khz); + command_print(CMD_CTX, "RCLK not supported - fallback to %d kHz", cur_khz); else - command_print(cmd_ctx, "RCLK - adaptive"); + command_print(CMD_CTX, "RCLK - adaptive"); return retval; } COMMAND_HANDLER(handle_jtag_reset_command) { - if (argc != 2) + if (CMD_ARGC != 2) return ERROR_COMMAND_SYNTAX_ERROR; int trst = -1; - if (args[0][0] == '1') + if (CMD_ARGV[0][0] == '1') trst = 1; - else if (args[0][0] == '0') + else if (CMD_ARGV[0][0] == '0') trst = 0; else return ERROR_COMMAND_SYNTAX_ERROR; int srst = -1; - if (args[1][0] == '1') + if (CMD_ARGV[1][0] == '1') srst = 1; - else if (args[1][0] == '0') + else if (CMD_ARGV[1][0] == '0') srst = 0; else return ERROR_COMMAND_SYNTAX_ERROR; - if (jtag_interface_init(cmd_ctx) != ERROR_OK) + if (jtag_interface_init(CMD_CTX) != ERROR_OK) return ERROR_JTAG_INIT_FAILED; jtag_add_reset(trst, srst); @@ -1043,11 +1078,11 @@ COMMAND_HANDLER(handle_jtag_reset_command) COMMAND_HANDLER(handle_runtest_command) { - if (argc != 1) + if (CMD_ARGC != 1) return ERROR_COMMAND_SYNTAX_ERROR; unsigned num_clocks; - COMMAND_PARSE_NUMBER(uint, args[0], num_clocks); + COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], num_clocks); jtag_add_runtest(num_clocks, TAP_IDLE); return jtag_execute_queue(); @@ -1078,11 +1113,11 @@ static bool scan_is_safe(tap_state_t state) COMMAND_HANDLER(handle_irscan_command) { int i; - scan_field_t *fields; - jtag_tap_t *tap; + struct scan_field *fields; + struct jtag_tap *tap; tap_state_t endstate; - if ((argc < 2) || (argc % 2)) + if ((CMD_ARGC < 2) || (CMD_ARGC % 2)) { return ERROR_COMMAND_SYNTAX_ERROR; } @@ -1093,46 +1128,46 @@ COMMAND_HANDLER(handle_irscan_command) */ endstate = TAP_IDLE; - if (argc >= 4) { + if (CMD_ARGC >= 4) { /* have at least one pair of numbers. */ /* is last pair the magic text? */ - if (strcmp("-endstate", args[argc - 2]) == 0) { - endstate = tap_state_by_name(args[argc - 1]); + if (strcmp("-endstate", CMD_ARGV[CMD_ARGC - 2]) == 0) { + endstate = tap_state_by_name(CMD_ARGV[CMD_ARGC - 1]); if (endstate == TAP_INVALID) return ERROR_COMMAND_SYNTAX_ERROR; if (!scan_is_safe(endstate)) LOG_WARNING("unstable irscan endstate \"%s\"", - args[argc - 1]); - argc -= 2; + CMD_ARGV[CMD_ARGC - 1]); + CMD_ARGC -= 2; } } - int num_fields = argc / 2; - size_t fields_len = sizeof(scan_field_t) * num_fields; + int num_fields = CMD_ARGC / 2; + size_t fields_len = sizeof(struct scan_field) * num_fields; fields = malloc(fields_len); memset(fields, 0, fields_len); int retval; for (i = 0; i < num_fields; i++) { - tap = jtag_tap_by_string(args[i*2]); + tap = jtag_tap_by_string(CMD_ARGV[i*2]); if (tap == NULL) { int j; for (j = 0; j < i; j++) free(fields[j].out_value); free(fields); - command_print(cmd_ctx, "Tap: %s unknown", args[i*2]); + command_print(CMD_CTX, "Tap: %s unknown", CMD_ARGV[i*2]); return ERROR_FAIL; } int field_size = tap->ir_length; fields[i].tap = tap; fields[i].num_bits = field_size; - fields[i].out_value = malloc(CEIL(field_size, 8)); + fields[i].out_value = malloc(DIV_ROUND_UP(field_size, 8)); uint32_t value; - retval = parse_u32(args[i * 2 + 1], &value); + retval = parse_u32(CMD_ARGV[i * 2 + 1], &value); if (ERROR_OK != retval) goto error_return; buf_set_u32(fields[i].out_value, 0, field_size, value); @@ -1159,11 +1194,11 @@ error_return: static int Jim_Command_drscan(Jim_Interp *interp, int argc, Jim_Obj *const *args) { int retval; - scan_field_t *fields; + struct scan_field *fields; int num_fields; int field_count = 0; int i, e; - jtag_tap_t *tap; + struct jtag_tap *tap; tap_state_t endstate; /* args[1] = device @@ -1245,7 +1280,7 @@ static int Jim_Command_drscan(Jim_Interp *interp, int argc, Jim_Obj *const *args } num_fields = (argc-2)/2; - fields = malloc(sizeof(scan_field_t) * num_fields); + fields = malloc(sizeof(struct scan_field) * num_fields); for (i = 2; i < argc; i += 2) { long bits; @@ -1257,7 +1292,7 @@ static int Jim_Command_drscan(Jim_Interp *interp, int argc, Jim_Obj *const *args fields[field_count].tap = tap; fields[field_count].num_bits = bits; - fields[field_count].out_value = malloc(CEIL(bits, 8)); + fields[field_count].out_value = malloc(DIV_ROUND_UP(bits, 8)); str_to_buf(str, len, fields[field_count].out_value, bits, 0); fields[field_count].in_value = fields[field_count].out_value; field_count++; @@ -1300,7 +1335,7 @@ static int Jim_Command_pathmove(Jim_Interp *interp, int argc, Jim_Obj *const *ar { tap_state_t states[8]; - if ((argc < 2) || ((size_t)argc > (sizeof(states)/sizeof(*states) + 1))) + if ((argc < 2) || ((size_t)argc > (ARRAY_SIZE(states) + 1))) { Jim_WrongNumArgs(interp, 1, args, "wrong arguments"); return JIM_ERR; @@ -1352,57 +1387,51 @@ static int Jim_Command_flush_count(Jim_Interp *interp, int argc, Jim_Obj *const COMMAND_HANDLER(handle_verify_ircapture_command) { - if (argc > 1) + if (CMD_ARGC > 1) return ERROR_COMMAND_SYNTAX_ERROR; - if (argc == 1) + if (CMD_ARGC == 1) { - if (strcmp(args[0], "enable") == 0) - jtag_set_verify_capture_ir(true); - else if (strcmp(args[0], "disable") == 0) - jtag_set_verify_capture_ir(false); - else - return ERROR_COMMAND_SYNTAX_ERROR; + bool enable; + COMMAND_PARSE_ENABLE(CMD_ARGV[0], enable); + jtag_set_verify_capture_ir(enable); } const char *status = jtag_will_verify_capture_ir() ? "enabled": "disabled"; - command_print(cmd_ctx, "verify Capture-IR is %s", status); + command_print(CMD_CTX, "verify Capture-IR is %s", status); return ERROR_OK; } COMMAND_HANDLER(handle_verify_jtag_command) { - if (argc > 1) + if (CMD_ARGC > 1) return ERROR_COMMAND_SYNTAX_ERROR; - if (argc == 1) + if (CMD_ARGC == 1) { - if (strcmp(args[0], "enable") == 0) - jtag_set_verify(true); - else if (strcmp(args[0], "disable") == 0) - jtag_set_verify(false); - else - return ERROR_COMMAND_SYNTAX_ERROR; + bool enable; + COMMAND_PARSE_ENABLE(CMD_ARGV[0], enable); + jtag_set_verify(enable); } const char *status = jtag_will_verify() ? "enabled": "disabled"; - command_print(cmd_ctx, "verify jtag capture is %s", status); + command_print(CMD_CTX, "verify jtag capture is %s", status); return ERROR_OK; } COMMAND_HANDLER(handle_tms_sequence_command) { - if (argc > 1) + if (CMD_ARGC > 1) return ERROR_COMMAND_SYNTAX_ERROR; - if (argc == 1) + if (CMD_ARGC == 1) { bool use_new_table; - if (strcmp(args[0], "short") == 0) + if (strcmp(CMD_ARGV[0], "short") == 0) use_new_table = true; - else if (strcmp(args[0], "long") == 0) + else if (strcmp(CMD_ARGV[0], "long") == 0) use_new_table = false; else return ERROR_COMMAND_SYNTAX_ERROR; @@ -1410,95 +1439,159 @@ COMMAND_HANDLER(handle_tms_sequence_command) tap_use_new_tms_table(use_new_table); } - command_print(cmd_ctx, "tms sequence is %s", + command_print(CMD_CTX, "tms sequence is %s", tap_uses_new_tms_table() ? "short": "long"); return ERROR_OK; } -int jtag_register_commands(struct command_context_s *cmd_ctx) -{ - register_jim(cmd_ctx, "jtag", jim_jtag_command, - "perform jtag tap actions"); - - register_command(cmd_ctx, NULL, "interface", - handle_interface_command, COMMAND_CONFIG, - "try to configure interface"); - register_command(cmd_ctx, NULL, "interface_list", - &handle_interface_list_command, COMMAND_ANY, - "list all built-in interfaces"); - - register_command(cmd_ctx, NULL, "jtag_khz", - handle_jtag_khz_command, COMMAND_ANY, - "set maximum jtag speed (if supported); " - "parameter is maximum khz, or 0 for adaptive clocking (RTCK)."); - register_command(cmd_ctx, NULL, "jtag_rclk", - handle_jtag_rclk_command, COMMAND_ANY, - "fallback_speed_khz - set JTAG speed to RCLK or use fallback speed"); - register_command(cmd_ctx, NULL, "reset_config", - handle_reset_config_command, COMMAND_ANY, - "reset_config " - "[none|trst_only|srst_only|trst_and_srst] " +static const struct command_registration jtag_command_handlers[] = { + { + .name = "interface", + .handler = &handle_interface_command, + .mode = COMMAND_CONFIG, + .help = "select a JTAG interface", + .usage = "", + }, + { + .name = "interface_list", + .handler = &handle_interface_list_command, + .mode = COMMAND_ANY, + .help = "list all built-in interfaces", + }, + { + .name = "jtag_khz", + .handler = &handle_jtag_khz_command, + .mode = COMMAND_ANY, + .help = "set maximum jtag speed (if supported)", + .usage = "", + }, + { + .name = "jtag_rclk", + .handler = &handle_jtag_rclk_command, + .mode = COMMAND_ANY, + .help = "set JTAG speed to RCLK or use fallback speed", + .usage = "", + }, + { + .name = "reset_config", + .handler = &handle_reset_config_command, + .mode = COMMAND_ANY, + .help = "configure JTAG reset behavior", + .usage = "[none|trst_only|srst_only|trst_and_srst] " "[srst_pulls_trst|trst_pulls_srst|combined|separate] " "[srst_gates_jtag|srst_nogate] " "[trst_push_pull|trst_open_drain] " - "[srst_push_pull|srst_open_drain]"); - - register_command(cmd_ctx, NULL, "jtag_nsrst_delay", - handle_jtag_nsrst_delay_command, COMMAND_ANY, - "jtag_nsrst_delay " - "- delay after deasserting srst in ms"); - register_command(cmd_ctx, NULL, "jtag_ntrst_delay", - handle_jtag_ntrst_delay_command, COMMAND_ANY, - "jtag_ntrst_delay " - "- delay after deasserting trst in ms"); - - register_command(cmd_ctx, NULL, "jtag_nsrst_assert_width", - handle_jtag_nsrst_assert_width_command, COMMAND_ANY, - "jtag_nsrst_assert_width " - "- delay after asserting srst in ms"); - register_command(cmd_ctx, NULL, "jtag_ntrst_assert_width", - handle_jtag_ntrst_assert_width_command, COMMAND_ANY, - "jtag_ntrst_assert_width " - "- delay after asserting trst in ms"); - - register_command(cmd_ctx, NULL, "scan_chain", - handle_scan_chain_command, COMMAND_EXEC, - "print current scan chain configuration"); - - register_command(cmd_ctx, NULL, "jtag_reset", - handle_jtag_reset_command, COMMAND_EXEC, - "toggle reset lines "); - register_command(cmd_ctx, NULL, "runtest", - handle_runtest_command, COMMAND_EXEC, - "move to Run-Test/Idle, and execute "); - register_command(cmd_ctx, NULL, "irscan", - handle_irscan_command, COMMAND_EXEC, - "execute IR scan [dev2] [instr2] ..."); - - register_jim(cmd_ctx, "drscan", Jim_Command_drscan, - "execute DR scan " - " ..."); - - register_jim(cmd_ctx, "flush_count", Jim_Command_flush_count, - "returns number of times the JTAG queue has been flushed"); - - register_jim(cmd_ctx, "pathmove", Jim_Command_pathmove, - ",,... " - "- move JTAG to state1 then to state2, state3, etc."); - - register_command(cmd_ctx, NULL, "verify_ircapture", - handle_verify_ircapture_command, COMMAND_ANY, - "verify value captured during Capture-IR "); - register_command(cmd_ctx, NULL, "verify_jtag", - handle_verify_jtag_command, COMMAND_ANY, - "verify value capture "); - - register_command(cmd_ctx, NULL, "tms_sequence", - handle_tms_sequence_command, COMMAND_ANY, - "choose short(default) or long tms_sequence "); - - return ERROR_OK; -} + "[srst_push_pull|srst_open_drain]", + }, + { + .name = "jtag_nsrst_delay", + .handler = &handle_jtag_nsrst_delay_command, + .mode = COMMAND_ANY, + .help = "delay after deasserting srst in ms", + .usage = "", + }, + { + .name = "jtag_ntrst_delay", + .handler = &handle_jtag_ntrst_delay_command, + .mode = COMMAND_ANY, + .help = "delay after deasserting trst in ms", + .usage = "" + }, + { + .name = "jtag_nsrst_assert_width", + .handler = &handle_jtag_nsrst_assert_width_command, + .mode = COMMAND_ANY, + .help = "delay after asserting srst in ms", + .usage = "" + }, + { + .name = "jtag_ntrst_assert_width", + .handler = &handle_jtag_ntrst_assert_width_command, + .mode = COMMAND_ANY, + .help = "delay after asserting trst in ms", + .usage = "" + }, + { + .name = "scan_chain", + .handler = &handle_scan_chain_command, + .mode = COMMAND_EXEC, + .help = "print current scan chain configuration", + }, + { + .name = "jtag_reset", + .handler = &handle_jtag_reset_command, + .mode = COMMAND_EXEC, + .help = "toggle reset lines", + .usage = " ", + }, + { + .name = "runtest", + .handler = &handle_runtest_command, + .mode = COMMAND_EXEC, + .help = "move to Run-Test/Idle, and execute ", + .usage = "" + }, + { + .name = "irscan", + .handler = &handle_irscan_command, + .mode = COMMAND_EXEC, + .help = "execute IR scan", + .usage = " [dev2] [instr2] ...", + }, + { + .name = "verify_ircapture", + .handler = &handle_verify_ircapture_command, + .mode = COMMAND_ANY, + .help = "verify value captured during Capture-IR", + .usage = "", + }, + { + .name = "verify_jtag", + .handler = &handle_verify_jtag_command, + .mode = COMMAND_ANY, + .help = "verify value capture", + .usage = "", + }, + { + .name = "tms_sequence", + .handler = &handle_tms_sequence_command, + .mode = COMMAND_ANY, + .help = "choose short(default) or long tms_sequence", + .usage = "", + }, + // jim commands + { + .name = "jtag", + .mode = COMMAND_ANY, + .help = "perform jtag tap actions", + .chain = jtag_subcommand_handlers, + }, + { + .name = "drscan", + .mode = COMMAND_EXEC, + .jim_handler = &Jim_Command_drscan, + .help = "execute DR scan " + " ...", + }, + { + .name = "flush_count", + .mode = COMMAND_EXEC, + .jim_handler = &Jim_Command_flush_count, + .help = "returns number of times the JTAG queue has been flushed", + }, + { + .name = "pathmove", + .mode = COMMAND_EXEC, + .jim_handler = &Jim_Command_pathmove, + .usage = ",,... ", + .help = "move JTAG to state1 then to state2, state3, etc.", + }, + COMMAND_REGISTRATION_DONE +}; +int jtag_register_commands(struct command_context *cmd_ctx) +{ + return register_commands(cmd_ctx, NULL, jtag_command_handlers); +}