target: Add default implementation of bulk_write_memory
[openocd.git] / src / target / cortex_m.c
index 74505a85e1b669168640eaae4cf0ec7fa1f8b9c3..c436bad482a592431fa6b8c1d30b6125b2bbd62c 100644 (file)
@@ -61,7 +61,7 @@
 
 /* forward declarations */
 static int cortex_m3_store_core_reg_u32(struct target *target,
-       enum armv7m_regtype type, uint32_t num, uint32_t value);
+               uint32_t num, uint32_t value);
 
 static int cortexm3_dap_read_coreregister_u32(struct adiv5_dap *swjdp,
        uint32_t *value, int regnum)
@@ -298,7 +298,7 @@ static int cortex_m3_endreset_event(struct target *target)
        if (retval != ERROR_OK)
                return retval;
 
-       register_cache_invalidate(cortex_m3->armv7m.core_cache);
+       register_cache_invalidate(armv7m->arm.core_cache);
 
        /* make sure we have latest dhcsr flags */
        retval = mem_ap_read_atomic_u32(swjdp, DCB_DHCSR, &cortex_m3->dcb_dhcsr);
@@ -421,15 +421,16 @@ static int cortex_m3_debug_entry(struct target *target)
                return retval;
 
        /* Examine target state and mode
-        * First load register acessible through core debug port*/
-       int num_regs = armv7m->core_cache->num_regs;
+        * First load register accessible through core debug port */
+       int num_regs = arm->core_cache->num_regs;
 
        for (i = 0; i < num_regs; i++) {
-               if (!armv7m->core_cache->reg_list[i].valid)
-                       armv7m->read_core_reg(target, i);
+               r = &armv7m->arm.core_cache->reg_list[i];
+               if (!r->valid)
+                       arm->read_core_reg(target, r, i, ARM_MODE_ANY);
        }
 
-       r = armv7m->core_cache->reg_list + ARMV7M_xPSR;
+       r = arm->core_cache->reg_list + ARMV7M_xPSR;
        xPSR = buf_get_u32(r->value, 0, 32);
 
 #ifdef ARMV7_GDB_HACKS
