target/tcl: Add set_reg function
[openocd.git] / src / target / target.c
index 6250d303115847b2b911b9062c3bb1525abdc1cb..1d26cb91cbab6edb68638ab0958c44d212af598d 100644 (file)
@@ -4863,6 +4863,57 @@ void target_handle_event(struct target *target, enum target_event e)
        }
 }
 
+static int target_jim_set_reg(Jim_Interp *interp, int argc,
+               Jim_Obj * const *argv)
+{
+       if (argc != 2) {
+               Jim_WrongNumArgs(interp, 1, argv, "dict");
+               return JIM_ERR;
+       }
+
+       int tmp;
+       Jim_Obj **dict = Jim_DictPairs(interp, argv[1], &tmp);
+
+       if (!dict)
+               return JIM_ERR;
+
+       const unsigned int length = tmp;
+       struct command_context *cmd_ctx = current_command_context(interp);
+       assert(cmd_ctx);
+       const struct target *target = get_current_target(cmd_ctx);
+
+       for (unsigned int i = 0; i < length; i += 2) {
+               const char *reg_name = Jim_String(dict[i]);
+               const char *reg_value = Jim_String(dict[i + 1]);
+               struct reg *reg = register_get_by_name(target->reg_cache, reg_name,
+                       false);
+
+               if (!reg || !reg->exist) {
+                       Jim_SetResultFormatted(interp, "unknown register '%s'", reg_name);
+                       return JIM_ERR;
+               }
+
+               uint8_t *buf = malloc(DIV_ROUND_UP(reg->size, 8));
+
+               if (!buf) {
+                       LOG_ERROR("Failed to allocate memory");
+                       return JIM_ERR;
+               }
+
+               str_to_buf(reg_value, strlen(reg_value), buf, reg->size, 0);
+               int retval = reg->type->set(reg, buf);
+               free(buf);
+
+               if (retval != ERROR_OK) {
+                       Jim_SetResultFormatted(interp, "failed to set '%s' to register '%s'",
+                               reg_value, reg_name);
+                       return JIM_ERR;
+               }
+       }
+
+       return JIM_OK;
+}
+
 /**
  * Returns true only if the target has a handler for the specified event.
  */
@@ -5645,6 +5696,13 @@ static const struct command_registration target_instance_command_handlers[] = {
                        "from target memory",
                .usage = "arrayname bitwidth address count",
        },
+       {
+               .name = "set_reg",
+               .mode = COMMAND_EXEC,
+               .jim_handler = target_jim_set_reg,
+               .help = "Set target register values",
+               .usage = "dict",
+       },
        {
                .name = "eventlist",
                .handler = handle_target_event_list,
@@ -6727,6 +6785,13 @@ static const struct command_registration target_exec_command_handlers[] = {
                        "and write the 8/16/32 bit values",
                .usage = "arrayname bitwidth address count",
        },
+       {
+               .name = "set_reg",
+               .mode = COMMAND_EXEC,
+               .jim_handler = target_jim_set_reg,
+               .help = "Set target register values",
+               .usage = "dict",
+       },
        {
                .name = "reset_nag",
                .handler = handle_target_reset_nag,

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)