target: fix halt and wait_halt timeout units
[openocd.git] / src / target / target.c
index dbac9a1fba0ebaf340a51ad191fb296e6a80cd1d..1f517ac068c0a17fd2bbc649b99e3e4f5f7e105c 100644 (file)
@@ -36,7 +36,7 @@
  *   You should have received a copy of the GNU General Public License     *
  *   along with this program; if not, write to the                         *
  *   Free Software Foundation, Inc.,                                       *
- *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
+ *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.           *
  ***************************************************************************/
 
 #ifdef HAVE_CONFIG_H
@@ -56,6 +56,9 @@
 #include "image.h"
 #include "rtos/rtos.h"
 
+/* default halt wait timeout (ms) */
+#define DEFAULT_HALT_TIMEOUT 5000
+
 static int target_read_buffer_default(struct target *target, uint32_t address,
                uint32_t size, uint8_t *buffer);
 static int target_write_buffer_default(struct target *target, uint32_t address,
@@ -80,6 +83,7 @@ extern struct target_type dragonite_target;
 extern struct target_type xscale_target;
 extern struct target_type cortexm3_target;
 extern struct target_type cortexa8_target;
+extern struct target_type cortexr4_target;
 extern struct target_type arm11_target;
 extern struct target_type mips_m4k_target;
 extern struct target_type avr_target;
@@ -87,7 +91,10 @@ extern struct target_type dsp563xx_target;
 extern struct target_type dsp5680xx_target;
 extern struct target_type testee_target;
 extern struct target_type avr32_ap7k_target;
-extern struct target_type stm32_stlink_target;
+extern struct target_type hla_target;
+extern struct target_type nds32_v2_target;
+extern struct target_type nds32_v3_target;
+extern struct target_type nds32_v3m_target;
 
 static struct target_type *target_types[] = {
        &arm7tdmi_target,
@@ -103,6 +110,7 @@ static struct target_type *target_types[] = {
        &xscale_target,
        &cortexm3_target,
        &cortexa8_target,
+       &cortexr4_target,
        &arm11_target,
        &mips_m4k_target,
        &avr_target,
@@ -110,7 +118,10 @@ static struct target_type *target_types[] = {
        &dsp5680xx_target,
        &testee_target,
        &avr32_ap7k_target,
-       &stm32_stlink_target,
+       &hla_target,
+       &nds32_v2_target,
+       &nds32_v3_target,
+       &nds32_v3m_target,
        NULL,
 };
 
@@ -379,9 +390,9 @@ struct target *get_target(const char *id)
 
        /* try as tcltarget name */
        for (target = all_targets; target; target = target->next) {
-               if (target->cmd_name == NULL)
+               if (target_name(target) == NULL)
                        continue;
-               if (strcmp(id, target->cmd_name) == 0)
+               if (strcmp(id, target_name(target)) == 0)
                        return target;
        }
 
@@ -395,7 +406,7 @@ struct target *get_target(const char *id)
        for (target = all_targets; target; target = target->next) {
                if (target->target_number == (int)num) {
                        LOG_WARNING("use '%s' as target identifier, not '%u'",
-                                       target->cmd_name, num);
+                                       target_name(target), num);
                        return target;
                }
        }
@@ -448,7 +459,7 @@ int target_poll(struct target *target)
                        target->halt_issued = false;
                else {
                        long long t = timeval_ms() - target->halt_issued_time;
-                       if (t > 1000) {
+                       if (t > DEFAULT_HALT_TIMEOUT) {
                                target->halt_issued = false;
                                LOG_INFO("Halt timed out, wake up GDB.");
                                target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
@@ -657,38 +668,18 @@ const char *target_type_name(struct target *target)
        return target->type->name;
 }
 
-static int target_write_memory_imp(struct target *target, uint32_t address,
-               uint32_t size, uint32_t count, const uint8_t *buffer)
-{
-       if (!target_was_examined(target)) {
-               LOG_ERROR("Target not examined yet");
-               return ERROR_FAIL;
-       }
-       return target->type->write_memory_imp(target, address, size, count, buffer);
-}
-
-static int target_read_memory_imp(struct target *target, uint32_t address,
-               uint32_t size, uint32_t count, uint8_t *buffer)
-{
-       if (!target_was_examined(target)) {
-               LOG_ERROR("Target not examined yet");
-               return ERROR_FAIL;
-       }
-       return target->type->read_memory_imp(target, address, size, count, buffer);
-}
-
-static int target_soft_reset_halt_imp(struct target *target)
+static int target_soft_reset_halt(struct target *target)
 {
        if (!target_was_examined(target)) {
                LOG_ERROR("Target not examined yet");
                return ERROR_FAIL;
        }
-       if (!target->type->soft_reset_halt_imp) {
+       if (!target->type->soft_reset_halt) {
                LOG_ERROR("Target %s does not support soft_reset_halt",
                                target_name(target));
                return ERROR_FAIL;
        }
-       return target->type->soft_reset_halt_imp(target);
+       return target->type->soft_reset_halt(target);
 }
 
 /**
@@ -822,6 +813,7 @@ int target_run_flash_async_algorithm(struct target *target,
                uint32_t entry_point, uint32_t exit_point, void *arch_info)
 {
        int retval;
+       int timeout = 0;
 
        /* Set up working area. First word is write pointer, second word is read pointer,
         * rest is fifo data area. */
@@ -893,9 +885,19 @@ int target_run_flash_async_algorithm(struct target *target,
                         * less than buffer size / flash speed. This is very unlikely to
                         * run when using high latency connections such as USB. */
                        alive_sleep(10);
+
+                       /* to stop an infinite loop on some targets check and increment a timeout
+                        * this issue was observed on a stellaris using the new ICDI interface */
+                       if (timeout++ >= 500) {
+                               LOG_ERROR("timeout waiting for algorithm, a target reset is recommended");
+                               return ERROR_FLASH_OPERATION_FAILED;
+                       }
                        continue;
                }
 
+               /* reset our timeout */
+               timeout = 0;
+
                /* Limit to the amount of data we actually want to write */
                if (thisrun_bytes > count * block_size)
                        thisrun_bytes = count * block_size;
@@ -940,38 +942,54 @@ int target_run_flash_async_algorithm(struct target *target,
 int target_read_memory(struct target *target,
                uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
 {
+       if (!target_was_examined(target)) {
+               LOG_ERROR("Target not examined yet");
+               return ERROR_FAIL;
+       }
        return target->type->read_memory(target, address, size, count, buffer);
 }
 
-static int target_read_phys_memory(struct target *target,
+int target_read_phys_memory(struct target *target,
                uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
 {
+       if (!target_was_examined(target)) {
+               LOG_ERROR("Target not examined yet");
+               return ERROR_FAIL;
+       }
        return target->type->read_phys_memory(target, address, size, count, buffer);
 }
 
 int target_write_memory(struct target *target,
                uint32_t address, uint32_t size, uint32_t count, const uint8_t *buffer)
 {
+       if (!target_was_examined(target)) {
+               LOG_ERROR("Target not examined yet");
+               return ERROR_FAIL;
+       }
        return target->type->write_memory(target, address, size, count, buffer);
 }
 
-static int target_write_phys_memory(struct target *target,
+int target_write_phys_memory(struct target *target,
                uint32_t address, uint32_t size, uint32_t count, const uint8_t *buffer)
 {
+       if (!target_was_examined(target)) {
+               LOG_ERROR("Target not examined yet");
+               return ERROR_FAIL;
+       }
        return target->type->write_phys_memory(target, address, size, count, buffer);
 }
 
-int target_bulk_write_memory(struct target *target,
+static int target_bulk_write_memory_default(struct target *target,
                uint32_t address, uint32_t count, const uint8_t *buffer)
 {
-       return target->type->bulk_write_memory(target, address, count, buffer);
+       return target_write_memory(target, address, 4, count, buffer);
 }
 
 int target_add_breakpoint(struct target *target,
                struct breakpoint *breakpoint)
 {
        if ((target->state != TARGET_HALTED) && (breakpoint->type != BKPT_HARD)) {
-               LOG_WARNING("target %s is not halted", target->cmd_name);
+               LOG_WARNING("target %s is not halted", target_name(target));
                return ERROR_TARGET_NOT_HALTED;
        }
        return target->type->add_breakpoint(target, breakpoint);
@@ -981,7 +999,7 @@ int target_add_context_breakpoint(struct target *target,
                struct breakpoint *breakpoint)
 {
        if (target->state != TARGET_HALTED) {
-               LOG_WARNING("target %s is not halted", target->cmd_name);
+               LOG_WARNING("target %s is not halted", target_name(target));
                return ERROR_TARGET_NOT_HALTED;
        }
        return target->type->add_context_breakpoint(target, breakpoint);
@@ -991,7 +1009,7 @@ int target_add_hybrid_breakpoint(struct target *target,
                struct breakpoint *breakpoint)
 {
        if (target->state != TARGET_HALTED) {
-               LOG_WARNING("target %s is not halted", target->cmd_name);
+               LOG_WARNING("target %s is not halted", target_name(target));
                return ERROR_TARGET_NOT_HALTED;
        }
        return target->type->add_hybrid_breakpoint(target, breakpoint);
@@ -1007,7 +1025,7 @@ int target_add_watchpoint(struct target *target,
                struct watchpoint *watchpoint)
 {
        if (target->state != TARGET_HALTED) {
-               LOG_WARNING("target %s is not halted", target->cmd_name);
+               LOG_WARNING("target %s is not halted", target_name(target));
                return ERROR_TARGET_NOT_HALTED;
        }
        return target->type->add_watchpoint(target, watchpoint);
@@ -1074,23 +1092,6 @@ static int target_init_one(struct command_context *cmd_ctx,
                return retval;
        }
 
-       /**
-        * @todo get rid of those *memory_imp() methods, now that all
-        * callers are using target_*_memory() accessors ... and make
-        * sure the "physical" paths handle the same issues.
-        */
-       /* a non-invasive way(in terms of patches) to add some code that
-        * runs before the type->write/read_memory implementation
-        */
-       type->write_memory_imp = target->type->write_memory;
-       type->write_memory = target_write_memory_imp;
-
-       type->read_memory_imp = target->type->read_memory;
-       type->read_memory = target_read_memory_imp;
-
-       type->soft_reset_halt_imp = target->type->soft_reset_halt;
-       type->soft_reset_halt = target_soft_reset_halt_imp;
-
        /* Sanity-check MMU support ... stub in what we must, to help
         * implement it in stages, but warn if we need to do so.
         */
@@ -1129,6 +1130,9 @@ static int target_init_one(struct command_context *cmd_ctx,
        if (target->type->write_buffer == NULL)
                target->type->write_buffer = target_write_buffer_default;
 
+       if (target->type->bulk_write_memory == NULL)
+               target->type->bulk_write_memory = target_bulk_write_memory_default;
+
        return ERROR_OK;
 }
 
@@ -2153,9 +2157,6 @@ static int sense_handler(void)
        return ERROR_OK;
 }
 
-static int backoff_times;
-static int backoff_count;
-
 /* process target state changes */
 static int handle_target(void *priv)
 {
@@ -2211,13 +2212,6 @@ static int handle_target(void *priv)
                recursive = 0;
        }
 
-       if (backoff_times > backoff_count) {
-               /* do not poll this time as we failed previously */
-               backoff_count++;
-               return ERROR_OK;
-       }
-       backoff_count = 0;
-
        /* Poll targets for state changes unless that's globally disabled.
         * Skip targets that are currently disabled.
         */
@@ -2227,18 +2221,26 @@ static int handle_target(void *priv)
                if (!target->tap->enabled)
                        continue;
 
+               if (target->backoff.times > target->backoff.count) {
+                       /* do not poll this time as we failed previously */
+                       target->backoff.count++;
+                       continue;
+               }
+               target->backoff.count = 0;
+
                /* only poll target if we've got power and srst isn't asserted */
                if (!powerDropout && !srstAsserted) {
                        /* polling may fail silently until the target has been examined */
                        retval = target_poll(target);
                        if (retval != ERROR_OK) {
                                /* 100ms polling interval. Increase interval between polling up to 5000ms */
-                               if (backoff_times * polling_interval < 5000) {
-                                       backoff_times *= 2;
-                                       backoff_times++;
+                               if (target->backoff.times * polling_interval < 5000) {
+                                       target->backoff.times *= 2;
+                                       target->backoff.times++;
                                }
-                               LOG_USER("Polling target failed, GDB will be halted. Polling again in %dms",
-                                               backoff_times * polling_interval);
+                               LOG_USER("Polling target %s failed, GDB will be halted. Polling again in %dms",
+                                               target_name(target),
+                                               target->backoff.times * polling_interval);
 
                                /* Tell GDB to halt the debugger. This allows the user to
                                 * run monitor commands to handle the situation.
@@ -2247,9 +2249,9 @@ static int handle_target(void *priv)
                                return retval;
                        }
                        /* Since we succeeded, we reset backoff count */
-                       if (backoff_times > 0)
-                               LOG_USER("Polling succeeded again");
-                       backoff_times = 0;
+                       if (target->backoff.times > 0)
+                               LOG_USER("Polling target %s succeeded again", target_name(target));
+                       target->backoff.times = 0;
                }
        }
 
@@ -2410,13 +2412,11 @@ COMMAND_HANDLER(handle_wait_halt_command)
        if (CMD_ARGC > 1)
                return ERROR_COMMAND_SYNTAX_ERROR;
 
-       unsigned ms = 5000;
+       unsigned ms = DEFAULT_HALT_TIMEOUT;
        if (1 == CMD_ARGC) {
                int retval = parse_uint(CMD_ARGV[0], &ms);
                if (ERROR_OK != retval)
                        return ERROR_COMMAND_SYNTAX_ERROR;
-               /* convert seconds (given) to milliseconds (needed) */
-               ms *= 1000;
        }
 
        struct target *target = get_current_target(CMD_CTX);
@@ -2489,7 +2489,7 @@ COMMAND_HANDLER(handle_soft_reset_halt_command)
 
        LOG_USER("requesting target halt and executing a soft reset");
 
-       target->type->soft_reset_halt(target);
+       target_soft_reset_halt(target);
 
        return ERROR_OK;
 }
@@ -4447,32 +4447,32 @@ static int jim_target_md(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
                        return JIM_ERR;
                }
 
-               command_print(NULL, "0x%08x ", (int)(addr));
+               command_print_sameline(NULL, "0x%08x ", (int)(addr));
                switch (dwidth) {
                case 4:
                        for (x = 0; x < 16 && x < y; x += 4) {
                                z = target_buffer_get_u32(target, &(target_buf[x]));
-                               command_print(NULL, "%08x ", (int)(z));
+                               command_print_sameline(NULL, "%08x ", (int)(z));
                        }
                        for (; (x < 16) ; x += 4)
-                               command_print(NULL, "         ");
+                               command_print_sameline(NULL, "         ");
                        break;
                case 2:
                        for (x = 0; x < 16 && x < y; x += 2) {
                                z = target_buffer_get_u16(target, &(target_buf[x]));
-                               command_print(NULL, "%04x ", (int)(z));
+                               command_print_sameline(NULL, "%04x ", (int)(z));
                        }
                        for (; (x < 16) ; x += 2)
-                               command_print(NULL, "     ");
+                               command_print_sameline(NULL, "     ");
                        break;
                case 1:
                default:
                        for (x = 0 ; (x < 16) && (x < y) ; x += 1) {
                                z = target_buffer_get_u8(target, &(target_buf[x]));
-                               command_print(NULL, "%02x ", (int)(z));
+                               command_print_sameline(NULL, "%02x ", (int)(z));
                        }
                        for (; (x < 16) ; x += 1)
-                               command_print(NULL, "   ");
+                               command_print_sameline(NULL, "   ");
                        break;
                }
                /* ascii-ify the bytes */
@@ -4493,7 +4493,7 @@ static int jim_target_md(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
                /* terminate */
                target_buf[16] = 0;
                /* print - with a newline */
-               command_print(NULL, "%s\n", target_buf);
+               command_print_sameline(NULL, "%s\n", target_buf);
                /* NEXT... */
                bytes -= 16;
                addr += 16;
@@ -4898,6 +4898,15 @@ static int target_create(Jim_GetOptInfo *goi)
                        /* found */
                        break;
                }
+
+               /* check for deprecated name */
+               if (target_types[x]->deprecated_name) {
+                       if (0 == strcmp(cp, target_types[x]->deprecated_name)) {
+                               /* found */
+                               LOG_WARNING("target name is deprecated use: \'%s\'", target_types[x]->name);
+                               break;
+                       }
+               }
        }
        if (target_types[x] == NULL) {
                Jim_SetResultFormatted(goi->interp, "Unknown target type %s, try one of ", cp);
@@ -5014,7 +5023,7 @@ static int target_create(Jim_GetOptInfo *goi)
        }
 
        /* now - create the new target name command */
-       const const struct command_registration target_subcommands[] = {
+       const struct command_registration target_subcommands[] = {
                {
                        .chain = target_instance_command_handlers,
                },
@@ -5023,7 +5032,7 @@ static int target_create(Jim_GetOptInfo *goi)
                },
                COMMAND_REGISTRATION_DONE
        };
-       const const struct command_registration target_commands[] = {
+       const struct command_registration target_commands[] = {
                {
                        .name = cp,
                        .mode = COMMAND_ANY,
@@ -5053,7 +5062,7 @@ static int jim_target_current(Jim_Interp *interp, int argc, Jim_Obj *const *argv
        struct command_context *cmd_ctx = current_command_context(interp);
        assert(cmd_ctx != NULL);
 
-       Jim_SetResultString(interp, get_current_target(cmd_ctx)->cmd_name, -1);
+       Jim_SetResultString(interp, target_name(get_current_target(cmd_ctx)), -1);
        return JIM_OK;
 }
 
@@ -5131,8 +5140,10 @@ static int jim_target_smp(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
                target->head = head;
                curr = curr->next;
        }
-       if (target->rtos)
+
+       if (target && target->rtos)
                retval = rtos_smp_init(head->target);
+
        return retval;
 }
 
@@ -5525,7 +5536,7 @@ static const struct command_registration target_exec_command_handlers[] = {
                .handler = handle_wait_halt_command,
                .mode = COMMAND_EXEC,
                .help = "wait up to the specified number of milliseconds "
-                       "(default 5) for a previously requested halt",
+                       "(default 5000) for a previously requested halt",
                .usage = "[milliseconds]",
        },
        {
@@ -5533,7 +5544,7 @@ static const struct command_registration target_exec_command_handlers[] = {
                .handler = handle_halt_command,
                .mode = COMMAND_EXEC,
                .help = "request target to halt, then wait up to the specified"
-                       "number of milliseconds (default 5) for it to complete",
+                       "number of milliseconds (default 5000) for it to complete",
                .usage = "[milliseconds]",
        },
        {

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)