1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * Copyright (C) 2006 by Magnus Lundin *
8 * Copyright (C) 2008 by Spencer Oliver *
9 * spen@spen-soft.co.uk *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * GNU General Public License for more details. *
21 * You should have received a copy of the GNU General Public License *
22 * along with this program; if not, write to the *
23 * Free Software Foundation, Inc., *
24 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
27 * Cortex-M3(tm) TRM, ARM DDI 0337E (r1p1) and 0337G (r2p0) *
29 ***************************************************************************/
34 #include "cortex_m3.h"
35 #include "target_request.h"
36 #include "target_type.h"
37 #include "arm_disassembler.h"
41 int cortex_m3_register_commands(struct command_context_s *cmd_ctx);
42 int handle_cortex_m3_mask_interrupts_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
44 /* forward declarations */
45 void cortex_m3_enable_breakpoints(struct target_s *target);
46 void cortex_m3_enable_watchpoints(struct target_s *target);
47 int cortex_m3_target_create(struct target_s *target, Jim_Interp *interp);
48 int cortex_m3_init_target(struct command_context_s *cmd_ctx, struct target_s *target);
49 int cortex_m3_quit(void);
50 int cortex_m3_load_core_reg_u32(target_t *target, enum armv7m_regtype type, uint32_t num, uint32_t *value);
51 int cortex_m3_store_core_reg_u32(target_t *target, enum armv7m_regtype type, uint32_t num, uint32_t value);
52 int cortex_m3_target_request_data(target_t *target, uint32_t size, uint8_t *buffer);
53 int cortex_m3_examine(struct target_s *target);
55 #ifdef ARMV7_GDB_HACKS
56 extern uint8_t armv7m_gdb_dummy_cpsr_value[];
57 extern reg_t armv7m_gdb_dummy_cpsr_reg;
60 target_type_t cortexm3_target =
64 .poll = cortex_m3_poll,
65 .arch_state = armv7m_arch_state,
67 .target_request_data = cortex_m3_target_request_data,
69 .halt = cortex_m3_halt,
70 .resume = cortex_m3_resume,
71 .step = cortex_m3_step,
73 .assert_reset = cortex_m3_assert_reset,
74 .deassert_reset = cortex_m3_deassert_reset,
75 .soft_reset_halt = cortex_m3_soft_reset_halt,
77 .get_gdb_reg_list = armv7m_get_gdb_reg_list,
79 .read_memory = cortex_m3_read_memory,
80 .write_memory = cortex_m3_write_memory,
81 .bulk_write_memory = cortex_m3_bulk_write_memory,
82 .checksum_memory = armv7m_checksum_memory,
83 .blank_check_memory = armv7m_blank_check_memory,
85 .run_algorithm = armv7m_run_algorithm,
87 .add_breakpoint = cortex_m3_add_breakpoint,
88 .remove_breakpoint = cortex_m3_remove_breakpoint,
89 .add_watchpoint = cortex_m3_add_watchpoint,
90 .remove_watchpoint = cortex_m3_remove_watchpoint,
92 .register_commands = cortex_m3_register_commands,
93 .target_create = cortex_m3_target_create,
94 .init_target = cortex_m3_init_target,
95 .examine = cortex_m3_examine,
96 .quit = cortex_m3_quit
99 int cortexm3_dap_read_coreregister_u32(swjdp_common_t *swjdp, uint32_t *value, int regnum)
104 /* because the DCB_DCRDR is used for the emulated dcc channel
105 * we gave to save/restore the DCB_DCRDR when used */
107 mem_ap_read_u32(swjdp, DCB_DCRDR, &dcrdr);
109 swjdp->trans_mode = TRANS_MODE_COMPOSITE;
111 /* mem_ap_write_u32(swjdp, DCB_DCRSR, regnum); */
112 dap_setup_accessport(swjdp, CSW_32BIT | CSW_ADDRINC_OFF, DCB_DCRSR & 0xFFFFFFF0);
113 dap_ap_write_reg_u32(swjdp, AP_REG_BD0 | (DCB_DCRSR & 0xC), regnum);
115 /* mem_ap_read_u32(swjdp, DCB_DCRDR, value); */
116 dap_setup_accessport(swjdp, CSW_32BIT | CSW_ADDRINC_OFF, DCB_DCRDR & 0xFFFFFFF0);
117 dap_ap_read_reg_u32(swjdp, AP_REG_BD0 | (DCB_DCRDR & 0xC), value);
119 mem_ap_write_u32(swjdp, DCB_DCRDR, dcrdr);
120 retval = swjdp_transaction_endcheck(swjdp);
124 int cortexm3_dap_write_coreregister_u32(swjdp_common_t *swjdp, uint32_t value, int regnum)
129 /* because the DCB_DCRDR is used for the emulated dcc channel
130 * we gave to save/restore the DCB_DCRDR when used */
132 mem_ap_read_u32(swjdp, DCB_DCRDR, &dcrdr);
134 swjdp->trans_mode = TRANS_MODE_COMPOSITE;
136 /* mem_ap_write_u32(swjdp, DCB_DCRDR, core_regs[i]); */
137 dap_setup_accessport(swjdp, CSW_32BIT | CSW_ADDRINC_OFF, DCB_DCRDR & 0xFFFFFFF0);
138 dap_ap_write_reg_u32(swjdp, AP_REG_BD0 | (DCB_DCRDR & 0xC), value);
140 /* mem_ap_write_u32(swjdp, DCB_DCRSR, i | DCRSR_WnR); */
141 dap_setup_accessport(swjdp, CSW_32BIT | CSW_ADDRINC_OFF, DCB_DCRSR & 0xFFFFFFF0);
142 dap_ap_write_reg_u32(swjdp, AP_REG_BD0 | (DCB_DCRSR & 0xC), regnum | DCRSR_WnR);
144 mem_ap_write_u32(swjdp, DCB_DCRDR, dcrdr);
145 retval = swjdp_transaction_endcheck(swjdp);
150 int cortex_m3_write_debug_halt_mask(target_t *target, uint32_t mask_on, uint32_t mask_off)
152 /* get pointers to arch-specific information */
153 armv7m_common_t *armv7m = target->arch_info;
154 cortex_m3_common_t *cortex_m3 = armv7m->arch_info;
155 swjdp_common_t *swjdp = &armv7m->swjdp_info;
157 /* mask off status bits */
158 cortex_m3->dcb_dhcsr &= ~((0xFFFF << 16) | mask_off);
159 /* create new register mask */
160 cortex_m3->dcb_dhcsr |= DBGKEY | C_DEBUGEN | mask_on;
162 return mem_ap_write_atomic_u32(swjdp, DCB_DHCSR, cortex_m3->dcb_dhcsr);
165 int cortex_m3_clear_halt(target_t *target)
167 /* get pointers to arch-specific information */
168 armv7m_common_t *armv7m = target->arch_info;
169 cortex_m3_common_t *cortex_m3 = armv7m->arch_info;
170 swjdp_common_t *swjdp = &armv7m->swjdp_info;
172 /* clear step if any */
173 cortex_m3_write_debug_halt_mask(target, C_HALT, C_STEP);
175 /* Read Debug Fault Status Register */
176 mem_ap_read_atomic_u32(swjdp, NVIC_DFSR, &cortex_m3->nvic_dfsr);
177 /* Write Debug Fault Status Register to enable processing to resume ?? Try with and without this !! */
178 mem_ap_write_atomic_u32(swjdp, NVIC_DFSR, cortex_m3->nvic_dfsr);
179 LOG_DEBUG(" NVIC_DFSR 0x%" PRIx32 "", cortex_m3->nvic_dfsr);
184 int cortex_m3_single_step_core(target_t *target)
186 /* get pointers to arch-specific information */
187 armv7m_common_t *armv7m = target->arch_info;
188 cortex_m3_common_t *cortex_m3 = armv7m->arch_info;
189 swjdp_common_t *swjdp = &armv7m->swjdp_info;
192 /* backup dhcsr reg */
193 dhcsr_save = cortex_m3->dcb_dhcsr;
195 /* mask interrupts if not done already */
196 if (!(cortex_m3->dcb_dhcsr & C_MASKINTS))
197 mem_ap_write_atomic_u32(swjdp, DCB_DHCSR, DBGKEY | C_MASKINTS | C_HALT | C_DEBUGEN);
198 mem_ap_write_atomic_u32(swjdp, DCB_DHCSR, DBGKEY | C_MASKINTS | C_STEP | C_DEBUGEN);
201 /* restore dhcsr reg */
202 cortex_m3->dcb_dhcsr = dhcsr_save;
203 cortex_m3_clear_halt(target);
208 int cortex_m3_exec_opcode(target_t *target,uint32_t opcode, int len /* MODE, r0_invalue, &r0_outvalue */)
210 /* get pointers to arch-specific information */
211 armv7m_common_t *armv7m = target->arch_info;
212 swjdp_common_t *swjdp = &armv7m->swjdp_info;
216 mem_ap_read_u32(swjdp, 0x20000000, &savedram);
217 mem_ap_write_u32(swjdp, 0x20000000, opcode);
218 cortexm3_dap_write_coreregister_u32(swjdp, 0x20000000, 15);
219 cortex_m3_single_step_core(target);
220 armv7m->core_cache->reg_list[15].dirty = armv7m->core_cache->reg_list[15].valid;
221 retvalue = mem_ap_write_atomic_u32(swjdp, 0x20000000, savedram);
227 /* Enable interrupts */
228 int cortex_m3_cpsie(target_t *target, uint32_t IF)
230 return cortex_m3_exec_opcode(target, ARMV7M_T_CPSIE(IF), 2);
233 /* Disable interrupts */
234 int cortex_m3_cpsid(target_t *target, uint32_t IF)
236 return cortex_m3_exec_opcode(target, ARMV7M_T_CPSID(IF), 2);
240 int cortex_m3_endreset_event(target_t *target)
245 /* get pointers to arch-specific information */
246 armv7m_common_t *armv7m = target->arch_info;
247 cortex_m3_common_t *cortex_m3 = armv7m->arch_info;
248 swjdp_common_t *swjdp = &armv7m->swjdp_info;
249 cortex_m3_fp_comparator_t *fp_list = cortex_m3->fp_comparator_list;
250 cortex_m3_dwt_comparator_t *dwt_list = cortex_m3->dwt_comparator_list;
252 mem_ap_read_atomic_u32(swjdp, DCB_DEMCR, &dcb_demcr);
253 LOG_DEBUG("DCB_DEMCR = 0x%8.8" PRIx32 "",dcb_demcr);
255 /* this regsiter is used for emulated dcc channel */
256 mem_ap_write_u32(swjdp, DCB_DCRDR, 0);
258 /* Enable debug requests */
259 mem_ap_read_atomic_u32(swjdp, DCB_DHCSR, &cortex_m3->dcb_dhcsr);
260 if (!(cortex_m3->dcb_dhcsr & C_DEBUGEN))
261 mem_ap_write_u32(swjdp, DCB_DHCSR, DBGKEY | C_DEBUGEN);
263 /* clear any interrupt masking */
264 cortex_m3_write_debug_halt_mask(target, 0, C_MASKINTS);
266 /* Enable trace and dwt */
267 mem_ap_write_u32(swjdp, DCB_DEMCR, TRCENA | VC_HARDERR | VC_BUSERR);
268 /* Monitor bus faults */
269 mem_ap_write_u32(swjdp, NVIC_SHCSR, SHCSR_BUSFAULTENA);
272 target_write_u32(target, FP_CTRL, 3);
273 cortex_m3->fpb_enabled = 1;
275 /* Restore FPB registers */
276 for (i = 0; i < cortex_m3->fp_num_code + cortex_m3->fp_num_lit; i++)
278 target_write_u32(target, fp_list[i].fpcr_address, fp_list[i].fpcr_value);
281 /* Restore DWT registers */
282 for (i = 0; i < cortex_m3->dwt_num_comp; i++)
284 target_write_u32(target, dwt_list[i].dwt_comparator_address, dwt_list[i].comp);
285 target_write_u32(target, dwt_list[i].dwt_comparator_address | 0x4, dwt_list[i].mask);
286 target_write_u32(target, dwt_list[i].dwt_comparator_address | 0x8, dwt_list[i].function);
288 swjdp_transaction_endcheck(swjdp);
290 armv7m_invalidate_core_regs(target);
292 /* make sure we have latest dhcsr flags */
293 mem_ap_read_atomic_u32(swjdp, DCB_DHCSR, &cortex_m3->dcb_dhcsr);
298 int cortex_m3_examine_debug_reason(target_t *target)
300 /* get pointers to arch-specific information */
301 armv7m_common_t *armv7m = target->arch_info;
302 cortex_m3_common_t *cortex_m3 = armv7m->arch_info;
304 /* THIS IS NOT GOOD, TODO - better logic for detection of debug state reason */
305 /* only check the debug reason if we don't know it already */
307 if ((target->debug_reason != DBG_REASON_DBGRQ)
308 && (target->debug_reason != DBG_REASON_SINGLESTEP))
312 if (cortex_m3->nvic_dfsr & DFSR_BKPT)
314 target->debug_reason = DBG_REASON_BREAKPOINT;
315 if (cortex_m3->nvic_dfsr & DFSR_DWTTRAP)
316 target->debug_reason = DBG_REASON_WPTANDBKPT;
318 else if (cortex_m3->nvic_dfsr & DFSR_DWTTRAP)
319 target->debug_reason = DBG_REASON_WATCHPOINT;
325 int cortex_m3_examine_exception_reason(target_t *target)
327 uint32_t shcsr, except_sr, cfsr = -1, except_ar = -1;
329 /* get pointers to arch-specific information */
330 armv7m_common_t *armv7m = target->arch_info;
331 swjdp_common_t *swjdp = &armv7m->swjdp_info;
333 mem_ap_read_u32(swjdp, NVIC_SHCSR, &shcsr);
334 switch (armv7m->exception_number)
338 case 3: /* Hard Fault */
339 mem_ap_read_atomic_u32(swjdp, NVIC_HFSR, &except_sr);
340 if (except_sr & 0x40000000)
342 mem_ap_read_u32(swjdp, NVIC_CFSR, &cfsr);
345 case 4: /* Memory Management */
346 mem_ap_read_u32(swjdp, NVIC_CFSR, &except_sr);
347 mem_ap_read_u32(swjdp, NVIC_MMFAR, &except_ar);
349 case 5: /* Bus Fault */
350 mem_ap_read_u32(swjdp, NVIC_CFSR, &except_sr);
351 mem_ap_read_u32(swjdp, NVIC_BFAR, &except_ar);
353 case 6: /* Usage Fault */
354 mem_ap_read_u32(swjdp, NVIC_CFSR, &except_sr);
356 case 11: /* SVCall */
358 case 12: /* Debug Monitor */
359 mem_ap_read_u32(swjdp, NVIC_DFSR, &except_sr);
361 case 14: /* PendSV */
363 case 15: /* SysTick */
369 swjdp_transaction_endcheck(swjdp);
370 LOG_DEBUG("%s SHCSR 0x%" PRIx32 ", SR 0x%" PRIx32 ", CFSR 0x%" PRIx32 ", AR 0x%" PRIx32 "", armv7m_exception_string(armv7m->exception_number), \
371 shcsr, except_sr, cfsr, except_ar);
375 int cortex_m3_debug_entry(target_t *target)
381 /* get pointers to arch-specific information */
382 armv7m_common_t *armv7m = target->arch_info;
383 cortex_m3_common_t *cortex_m3 = armv7m->arch_info;
384 swjdp_common_t *swjdp = &armv7m->swjdp_info;
387 if (armv7m->pre_debug_entry)
388 armv7m->pre_debug_entry(target);
390 cortex_m3_clear_halt(target);
391 mem_ap_read_atomic_u32(swjdp, DCB_DHCSR, &cortex_m3->dcb_dhcsr);
393 if ((retval = armv7m->examine_debug_reason(target)) != ERROR_OK)
396 /* Examine target state and mode */
397 /* First load register acessible through core debug port*/
398 int num_regs = armv7m->core_cache->num_regs;
400 for (i = 0; i < num_regs; i++)
402 if (!armv7m->core_cache->reg_list[i].valid)
403 armv7m->read_core_reg(target, i);
406 xPSR = buf_get_u32(armv7m->core_cache->reg_list[ARMV7M_xPSR].value, 0, 32);
408 #ifdef ARMV7_GDB_HACKS
409 /* copy real xpsr reg for gdb, setting thumb bit */
410 buf_set_u32(armv7m_gdb_dummy_cpsr_value, 0, 32, xPSR);
411 buf_set_u32(armv7m_gdb_dummy_cpsr_value, 5, 1, 1);
412 armv7m_gdb_dummy_cpsr_reg.valid = armv7m->core_cache->reg_list[ARMV7M_xPSR].valid;
413 armv7m_gdb_dummy_cpsr_reg.dirty = armv7m->core_cache->reg_list[ARMV7M_xPSR].dirty;
416 /* For IT instructions xPSR must be reloaded on resume and clear on debug exec */
419 armv7m->core_cache->reg_list[ARMV7M_xPSR].dirty = armv7m->core_cache->reg_list[ARMV7M_xPSR].valid;
420 cortex_m3_store_core_reg_u32(target, ARMV7M_REGISTER_CORE_GP, 16, xPSR &~ 0xff);
423 /* Are we in an exception handler */
426 armv7m->core_mode = ARMV7M_MODE_HANDLER;
427 armv7m->exception_number = (xPSR & 0x1FF);
431 armv7m->core_mode = buf_get_u32(armv7m->core_cache->reg_list[ARMV7M_CONTROL].value, 0, 1);
432 armv7m->exception_number = 0;
435 if (armv7m->exception_number)
437 cortex_m3_examine_exception_reason(target);
440 LOG_DEBUG("entered debug state in core mode: %s at PC 0x%" PRIx32 ", target->state: %s",
441 armv7m_mode_strings[armv7m->core_mode],
442 *(uint32_t*)(armv7m->core_cache->reg_list[15].value),
443 target_state_name(target));
445 if (armv7m->post_debug_entry)
446 armv7m->post_debug_entry(target);
451 int cortex_m3_poll(target_t *target)
454 enum target_state prev_target_state = target->state;
456 /* get pointers to arch-specific information */
457 armv7m_common_t *armv7m = target->arch_info;
458 cortex_m3_common_t *cortex_m3 = armv7m->arch_info;
459 swjdp_common_t *swjdp = &armv7m->swjdp_info;
461 /* Read from Debug Halting Control and Status Register */
462 retval = mem_ap_read_atomic_u32(swjdp, DCB_DHCSR, &cortex_m3->dcb_dhcsr);
463 if (retval != ERROR_OK)
465 target->state = TARGET_UNKNOWN;
469 if (cortex_m3->dcb_dhcsr & S_RESET_ST)
471 /* check if still in reset */
472 mem_ap_read_atomic_u32(swjdp, DCB_DHCSR, &cortex_m3->dcb_dhcsr);
474 if (cortex_m3->dcb_dhcsr & S_RESET_ST)
476 target->state = TARGET_RESET;
481 if (target->state == TARGET_RESET)
483 /* Cannot switch context while running so endreset is called with target->state == TARGET_RESET */
484 LOG_DEBUG("Exit from reset with dcb_dhcsr 0x%" PRIx32 "", cortex_m3->dcb_dhcsr);
485 cortex_m3_endreset_event(target);
486 target->state = TARGET_RUNNING;
487 prev_target_state = TARGET_RUNNING;
490 if (cortex_m3->dcb_dhcsr & S_HALT)
492 target->state = TARGET_HALTED;
494 if ((prev_target_state == TARGET_RUNNING) || (prev_target_state == TARGET_RESET))
496 if ((retval = cortex_m3_debug_entry(target)) != ERROR_OK)
499 target_call_event_callbacks(target, TARGET_EVENT_HALTED);
501 if (prev_target_state == TARGET_DEBUG_RUNNING)
504 if ((retval = cortex_m3_debug_entry(target)) != ERROR_OK)
507 target_call_event_callbacks(target, TARGET_EVENT_DEBUG_HALTED);
511 /* REVISIT when S_SLEEP is set, it's in a Sleep or DeepSleep state.
512 * How best to model low power modes?
515 if (target->state == TARGET_UNKNOWN)
517 /* check if processor is retiring instructions */
518 if (cortex_m3->dcb_dhcsr & S_RETIRE_ST)
520 target->state = TARGET_RUNNING;
528 int cortex_m3_halt(target_t *target)
530 LOG_DEBUG("target->state: %s",
531 target_state_name(target));
533 if (target->state == TARGET_HALTED)
535 LOG_DEBUG("target was already halted");
539 if (target->state == TARGET_UNKNOWN)
541 LOG_WARNING("target was in unknown state when halt was requested");
544 if (target->state == TARGET_RESET)
546 if ((jtag_get_reset_config() & RESET_SRST_PULLS_TRST) && jtag_get_srst())
548 LOG_ERROR("can't request a halt while in reset if nSRST pulls nTRST");
549 return ERROR_TARGET_FAILURE;
553 /* we came here in a reset_halt or reset_init sequence
554 * debug entry was already prepared in cortex_m3_prepare_reset_halt()
556 target->debug_reason = DBG_REASON_DBGRQ;
562 /* Write to Debug Halting Control and Status Register */
563 cortex_m3_write_debug_halt_mask(target, C_HALT, 0);
565 target->debug_reason = DBG_REASON_DBGRQ;
570 int cortex_m3_soft_reset_halt(struct target_s *target)
572 /* get pointers to arch-specific information */
573 armv7m_common_t *armv7m = target->arch_info;
574 cortex_m3_common_t *cortex_m3 = armv7m->arch_info;
575 swjdp_common_t *swjdp = &armv7m->swjdp_info;
576 uint32_t dcb_dhcsr = 0;
577 int retval, timeout = 0;
579 /* Enter debug state on reset, cf. end_reset_event() */
580 mem_ap_write_u32(swjdp, DCB_DEMCR, TRCENA | VC_HARDERR | VC_BUSERR | VC_CORERESET);
582 /* Request a reset */
583 mem_ap_write_atomic_u32(swjdp, NVIC_AIRCR, AIRCR_VECTKEY | AIRCR_VECTRESET);
584 target->state = TARGET_RESET;
586 /* registers are now invalid */
587 armv7m_invalidate_core_regs(target);
589 while (timeout < 100)
591 retval = mem_ap_read_atomic_u32(swjdp, DCB_DHCSR, &dcb_dhcsr);
592 if (retval == ERROR_OK)
594 mem_ap_read_atomic_u32(swjdp, NVIC_DFSR, &cortex_m3->nvic_dfsr);
595 if ((dcb_dhcsr & S_HALT) && (cortex_m3->nvic_dfsr & DFSR_VCATCH))
597 LOG_DEBUG("system reset-halted, dcb_dhcsr 0x%" PRIx32 ", nvic_dfsr 0x%" PRIx32 "", dcb_dhcsr, cortex_m3->nvic_dfsr);
598 cortex_m3_poll(target);
602 LOG_DEBUG("waiting for system reset-halt, dcb_dhcsr 0x%" PRIx32 ", %i ms", dcb_dhcsr, timeout);
611 int cortex_m3_resume(struct target_s *target, int current, uint32_t address, int handle_breakpoints, int debug_execution)
613 /* get pointers to arch-specific information */
614 armv7m_common_t *armv7m = target->arch_info;
615 breakpoint_t *breakpoint = NULL;
618 if (target->state != TARGET_HALTED)
620 LOG_WARNING("target not halted");
621 return ERROR_TARGET_NOT_HALTED;
624 if (!debug_execution)
626 target_free_all_working_areas(target);
627 cortex_m3_enable_breakpoints(target);
628 cortex_m3_enable_watchpoints(target);
633 /* Disable interrupts */
634 /* We disable interrupts in the PRIMASK register instead of masking with C_MASKINTS,
635 * This is probably the same issue as Cortex-M3 Errata 377493:
636 * C_MASKINTS in parallel with disabled interrupts can cause local faults to not be taken. */
637 buf_set_u32(armv7m->core_cache->reg_list[ARMV7M_PRIMASK].value, 0, 32, 1);
638 armv7m->core_cache->reg_list[ARMV7M_PRIMASK].dirty = 1;
639 armv7m->core_cache->reg_list[ARMV7M_PRIMASK].valid = 1;
641 /* Make sure we are in Thumb mode */
642 buf_set_u32(armv7m->core_cache->reg_list[ARMV7M_xPSR].value, 0, 32,
643 buf_get_u32(armv7m->core_cache->reg_list[ARMV7M_xPSR].value, 0, 32) | (1 << 24));
644 armv7m->core_cache->reg_list[ARMV7M_xPSR].dirty = 1;
645 armv7m->core_cache->reg_list[ARMV7M_xPSR].valid = 1;
648 /* current = 1: continue on current pc, otherwise continue at <address> */
651 buf_set_u32(armv7m->core_cache->reg_list[15].value, 0, 32, address);
652 armv7m->core_cache->reg_list[15].dirty = 1;
653 armv7m->core_cache->reg_list[15].valid = 1;
656 resume_pc = buf_get_u32(armv7m->core_cache->reg_list[15].value, 0, 32);
658 armv7m_restore_context(target);
660 /* the front-end may request us not to handle breakpoints */
661 if (handle_breakpoints)
663 /* Single step past breakpoint at current address */
664 if ((breakpoint = breakpoint_find(target, resume_pc)))
666 LOG_DEBUG("unset breakpoint at 0x%8.8" PRIx32 " (ID: %d)",
668 breakpoint->unique_id );
669 cortex_m3_unset_breakpoint(target, breakpoint);
670 cortex_m3_single_step_core(target);
671 cortex_m3_set_breakpoint(target, breakpoint);
676 cortex_m3_write_debug_halt_mask(target, 0, C_HALT);
678 target->debug_reason = DBG_REASON_NOTHALTED;
680 /* registers are now invalid */
681 armv7m_invalidate_core_regs(target);
682 if (!debug_execution)
684 target->state = TARGET_RUNNING;
685 target_call_event_callbacks(target, TARGET_EVENT_RESUMED);
686 LOG_DEBUG("target resumed at 0x%" PRIx32 "", resume_pc);
690 target->state = TARGET_DEBUG_RUNNING;
691 target_call_event_callbacks(target, TARGET_EVENT_DEBUG_RESUMED);
692 LOG_DEBUG("target debug resumed at 0x%" PRIx32 "", resume_pc);
698 /* int irqstepcount = 0; */
699 int cortex_m3_step(struct target_s *target, int current, uint32_t address, int handle_breakpoints)
701 /* get pointers to arch-specific information */
702 armv7m_common_t *armv7m = target->arch_info;
703 cortex_m3_common_t *cortex_m3 = armv7m->arch_info;
704 swjdp_common_t *swjdp = &armv7m->swjdp_info;
705 breakpoint_t *breakpoint = NULL;
707 if (target->state != TARGET_HALTED)
709 LOG_WARNING("target not halted");
710 return ERROR_TARGET_NOT_HALTED;
713 /* current = 1: continue on current pc, otherwise continue at <address> */
715 buf_set_u32(armv7m->core_cache->reg_list[15].value, 0, 32, address);
717 /* the front-end may request us not to handle breakpoints */
718 if (handle_breakpoints)
719 if ((breakpoint = breakpoint_find(target, buf_get_u32(armv7m->core_cache->reg_list[15].value, 0, 32))))
720 cortex_m3_unset_breakpoint(target, breakpoint);
722 target->debug_reason = DBG_REASON_SINGLESTEP;
724 armv7m_restore_context(target);
726 target_call_event_callbacks(target, TARGET_EVENT_RESUMED);
728 /* set step and clear halt */
729 cortex_m3_write_debug_halt_mask(target, C_STEP, C_HALT);
730 mem_ap_read_atomic_u32(swjdp, DCB_DHCSR, &cortex_m3->dcb_dhcsr);
732 /* registers are now invalid */
733 armv7m_invalidate_core_regs(target);
736 cortex_m3_set_breakpoint(target, breakpoint);
738 LOG_DEBUG("target stepped dcb_dhcsr = 0x%" PRIx32 " nvic_icsr = 0x%" PRIx32 "", cortex_m3->dcb_dhcsr, cortex_m3->nvic_icsr);
740 cortex_m3_debug_entry(target);
741 target_call_event_callbacks(target, TARGET_EVENT_HALTED);
743 LOG_DEBUG("target stepped dcb_dhcsr = 0x%" PRIx32 " nvic_icsr = 0x%" PRIx32 "", cortex_m3->dcb_dhcsr, cortex_m3->nvic_icsr);
747 int cortex_m3_assert_reset(target_t *target)
749 armv7m_common_t *armv7m = target->arch_info;
750 cortex_m3_common_t *cortex_m3 = armv7m->arch_info;
751 swjdp_common_t *swjdp = &armv7m->swjdp_info;
754 LOG_DEBUG("target->state: %s",
755 target_state_name(target));
757 enum reset_types jtag_reset_config = jtag_get_reset_config();
758 if (!(jtag_reset_config & RESET_HAS_SRST))
760 LOG_ERROR("Can't assert SRST");
764 /* Enable debug requests */
765 mem_ap_read_atomic_u32(swjdp, DCB_DHCSR, &cortex_m3->dcb_dhcsr);
766 if (!(cortex_m3->dcb_dhcsr & C_DEBUGEN))
767 mem_ap_write_u32(swjdp, DCB_DHCSR, DBGKEY | C_DEBUGEN);
769 mem_ap_write_u32(swjdp, DCB_DCRDR, 0);
771 if (!target->reset_halt)
773 /* Set/Clear C_MASKINTS in a separate operation */
774 if (cortex_m3->dcb_dhcsr & C_MASKINTS)
775 mem_ap_write_atomic_u32(swjdp, DCB_DHCSR, DBGKEY | C_DEBUGEN | C_HALT);
777 /* clear any debug flags before resuming */
778 cortex_m3_clear_halt(target);
780 /* clear C_HALT in dhcsr reg */
781 cortex_m3_write_debug_halt_mask(target, 0, C_HALT);
783 /* Enter debug state on reset, cf. end_reset_event() */
784 mem_ap_write_u32(swjdp, DCB_DEMCR, TRCENA | VC_HARDERR | VC_BUSERR);
788 /* Enter debug state on reset, cf. end_reset_event() */
789 mem_ap_write_atomic_u32(swjdp, DCB_DEMCR, TRCENA | VC_HARDERR | VC_BUSERR | VC_CORERESET);
792 /* following hack is to handle luminary reset
793 * when srst is asserted the luminary device seesm to also clear the debug registers
794 * which does not match the armv7 debug TRM */
796 if (strcmp(target->variant, "lm3s") == 0)
798 /* get revision of lm3s target, only early silicon has this issue
799 * Fury Rev B, DustDevil Rev B, Tempest all ok */
803 if (target_read_u32(target, 0x400fe000, &did0) == ERROR_OK)
805 switch ((did0 >> 16) & 0xff)
808 /* all Sandstorm suffer issue */
814 /* only Fury/DustDevil rev A suffer reset problems */
815 if (((did0 >> 8) & 0xff) == 0)
824 /* default to asserting srst */
825 if (jtag_reset_config & RESET_SRST_PULLS_TRST)
827 jtag_add_reset(1, 1);
831 jtag_add_reset(0, 1);
836 /* this causes the luminary device to reset using the watchdog */
837 mem_ap_write_atomic_u32(swjdp, NVIC_AIRCR, AIRCR_VECTKEY | AIRCR_SYSRESETREQ);
838 LOG_DEBUG("Using Luminary Reset: SYSRESETREQ");
841 /* I do not know why this is necessary, but it fixes strange effects
842 * (step/resume cause a NMI after reset) on LM3S6918 -- Michael Schwingen */
844 mem_ap_read_atomic_u32(swjdp, NVIC_AIRCR, &tmp);
848 target->state = TARGET_RESET;
849 jtag_add_sleep(50000);
851 armv7m_invalidate_core_regs(target);
853 if (target->reset_halt)
856 if ((retval = target_halt(target)) != ERROR_OK)
863 int cortex_m3_deassert_reset(target_t *target)
865 LOG_DEBUG("target->state: %s",
866 target_state_name(target));
868 /* deassert reset lines */
869 jtag_add_reset(0, 0);
874 void cortex_m3_enable_breakpoints(struct target_s *target)
876 breakpoint_t *breakpoint = target->breakpoints;
878 /* set any pending breakpoints */
881 if (breakpoint->set == 0)
882 cortex_m3_set_breakpoint(target, breakpoint);
883 breakpoint = breakpoint->next;
887 int cortex_m3_set_breakpoint(struct target_s *target, breakpoint_t *breakpoint)
893 /* get pointers to arch-specific information */
894 armv7m_common_t *armv7m = target->arch_info;
895 cortex_m3_common_t *cortex_m3 = armv7m->arch_info;
897 cortex_m3_fp_comparator_t * comparator_list = cortex_m3->fp_comparator_list;
901 LOG_WARNING("breakpoint (BPID: %d) already set", breakpoint->unique_id);
905 if (cortex_m3->auto_bp_type)
907 breakpoint->type = (breakpoint->address < 0x20000000) ? BKPT_HARD : BKPT_SOFT;
910 if (breakpoint->type == BKPT_HARD)
912 while (comparator_list[fp_num].used && (fp_num < cortex_m3->fp_num_code))
914 if (fp_num >= cortex_m3->fp_num_code)
916 LOG_DEBUG("ERROR Can not find free FP Comparator");
917 LOG_WARNING("ERROR Can not find free FP Comparator");
920 breakpoint->set = fp_num + 1;
921 hilo = (breakpoint->address & 0x2) ? FPCR_REPLACE_BKPT_HIGH : FPCR_REPLACE_BKPT_LOW;
922 comparator_list[fp_num].used = 1;
923 comparator_list[fp_num].fpcr_value = (breakpoint->address & 0x1FFFFFFC) | hilo | 1;
924 target_write_u32(target, comparator_list[fp_num].fpcr_address, comparator_list[fp_num].fpcr_value);
925 LOG_DEBUG("fpc_num %i fpcr_value 0x%" PRIx32 "", fp_num, comparator_list[fp_num].fpcr_value);
926 if (!cortex_m3->fpb_enabled)
928 LOG_DEBUG("FPB wasn't enabled, do it now");
929 target_write_u32(target, FP_CTRL, 3);
932 else if (breakpoint->type == BKPT_SOFT)
935 buf_set_u32(code, 0, 32, ARMV7M_T_BKPT(0x11));
936 if ((retval = target_read_memory(target, breakpoint->address & 0xFFFFFFFE, breakpoint->length, 1, breakpoint->orig_instr)) != ERROR_OK)
940 if ((retval = target_write_memory(target, breakpoint->address & 0xFFFFFFFE, breakpoint->length, 1, code)) != ERROR_OK)
944 breakpoint->set = 0x11; /* Any nice value but 0 */
947 LOG_DEBUG("BPID: %d, Type: %d, Address: 0x%08" PRIx32 " Length: %d (set=%d)",
948 breakpoint->unique_id,
949 (int)(breakpoint->type),
957 int cortex_m3_unset_breakpoint(struct target_s *target, breakpoint_t *breakpoint)
960 /* get pointers to arch-specific information */
961 armv7m_common_t *armv7m = target->arch_info;
962 cortex_m3_common_t *cortex_m3 = armv7m->arch_info;
963 cortex_m3_fp_comparator_t * comparator_list = cortex_m3->fp_comparator_list;
965 if (!breakpoint->set)
967 LOG_WARNING("breakpoint not set");
971 LOG_DEBUG("BPID: %d, Type: %d, Address: 0x%08" PRIx32 " Length: %d (set=%d)",
972 breakpoint->unique_id,
973 (int)(breakpoint->type),
978 if (breakpoint->type == BKPT_HARD)
980 int fp_num = breakpoint->set - 1;
981 if ((fp_num < 0) || (fp_num >= cortex_m3->fp_num_code))
983 LOG_DEBUG("Invalid FP Comparator number in breakpoint");
986 comparator_list[fp_num].used = 0;
987 comparator_list[fp_num].fpcr_value = 0;
988 target_write_u32(target, comparator_list[fp_num].fpcr_address, comparator_list[fp_num].fpcr_value);
992 /* restore original instruction (kept in target endianness) */
993 if (breakpoint->length == 4)
995 if ((retval = target_write_memory(target, breakpoint->address & 0xFFFFFFFE, 4, 1, breakpoint->orig_instr)) != ERROR_OK)
1002 if ((retval = target_write_memory(target, breakpoint->address & 0xFFFFFFFE, 2, 1, breakpoint->orig_instr)) != ERROR_OK)
1008 breakpoint->set = 0;
1013 int cortex_m3_add_breakpoint(struct target_s *target, breakpoint_t *breakpoint)
1015 /* get pointers to arch-specific information */
1016 armv7m_common_t *armv7m = target->arch_info;
1017 cortex_m3_common_t *cortex_m3 = armv7m->arch_info;
1019 if (cortex_m3->auto_bp_type)
1021 breakpoint->type = (breakpoint->address < 0x20000000) ? BKPT_HARD : BKPT_SOFT;
1022 #ifdef ARMV7_GDB_HACKS
1023 if (breakpoint->length != 2) {
1024 /* XXX Hack: Replace all breakpoints with length != 2 with
1025 * a hardware breakpoint. */
1026 breakpoint->type = BKPT_HARD;
1027 breakpoint->length = 2;
1032 if ((breakpoint->type == BKPT_HARD) && (breakpoint->address >= 0x20000000))
1034 LOG_INFO("flash patch comparator requested outside code memory region");
1035 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1038 if ((breakpoint->type == BKPT_SOFT) && (breakpoint->address < 0x20000000))
1040 LOG_INFO("soft breakpoint requested in code (flash) memory region");
1041 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1044 if ((breakpoint->type == BKPT_HARD) && (cortex_m3->fp_code_available < 1))
1046 LOG_INFO("no flash patch comparator unit available for hardware breakpoint");
1047 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1050 if ((breakpoint->length != 2))
1052 LOG_INFO("only breakpoints of two bytes length supported");
1053 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1056 if (breakpoint->type == BKPT_HARD)
1057 cortex_m3->fp_code_available--;
1058 cortex_m3_set_breakpoint(target, breakpoint);
1063 int cortex_m3_remove_breakpoint(struct target_s *target, breakpoint_t *breakpoint)
1065 /* get pointers to arch-specific information */
1066 armv7m_common_t *armv7m = target->arch_info;
1067 cortex_m3_common_t *cortex_m3 = armv7m->arch_info;
1069 if (target->state != TARGET_HALTED)
1071 LOG_WARNING("target not halted");
1072 return ERROR_TARGET_NOT_HALTED;
1075 if (cortex_m3->auto_bp_type)
1077 breakpoint->type = (breakpoint->address < 0x20000000) ? BKPT_HARD : BKPT_SOFT;
1080 if (breakpoint->set)
1082 cortex_m3_unset_breakpoint(target, breakpoint);
1085 if (breakpoint->type == BKPT_HARD)
1086 cortex_m3->fp_code_available++;
1091 int cortex_m3_set_watchpoint(struct target_s *target, watchpoint_t *watchpoint)
1094 uint32_t mask, temp;
1096 /* get pointers to arch-specific information */
1097 armv7m_common_t *armv7m = target->arch_info;
1098 cortex_m3_common_t *cortex_m3 = armv7m->arch_info;
1099 cortex_m3_dwt_comparator_t * comparator_list = cortex_m3->dwt_comparator_list;
1101 if (watchpoint->set)
1103 LOG_WARNING("watchpoint (%d) already set", watchpoint->unique_id );
1107 if (watchpoint->mask == 0xffffffffu)
1109 while (comparator_list[dwt_num].used && (dwt_num < cortex_m3->dwt_num_comp))
1111 if (dwt_num >= cortex_m3->dwt_num_comp)
1113 LOG_DEBUG("ERROR Can not find free DWT Comparator");
1114 LOG_WARNING("ERROR Can not find free DWT Comparator");
1117 watchpoint->set = dwt_num + 1;
1119 temp = watchpoint->length;
1125 comparator_list[dwt_num].used = 1;
1126 comparator_list[dwt_num].comp = watchpoint->address;
1127 comparator_list[dwt_num].mask = mask;
1128 comparator_list[dwt_num].function = watchpoint->rw + 5;
1129 target_write_u32(target, comparator_list[dwt_num].dwt_comparator_address, comparator_list[dwt_num].comp);
1130 target_write_u32(target, comparator_list[dwt_num].dwt_comparator_address | 0x4, comparator_list[dwt_num].mask);
1131 target_write_u32(target, comparator_list[dwt_num].dwt_comparator_address | 0x8, comparator_list[dwt_num].function);
1132 LOG_DEBUG("dwt_num %i 0x%" PRIx32 " 0x%" PRIx32 " 0x%" PRIx32 "", dwt_num, comparator_list[dwt_num].comp, comparator_list[dwt_num].mask, comparator_list[dwt_num].function);
1136 /* Move this test to add_watchpoint */
1137 LOG_WARNING("Cannot watch data values (id: %d)",
1138 watchpoint->unique_id );
1141 LOG_DEBUG("Watchpoint (ID: %d) address: 0x%08" PRIx32 " set=%d ",
1142 watchpoint->unique_id, watchpoint->address, watchpoint->set );
1147 int cortex_m3_unset_watchpoint(struct target_s *target, watchpoint_t *watchpoint)
1149 /* get pointers to arch-specific information */
1150 armv7m_common_t *armv7m = target->arch_info;
1151 cortex_m3_common_t *cortex_m3 = armv7m->arch_info;
1152 cortex_m3_dwt_comparator_t * comparator_list = cortex_m3->dwt_comparator_list;
1155 if (!watchpoint->set)
1157 LOG_WARNING("watchpoint (wpid: %d) not set", watchpoint->unique_id );
1161 LOG_DEBUG("Watchpoint (ID: %d) address: 0x%08" PRIx32 " set=%d ",
1162 watchpoint->unique_id, watchpoint->address,watchpoint->set );
1164 dwt_num = watchpoint->set - 1;
1166 if ((dwt_num < 0) || (dwt_num >= cortex_m3->dwt_num_comp))
1168 LOG_DEBUG("Invalid DWT Comparator number in watchpoint");
1171 comparator_list[dwt_num].used = 0;
1172 comparator_list[dwt_num].function = 0;
1173 target_write_u32(target, comparator_list[dwt_num].dwt_comparator_address | 0x8, comparator_list[dwt_num].function);
1175 watchpoint->set = 0;
1180 int cortex_m3_add_watchpoint(struct target_s *target, watchpoint_t *watchpoint)
1182 /* get pointers to arch-specific information */
1183 armv7m_common_t *armv7m = target->arch_info;
1184 cortex_m3_common_t *cortex_m3 = armv7m->arch_info;
1186 if (target->state != TARGET_HALTED)
1188 LOG_WARNING("target not halted");
1189 return ERROR_TARGET_NOT_HALTED;
1192 if (cortex_m3->dwt_comp_available < 1)
1194 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1197 if ((watchpoint->length != 1) && (watchpoint->length != 2) && (watchpoint->length != 4))
1199 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1202 cortex_m3->dwt_comp_available--;
1203 LOG_DEBUG("dwt_comp_available: %d", cortex_m3->dwt_comp_available);
1208 int cortex_m3_remove_watchpoint(struct target_s *target, watchpoint_t *watchpoint)
1210 /* get pointers to arch-specific information */
1211 armv7m_common_t *armv7m = target->arch_info;
1212 cortex_m3_common_t *cortex_m3 = armv7m->arch_info;
1214 if (target->state != TARGET_HALTED)
1216 LOG_WARNING("target not halted");
1217 return ERROR_TARGET_NOT_HALTED;
1220 if (watchpoint->set)
1222 cortex_m3_unset_watchpoint(target, watchpoint);
1225 cortex_m3->dwt_comp_available++;
1226 LOG_DEBUG("dwt_comp_available: %d", cortex_m3->dwt_comp_available);
1231 void cortex_m3_enable_watchpoints(struct target_s *target)
1233 watchpoint_t *watchpoint = target->watchpoints;
1235 /* set any pending watchpoints */
1238 if (watchpoint->set == 0)
1239 cortex_m3_set_watchpoint(target, watchpoint);
1240 watchpoint = watchpoint->next;
1244 int cortex_m3_load_core_reg_u32(struct target_s *target, enum armv7m_regtype type, uint32_t num, uint32_t * value)
1247 /* get pointers to arch-specific information */
1248 armv7m_common_t *armv7m = target->arch_info;
1249 swjdp_common_t *swjdp = &armv7m->swjdp_info;
1251 /* NOTE: we "know" here that the register identifiers used
1252 * in the v7m header match the Cortex-M3 Debug Core Register
1253 * Selector values for R0..R15, xPSR, MSP, and PSP.
1257 /* read a normal core register */
1258 retval = cortexm3_dap_read_coreregister_u32(swjdp, value, num);
1260 if (retval != ERROR_OK)
1262 LOG_ERROR("JTAG failure %i",retval);
1263 return ERROR_JTAG_DEVICE_ERROR;
1265 LOG_DEBUG("load from core reg %i value 0x%" PRIx32 "",(int)num,*value);
1268 case ARMV7M_PRIMASK:
1269 case ARMV7M_BASEPRI:
1270 case ARMV7M_FAULTMASK:
1271 case ARMV7M_CONTROL:
1272 /* Cortex-M3 packages these four registers as bitfields
1273 * in one Debug Core register. So say r0 and r2 docs;
1274 * it was removed from r1 docs, but still works.
1276 cortexm3_dap_read_coreregister_u32(swjdp, value, 20);
1280 case ARMV7M_PRIMASK:
1281 *value = buf_get_u32((uint8_t*)value, 0, 1);
1284 case ARMV7M_BASEPRI:
1285 *value = buf_get_u32((uint8_t*)value, 8, 8);
1288 case ARMV7M_FAULTMASK:
1289 *value = buf_get_u32((uint8_t*)value, 16, 1);
1292 case ARMV7M_CONTROL:
1293 *value = buf_get_u32((uint8_t*)value, 24, 2);
1297 LOG_DEBUG("load from special reg %i value 0x%" PRIx32 "", (int)num, *value);
1301 return ERROR_INVALID_ARGUMENTS;
1307 int cortex_m3_store_core_reg_u32(struct target_s *target, enum armv7m_regtype type, uint32_t num, uint32_t value)
1312 /* get pointers to arch-specific information */
1313 armv7m_common_t *armv7m = target->arch_info;
1314 swjdp_common_t *swjdp = &armv7m->swjdp_info;
1316 #ifdef ARMV7_GDB_HACKS
1317 /* If the LR register is being modified, make sure it will put us
1318 * in "thumb" mode, or an INVSTATE exception will occur. This is a
1319 * hack to deal with the fact that gdb will sometimes "forge"
1320 * return addresses, and doesn't set the LSB correctly (i.e., when
1321 * printing expressions containing function calls, it sets LR = 0.)
1322 * Valid exception return codes have bit 0 set too.
1324 if (num == ARMV7M_R14)
1328 /* NOTE: we "know" here that the register identifiers used
1329 * in the v7m header match the Cortex-M3 Debug Core Register
1330 * Selector values for R0..R15, xPSR, MSP, and PSP.
1334 retval = cortexm3_dap_write_coreregister_u32(swjdp, value, num);
1335 if (retval != ERROR_OK)
1337 LOG_ERROR("JTAG failure %i", retval);
1338 armv7m->core_cache->reg_list[num].dirty = armv7m->core_cache->reg_list[num].valid;
1339 return ERROR_JTAG_DEVICE_ERROR;
1341 LOG_DEBUG("write core reg %i value 0x%" PRIx32 "", (int)num, value);
1344 case ARMV7M_PRIMASK:
1345 case ARMV7M_BASEPRI:
1346 case ARMV7M_FAULTMASK:
1347 case ARMV7M_CONTROL:
1348 /* Cortex-M3 packages these four registers as bitfields
1349 * in one Debug Core register. So say r0 and r2 docs;
1350 * it was removed from r1 docs, but still works.
1352 cortexm3_dap_read_coreregister_u32(swjdp, ®, 20);
1356 case ARMV7M_PRIMASK:
1357 buf_set_u32((uint8_t*)®, 0, 1, value);
1360 case ARMV7M_BASEPRI:
1361 buf_set_u32((uint8_t*)®, 8, 8, value);
1364 case ARMV7M_FAULTMASK:
1365 buf_set_u32((uint8_t*)®, 16, 1, value);
1368 case ARMV7M_CONTROL:
1369 buf_set_u32((uint8_t*)®, 24, 2, value);
1373 cortexm3_dap_write_coreregister_u32(swjdp, reg, 20);
1375 LOG_DEBUG("write special reg %i value 0x%" PRIx32 " ", (int)num, value);
1379 return ERROR_INVALID_ARGUMENTS;
1385 int cortex_m3_read_memory(struct target_s *target, uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
1387 /* get pointers to arch-specific information */
1388 armv7m_common_t *armv7m = target->arch_info;
1389 swjdp_common_t *swjdp = &armv7m->swjdp_info;
1392 /* sanitize arguments */
1393 if (((size != 4) && (size != 2) && (size != 1)) || (count == 0) || !(buffer))
1394 return ERROR_INVALID_ARGUMENTS;
1396 /* cortex_m3 handles unaligned memory access */
1401 retval = mem_ap_read_buf_u32(swjdp, buffer, 4 * count, address);
1404 retval = mem_ap_read_buf_u16(swjdp, buffer, 2 * count, address);
1407 retval = mem_ap_read_buf_u8(swjdp, buffer, count, address);
1410 LOG_ERROR("BUG: we shouldn't get here");
1417 int cortex_m3_write_memory(struct target_s *target, uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
1419 /* get pointers to arch-specific information */
1420 armv7m_common_t *armv7m = target->arch_info;
1421 swjdp_common_t *swjdp = &armv7m->swjdp_info;
1424 /* sanitize arguments */
1425 if (((size != 4) && (size != 2) && (size != 1)) || (count == 0) || !(buffer))
1426 return ERROR_INVALID_ARGUMENTS;
1431 retval = mem_ap_write_buf_u32(swjdp, buffer, 4 * count, address);
1434 retval = mem_ap_write_buf_u16(swjdp, buffer, 2 * count, address);
1437 retval = mem_ap_write_buf_u8(swjdp, buffer, count, address);
1440 LOG_ERROR("BUG: we shouldn't get here");
1447 int cortex_m3_bulk_write_memory(target_t *target, uint32_t address, uint32_t count, uint8_t *buffer)
1449 return cortex_m3_write_memory(target, address, 4, count, buffer);
1452 void cortex_m3_build_reg_cache(target_t *target)
1454 armv7m_build_reg_cache(target);
1457 int cortex_m3_init_target(struct command_context_s *cmd_ctx, struct target_s *target)
1459 cortex_m3_build_reg_cache(target);
1463 int cortex_m3_examine(struct target_s *target)
1466 uint32_t cpuid, fpcr, dwtcr, ictr;
1469 /* get pointers to arch-specific information */
1470 armv7m_common_t *armv7m = target->arch_info;
1471 cortex_m3_common_t *cortex_m3 = armv7m->arch_info;
1472 swjdp_common_t *swjdp = &armv7m->swjdp_info;
1474 if ((retval = ahbap_debugport_init(swjdp)) != ERROR_OK)
1477 if (!target_was_examined(target))
1479 target_set_examined(target);
1481 /* Read from Device Identification Registers */
1482 if ((retval = target_read_u32(target, CPUID, &cpuid)) != ERROR_OK)
1485 if (((cpuid >> 4) & 0xc3f) == 0xc23)
1486 LOG_DEBUG("CORTEX-M3 processor detected");
1487 LOG_DEBUG("cpuid: 0x%8.8" PRIx32 "", cpuid);
1489 target_read_u32(target, NVIC_ICTR, &ictr);
1490 cortex_m3->intlinesnum = (ictr & 0x1F) + 1;
1491 cortex_m3->intsetenable = calloc(cortex_m3->intlinesnum, 4);
1492 for (i = 0; i < cortex_m3->intlinesnum; i++)
1494 target_read_u32(target, NVIC_ISE0 + 4 * i, cortex_m3->intsetenable + i);
1495 LOG_DEBUG("interrupt enable[%i] = 0x%8.8" PRIx32 "", i, cortex_m3->intsetenable[i]);
1499 target_read_u32(target, FP_CTRL, &fpcr);
1500 cortex_m3->auto_bp_type = 1;
1501 cortex_m3->fp_num_code = ((fpcr >> 8) & 0x70) | ((fpcr >> 4) & 0xF); /* bits [14:12] and [7:4] */
1502 cortex_m3->fp_num_lit = (fpcr >> 8) & 0xF;
1503 cortex_m3->fp_code_available = cortex_m3->fp_num_code;
1504 cortex_m3->fp_comparator_list = calloc(cortex_m3->fp_num_code + cortex_m3->fp_num_lit, sizeof(cortex_m3_fp_comparator_t));
1505 cortex_m3->fpb_enabled = fpcr & 1;
1506 for (i = 0; i < cortex_m3->fp_num_code + cortex_m3->fp_num_lit; i++)
1508 cortex_m3->fp_comparator_list[i].type = (i < cortex_m3->fp_num_code) ? FPCR_CODE : FPCR_LITERAL;
1509 cortex_m3->fp_comparator_list[i].fpcr_address = FP_COMP0 + 4 * i;
1511 LOG_DEBUG("FPB fpcr 0x%" PRIx32 ", numcode %i, numlit %i", fpcr, cortex_m3->fp_num_code, cortex_m3->fp_num_lit);
1514 target_read_u32(target, DWT_CTRL, &dwtcr);
1515 cortex_m3->dwt_num_comp = (dwtcr >> 28) & 0xF;
1516 cortex_m3->dwt_comp_available = cortex_m3->dwt_num_comp;
1517 cortex_m3->dwt_comparator_list = calloc(cortex_m3->dwt_num_comp, sizeof(cortex_m3_dwt_comparator_t));
1518 for (i = 0; i < cortex_m3->dwt_num_comp; i++)
1520 cortex_m3->dwt_comparator_list[i].dwt_comparator_address = DWT_COMP0 + 0x10 * i;
1527 int cortex_m3_quit(void)
1533 int cortex_m3_dcc_read(swjdp_common_t *swjdp, uint8_t *value, uint8_t *ctrl)
1537 mem_ap_read_buf_u16(swjdp, (uint8_t*)&dcrdr, 1, DCB_DCRDR);
1538 *ctrl = (uint8_t)dcrdr;
1539 *value = (uint8_t)(dcrdr >> 8);
1541 LOG_DEBUG("data 0x%x ctrl 0x%x", *value, *ctrl);
1543 /* write ack back to software dcc register
1544 * signify we have read data */
1545 if (dcrdr & (1 << 0))
1548 mem_ap_write_buf_u16(swjdp, (uint8_t*)&dcrdr, 1, DCB_DCRDR);
1554 int cortex_m3_target_request_data(target_t *target, uint32_t size, uint8_t *buffer)
1556 armv7m_common_t *armv7m = target->arch_info;
1557 swjdp_common_t *swjdp = &armv7m->swjdp_info;
1562 for (i = 0; i < (size * 4); i++)
1564 cortex_m3_dcc_read(swjdp, &data, &ctrl);
1571 int cortex_m3_handle_target_request(void *priv)
1573 target_t *target = priv;
1574 if (!target_was_examined(target))
1576 armv7m_common_t *armv7m = target->arch_info;
1577 swjdp_common_t *swjdp = &armv7m->swjdp_info;
1579 if (!target->dbg_msg_enabled)
1582 if (target->state == TARGET_RUNNING)
1587 cortex_m3_dcc_read(swjdp, &data, &ctrl);
1589 /* check if we have data */
1590 if (ctrl & (1 << 0))
1594 /* we assume target is quick enough */
1596 cortex_m3_dcc_read(swjdp, &data, &ctrl);
1597 request |= (data << 8);
1598 cortex_m3_dcc_read(swjdp, &data, &ctrl);
1599 request |= (data << 16);
1600 cortex_m3_dcc_read(swjdp, &data, &ctrl);
1601 request |= (data << 24);
1602 target_request(target, request);
1609 int cortex_m3_init_arch_info(target_t *target, cortex_m3_common_t *cortex_m3, jtag_tap_t *tap)
1612 armv7m_common_t *armv7m;
1613 armv7m = &cortex_m3->armv7m;
1615 armv7m_init_arch_info(target, armv7m);
1617 /* prepare JTAG information for the new target */
1618 cortex_m3->jtag_info.tap = tap;
1619 cortex_m3->jtag_info.scann_size = 4;
1621 armv7m->swjdp_info.dp_select_value = -1;
1622 armv7m->swjdp_info.ap_csw_value = -1;
1623 armv7m->swjdp_info.ap_tar_value = -1;
1624 armv7m->swjdp_info.jtag_info = &cortex_m3->jtag_info;
1625 armv7m->swjdp_info.memaccess_tck = 8;
1626 armv7m->swjdp_info.tar_autoincr_block = (1 << 12); /* Cortex-M3 has 4096 bytes autoincrement range */
1628 /* initialize arch-specific breakpoint handling */
1630 cortex_m3->common_magic = CORTEX_M3_COMMON_MAGIC;
1631 cortex_m3->arch_info = NULL;
1633 /* register arch-specific functions */
1634 armv7m->examine_debug_reason = cortex_m3_examine_debug_reason;
1636 armv7m->pre_debug_entry = NULL;
1637 armv7m->post_debug_entry = NULL;
1639 armv7m->pre_restore_context = NULL;
1640 armv7m->post_restore_context = NULL;
1642 armv7m->arch_info = cortex_m3;
1643 armv7m->load_core_reg_u32 = cortex_m3_load_core_reg_u32;
1644 armv7m->store_core_reg_u32 = cortex_m3_store_core_reg_u32;
1646 target_register_timer_callback(cortex_m3_handle_target_request, 1, 1, target);
1648 if ((retval = arm_jtag_setup_connection(&cortex_m3->jtag_info)) != ERROR_OK)
1656 int cortex_m3_target_create(struct target_s *target, Jim_Interp *interp)
1658 cortex_m3_common_t *cortex_m3 = calloc(1,sizeof(cortex_m3_common_t));
1660 cortex_m3_init_arch_info(target, cortex_m3, target->tap);
1666 * REVISIT Thumb2 disassembly should work for all ARMv7 cores, as well
1667 * as at least ARM-1156T2. The interesting thing about Cortex-M is
1668 * that *only* Thumb2 disassembly matters. There are also some small
1669 * additions to Thumb2 that are specific to ARMv7-M.
1672 handle_cortex_m3_disassemble_command(struct command_context_s *cmd_ctx,
1673 char *cmd, char **args, int argc)
1675 int retval = ERROR_OK;
1676 target_t *target = get_current_target(cmd_ctx);
1678 unsigned long count;
1679 arm_instruction_t cur_instruction;
1682 command_print(cmd_ctx,
1683 "usage: cortex_m3 disassemble <address> <count>");
1688 address = strtoul(args[0], NULL, 0);
1691 count = strtoul(args[1], NULL, 0);
1696 retval = thumb2_opcode(target, address, &cur_instruction);
1697 if (retval != ERROR_OK)
1699 command_print(cmd_ctx, "%s", cur_instruction.text);
1700 address += cur_instruction.instruction_size;
1706 int cortex_m3_register_commands(struct command_context_s *cmd_ctx)
1709 command_t *cortex_m3_cmd;
1711 retval = armv7m_register_commands(cmd_ctx);
1713 cortex_m3_cmd = register_command(cmd_ctx, NULL, "cortex_m3",
1714 NULL, COMMAND_ANY, "cortex_m3 specific commands");
1716 register_command(cmd_ctx, cortex_m3_cmd, "disassemble",
1717 handle_cortex_m3_disassemble_command, COMMAND_EXEC,
1718 "disassemble Thumb2 instructions <address> <count>");
1719 register_command(cmd_ctx, cortex_m3_cmd, "maskisr",
1720 handle_cortex_m3_mask_interrupts_command, COMMAND_EXEC,
1721 "mask cortex_m3 interrupts ['on'|'off']");
1726 int handle_cortex_m3_mask_interrupts_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1728 target_t *target = get_current_target(cmd_ctx);
1729 armv7m_common_t *armv7m = target->arch_info;
1730 cortex_m3_common_t *cortex_m3 = armv7m->arch_info;
1732 if (target->state != TARGET_HALTED)
1734 command_print(cmd_ctx, "target must be stopped for \"%s\" command", cmd);
1740 if (!strcmp(args[0], "on"))
1742 cortex_m3_write_debug_halt_mask(target, C_HALT | C_MASKINTS, 0);
1744 else if (!strcmp(args[0], "off"))
1746 cortex_m3_write_debug_halt_mask(target, C_HALT, C_MASKINTS);
1750 command_print(cmd_ctx, "usage: cortex_m3 maskisr ['on'|'off']");
1754 command_print(cmd_ctx, "cortex_m3 interrupt mask %s",
1755 (cortex_m3->dcb_dhcsr & C_MASKINTS) ? "on" : "off");