@@ -446,23 +447,21 @@ static int cortex_m3_debug_entry(struct target *target)
        /* For IT instructions xPSR must be reloaded on resume and clear on debug exec */
        if (xPSR & 0xf00) {
                r->dirty = r->valid;
-               cortex_m3_store_core_reg_u32(target, ARMV7M_REGISTER_CORE_GP, 16, xPSR & ~0xff);
+               cortex_m3_store_core_reg_u32(target, 16, xPSR & ~0xff);
        }
 
        /* Are we in an exception handler */
        if (xPSR & 0x1FF) {
-               armv7m->core_mode = ARMV7M_MODE_HANDLER;
                armv7m->exception_number = (xPSR & 0x1FF);
 
                arm->core_mode = ARM_MODE_HANDLER;
                arm->map = armv7m_msp_reg_map;
        } else {
-               unsigned control = buf_get_u32(armv7m->core_cache
+               unsigned control = buf_get_u32(arm->core_cache
                                ->reg_list[ARMV7M_CONTROL].value, 0, 2);
 
                /* is this thread privileged? */
-               armv7m->core_mode = control & 1;
-               arm->core_mode = armv7m->core_mode
+               arm->core_mode = control & 1
                        ? ARM_MODE_USER_THREAD
                        : ARM_MODE_THREAD;
 
@@ -479,7 +478,7 @@ static int cortex_m3_debug_entry(struct target *target)
                cortex_m3_examine_exception_reason(target);
 
        LOG_DEBUG("entered debug state in core mode: %s at PC 0x%" PRIx32 ", target->state: %s",
-               armv7m_mode_strings[armv7m->core_mode],
+               arm_mode_name(arm->core_mode),
                *(uint32_t *)(arm->pc->value),
                target_state_name(target));
 
@@ -647,7 +646,7 @@ static int cortex_m3_soft_reset_halt(struct target *target)
        target->state = TARGET_RESET;
 
        /* registers are now invalid */
-       register_cache_invalidate(cortex_m3->armv7m.core_cache);
+       register_cache_invalidate(cortex_m3->armv7m.arm.core_cache);
 
        while (timeout < 100) {
                retval = mem_ap_read_atomic_u32(swjdp, DCB_DHCSR, &dcb_dhcsr);
@@ -677,7 +676,7 @@ static int cortex_m3_soft_reset_halt(struct target *target)
        return ERROR_OK;
 }
 
-static void cortex_m3_enable_breakpoints(struct target *target)
+void cortex_m3_enable_breakpoints(struct target *target)
 {
        struct breakpoint *breakpoint = target->breakpoints;
 
@@ -709,7 +708,7 @@ static int cortex_m3_resume(struct target *target, int current,
        }
 
        if (debug_execution) {
-               r = armv7m->core_cache->reg_list + ARMV7M_PRIMASK;
+               r = armv7m->arm.core_cache->reg_list + ARMV7M_PRIMASK;
 
                /* Disable interrupts */
                /* We disable interrupts in the PRIMASK register instead of
@@ -727,7 +726,7 @@ static int cortex_m3_resume(struct target *target, int current,
                r->valid = true;
 
                /* Make sure we are in Thumb mode */
-               r = armv7m->core_cache->reg_list + ARMV7M_xPSR;
+               r = armv7m->arm.core_cache->reg_list + ARMV7M_xPSR;
                buf_set_u32(r->value, 24, 1, 1);
                r->dirty = true;
                r->valid = true;
@@ -773,7 +772,7 @@ static int cortex_m3_resume(struct target *target, int current,
        target->debug_reason = DBG_REASON_NOTHALTED;
 
        /* registers are now invalid */
-       register_cache_invalidate(armv7m->core_cache);
+       register_cache_invalidate(armv7m->arm.core_cache);
 
        if (!debug_execution) {
                target->state = TARGET_RUNNING;
@@ -854,45 +853,78 @@ static int cortex_m3_step(struct target *target, int current,
                         *
                         */
 
-                       /* Set a temporary break point */
-                       retval = breakpoint_add(target, pc_value, 2, BKPT_TYPE_BY_ADDR(pc_value));
-                       bool tmp_bp_set = (retval == ERROR_OK);
-
-                       /* No more breakpoints left, just do a step */
-                       if (!tmp_bp_set)
+                       /* 2012-09-29 ph
+                        *
+                        * If a break point is already set on the lower half word then a break point on
+                        * the upper half word will not break again when the core is restarted. So we
+                        * just step over the instruction with interrupts disabled.
+                        *
+                        * The documentation has no information about this, it was found by observation
+                        * on STM32F1 and STM32F2. Proper explanation welcome. STM32F0 dosen't seem to
+                        * suffer from this problem.
+                        *
+                        * To add some confusion: pc_value has bit 0 always set, while the breakpoint
+                        * address has it always cleared. The former is done to indicate thumb mode
+                        * to gdb.
+                        *
+                        */
+                       if ((pc_value & 0x02) && breakpoint_find(target, pc_value & ~0x03)) {
+                               LOG_DEBUG("Stepping over next instruction with interrupts disabled");
+                               cortex_m3_write_debug_halt_mask(target, C_HALT | C_MASKINTS, 0);
                                cortex_m3_write_debug_halt_mask(target, C_STEP, C_HALT);
+                               /* Re-enable interrupts */
+                               cortex_m3_write_debug_halt_mask(target, C_HALT, C_MASKINTS);
+                       }
                        else {
-                               /* Start the core */
-                               LOG_DEBUG("Starting core to serve pending interrupts");
-                               int64_t t_start = timeval_ms();
-                               cortex_m3_write_debug_halt_mask(target, 0, C_HALT | C_STEP);
-
-                               /* Wait for pending handlers to complete or timeout */
-                               do {
-                                       retval = mem_ap_read_atomic_u32(swjdp,
-                                                       DCB_DHCSR,
-                                                       &cortex_m3->dcb_dhcsr);
-                                       if (retval != ERROR_OK) {
-                                               target->state = TARGET_UNKNOWN;
-                                               return retval;
-                                       }
-                                       isr_timed_out = ((timeval_ms() - t_start) > 500);
-                               } while (!((cortex_m3->dcb_dhcsr & S_HALT) || isr_timed_out));
-
-                               /* Remove the temporary breakpoint */
-                               breakpoint_remove(target, pc_value);
-
-                               if (isr_timed_out) {
-                                       LOG_DEBUG("Interrupt handlers didn't complete within time, "
-                                               "leaving target running");
-                               } else {
-                                       /* Step over next instruction with interrupts disabled */
-                                       cortex_m3_write_debug_halt_mask(target,
-                                               C_HALT | C_MASKINTS,
-                                               0);
+
+                               /* Set a temporary break point */
+                               if (breakpoint)
+                                       retval = cortex_m3_set_breakpoint(target, breakpoint);
+                               else
+                                       retval = breakpoint_add(target, pc_value, 2, BKPT_TYPE_BY_ADDR(pc_value));
+                               bool tmp_bp_set = (retval == ERROR_OK);
+
+                               /* No more breakpoints left, just do a step */
+                               if (!tmp_bp_set)
                                        cortex_m3_write_debug_halt_mask(target, C_STEP, C_HALT);
-                                       /* Re-enable interrupts */
-                                       cortex_m3_write_debug_halt_mask(target, C_HALT, C_MASKINTS);
+                               else {
+                                       /* Start the core */
+                                       LOG_DEBUG("Starting core to serve pending interrupts");
+                                       int64_t t_start = timeval_ms();
+                                       cortex_m3_write_debug_halt_mask(target, 0, C_HALT | C_STEP);
+
+                                       /* Wait for pending handlers to complete or timeout */
+                                       do {
+                                               retval = mem_ap_read_atomic_u32(swjdp,
+                                                               DCB_DHCSR,
+                                                               &cortex_m3->dcb_dhcsr);
+                                               if (retval != ERROR_OK) {
+                                                       target->state = TARGET_UNKNOWN;
+                                                       return retval;
+                                               }
+                                               isr_timed_out = ((timeval_ms() - t_start) > 500);
+                                       } while (!((cortex_m3->dcb_dhcsr & S_HALT) || isr_timed_out));
+
+                                       /* only remove breakpoint if we created it */
+                                       if (breakpoint)
+                                               cortex_m3_unset_breakpoint(target, breakpoint);
+                                       else {
+                                               /* Remove the temporary breakpoint */
+                                               breakpoint_remove(target, pc_value);
+                                       }
+
+                                       if (isr_timed_out) {
+                                               LOG_DEBUG("Interrupt handlers didn't complete within time, "
+                                                       "leaving target running");
+                                       } else {
+                                               /* Step over next instruction with interrupts disabled */
+                                               cortex_m3_write_debug_halt_mask(target,
+                                                       C_HALT | C_MASKINTS,
+                                                       0);
+                                               cortex_m3_write_debug_halt_mask(target, C_STEP, C_HALT);
+                                               /* Re-enable interrupts */
+                                               cortex_m3_write_debug_halt_mask(target, C_HALT, C_MASKINTS);
+                                       }
                                }
                        }
                }
@@ -903,7 +935,7 @@ static int cortex_m3_step(struct target *target, int current,
                return retval;
 
        /* registers are now invalid */
-       register_cache_invalidate(cortex_m3->armv7m.core_cache);
+       register_cache_invalidate(armv7m->arm.core_cache);
 
        if (breakpoint)
                cortex_m3_set_breakpoint(target, breakpoint);
@@ -946,7 +978,7 @@ static int cortex_m3_assert_reset(struct target *target)
                /* allow scripts to override the reset event */
 
                target_handle_event(target, TARGET_EVENT_RESET_ASSERT);
-               register_cache_invalidate(cortex_m3->armv7m.core_cache);
+               register_cache_invalidate(cortex_m3->armv7m.arm.core_cache);
                target->state = TARGET_RESET;
 
                return ERROR_OK;
@@ -973,6 +1005,14 @@ static int cortex_m3_assert_reset(struct target *target)
                        return retval;
        }
 
+       /* If the processor is sleeping in a WFI or WFE instruction, the
+        * C_HALT bit must be asserted to regain control */
+       if (cortex_m3->dcb_dhcsr & S_SLEEP) {
+               retval = mem_ap_write_u32(swjdp, DCB_DHCSR, DBGKEY | C_HALT | C_DEBUGEN);
+               if (retval != ERROR_OK)
+                       return retval;
+       }
+
        retval = mem_ap_write_u32(swjdp, DCB_DCRDR, 0);
        if (retval != ERROR_OK)
                return retval;
@@ -1043,7 +1083,7 @@ static int cortex_m3_assert_reset(struct target *target)
        target->state = TARGET_RESET;
        jtag_add_sleep(50000);
 
-       register_cache_invalidate(cortex_m3->armv7m.core_cache);
+       register_cache_invalidate(cortex_m3->armv7m.arm.core_cache);
 
        if (target->reset_halt) {
                retval = target_halt(target);
@@ -1425,7 +1465,7 @@ void cortex_m3_enable_watchpoints(struct target *target)
 }
 
 static int cortex_m3_load_core_reg_u32(struct target *target,
-       enum armv7m_regtype type, uint32_t num, uint32_t *value)
+               uint32_t num, uint32_t *value)
 {
        int retval;
        struct armv7m_common *armv7m = target_to_armv7m(target);
@@ -1486,7 +1526,7 @@ static int cortex_m3_load_core_reg_u32(struct target *target,
 }
 
 static int cortex_m3_store_core_reg_u32(struct target *target,
-       enum armv7m_regtype type, uint32_t num, uint32_t value)
+               uint32_t num, uint32_t value)
 {
        int retval;
        uint32_t reg;
@@ -1516,7 +1556,7 @@ static int cortex_m3_store_core_reg_u32(struct target *target,
                                struct reg *r;
 
                                LOG_ERROR("JTAG failure");
-                               r = armv7m->core_cache->reg_list + num;
+                               r = armv7m->arm.core_cache->reg_list + num;
                                r->dirty = r->valid;
                                return ERROR_JTAG_DEVICE_ERROR;
                        }
@@ -1580,7 +1620,7 @@ static int cortex_m3_read_memory(struct target *target, uint32_t address,
        if (count && buffer) {
                switch (size) {
                        case 4:
-                               retval = mem_ap_read_buf_u32(swjdp, buffer, 4 * count, address);
+                               retval = mem_ap_read_buf_u32(swjdp, buffer, 4 * count, address, true);
                                break;
                        case 2:
                                retval = mem_ap_read_buf_u16(swjdp, buffer, 2 * count, address);
@@ -1610,7 +1650,7 @@ static int cortex_m3_write_memory(struct target *target, uint32_t address,
        if (count && buffer) {
                switch (size) {
                        case 4:
-                               retval = mem_ap_write_buf_u32(swjdp, buffer, 4 * count, address);
+                               retval = mem_ap_write_buf_u32(swjdp, buffer, 4 * count, address, true);
                                break;
                        case 2:
                                retval = mem_ap_write_buf_u16(swjdp, buffer, 2 * count, address);
@@ -1624,12 +1664,6 @@ static int cortex_m3_write_memory(struct target *target, uint32_t address,
        return retval;
 }
 
-static int cortex_m3_bulk_write_memory(struct target *target, uint32_t address,
-       uint32_t count, const uint8_t *buffer)
-{
-       return cortex_m3_write_memory(target, address, 4, count, buffer);
-}
-
 static int cortex_m3_init_target(struct command_context *cmd_ctx,
        struct target *target)
 {
@@ -2245,7 +2279,6 @@ struct target_type cortexm3_target = {
 
        .read_memory = cortex_m3_read_memory,
        .write_memory = cortex_m3_write_memory,
-       .bulk_write_memory = cortex_m3_bulk_write_memory,
        .checksum_memory = armv7m_checksum_memory,
        .blank_check_memory = armv7m_blank_check_memory,
 

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)