- Fixes '=' whitespace
[openocd.git] / src / target / cortex_m3.c
blobc1ebae3f10eede1c4f39aa7005dd2a3a0d73a9af
1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
4 * *
5 * Copyright (C) 2006 by Magnus Lundin *
6 * lundin@mlu.mine.nu *
7 * *
8 * Copyright (C) 2008 by Spencer Oliver *
9 * spen@spen-soft.co.uk *
10 * *
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. *
15 * *
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. *
20 * *
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. *
25 * *
26 * *
27 * Cortex-M3(tm) TRM, ARM DDI 0337C *
28 * *
29 ***************************************************************************/
30 #ifdef HAVE_CONFIG_H
31 #include "config.h"
32 #endif
34 #include "cortex_m3.h"
35 #include "target_request.h"
36 #include "target_type.h"
39 /* cli handling */
40 int cortex_m3_register_commands(struct command_context_s *cmd_ctx);
41 int handle_cortex_m3_mask_interrupts_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
43 /* forward declarations */
44 void cortex_m3_enable_breakpoints(struct target_s *target);
45 void cortex_m3_enable_watchpoints(struct target_s *target);
46 int cortex_m3_target_create(struct target_s *target, Jim_Interp *interp);
47 int cortex_m3_init_target(struct command_context_s *cmd_ctx, struct target_s *target);
48 int cortex_m3_quit(void);
49 int cortex_m3_load_core_reg_u32(target_t *target, enum armv7m_regtype type, uint32_t num, uint32_t *value);
50 int cortex_m3_store_core_reg_u32(target_t *target, enum armv7m_regtype type, uint32_t num, uint32_t value);
51 int cortex_m3_target_request_data(target_t *target, uint32_t size, uint8_t *buffer);
52 int cortex_m3_examine(struct target_s *target);
54 #ifdef ARMV7_GDB_HACKS
55 extern uint8_t armv7m_gdb_dummy_cpsr_value[];
56 extern reg_t armv7m_gdb_dummy_cpsr_reg;
57 #endif
59 target_type_t cortexm3_target =
61 .name = "cortex_m3",
63 .poll = cortex_m3_poll,
64 .arch_state = armv7m_arch_state,
66 .target_request_data = cortex_m3_target_request_data,
68 .halt = cortex_m3_halt,
69 .resume = cortex_m3_resume,
70 .step = cortex_m3_step,
72 .assert_reset = cortex_m3_assert_reset,
73 .deassert_reset = cortex_m3_deassert_reset,
74 .soft_reset_halt = cortex_m3_soft_reset_halt,
76 .get_gdb_reg_list = armv7m_get_gdb_reg_list,
78 .read_memory = cortex_m3_read_memory,
79 .write_memory = cortex_m3_write_memory,
80 .bulk_write_memory = cortex_m3_bulk_write_memory,
81 .checksum_memory = armv7m_checksum_memory,
82 .blank_check_memory = armv7m_blank_check_memory,
84 .run_algorithm = armv7m_run_algorithm,
86 .add_breakpoint = cortex_m3_add_breakpoint,
87 .remove_breakpoint = cortex_m3_remove_breakpoint,
88 .add_watchpoint = cortex_m3_add_watchpoint,
89 .remove_watchpoint = cortex_m3_remove_watchpoint,
91 .register_commands = cortex_m3_register_commands,
92 .target_create = cortex_m3_target_create,
93 .init_target = cortex_m3_init_target,
94 .examine = cortex_m3_examine,
95 .quit = cortex_m3_quit
98 int cortexm3_dap_read_coreregister_u32(swjdp_common_t *swjdp, uint32_t *value, int regnum)
100 int retval;
101 uint32_t dcrdr;
103 /* because the DCB_DCRDR is used for the emulated dcc channel
104 * we gave to save/restore the DCB_DCRDR when used */
106 mem_ap_read_u32(swjdp, DCB_DCRDR, &dcrdr);
108 swjdp->trans_mode = TRANS_MODE_COMPOSITE;
110 /* mem_ap_write_u32(swjdp, DCB_DCRSR, regnum); */
111 dap_setup_accessport(swjdp, CSW_32BIT | CSW_ADDRINC_OFF, DCB_DCRSR & 0xFFFFFFF0);
112 dap_ap_write_reg_u32(swjdp, AP_REG_BD0 | (DCB_DCRSR & 0xC), regnum );
114 /* mem_ap_read_u32(swjdp, DCB_DCRDR, value); */
115 dap_setup_accessport(swjdp, CSW_32BIT | CSW_ADDRINC_OFF, DCB_DCRDR & 0xFFFFFFF0);
116 dap_ap_read_reg_u32(swjdp, AP_REG_BD0 | (DCB_DCRDR & 0xC), value );
118 mem_ap_write_u32(swjdp, DCB_DCRDR, dcrdr);
119 retval = swjdp_transaction_endcheck(swjdp);
120 return retval;
123 int cortexm3_dap_write_coreregister_u32(swjdp_common_t *swjdp, uint32_t value, int regnum)
125 int retval;
126 uint32_t dcrdr;
128 /* because the DCB_DCRDR is used for the emulated dcc channel
129 * we gave to save/restore the DCB_DCRDR when used */
131 mem_ap_read_u32(swjdp, DCB_DCRDR, &dcrdr);
133 swjdp->trans_mode = TRANS_MODE_COMPOSITE;
135 /* mem_ap_write_u32(swjdp, DCB_DCRDR, core_regs[i]); */
136 dap_setup_accessport(swjdp, CSW_32BIT | CSW_ADDRINC_OFF, DCB_DCRDR & 0xFFFFFFF0);
137 dap_ap_write_reg_u32(swjdp, AP_REG_BD0 | (DCB_DCRDR & 0xC), value );
139 /* mem_ap_write_u32(swjdp, DCB_DCRSR, i | DCRSR_WnR ); */
140 dap_setup_accessport(swjdp, CSW_32BIT | CSW_ADDRINC_OFF, DCB_DCRSR & 0xFFFFFFF0);
141 dap_ap_write_reg_u32(swjdp, AP_REG_BD0 | (DCB_DCRSR & 0xC), regnum | DCRSR_WnR );
143 mem_ap_write_u32(swjdp, DCB_DCRDR, dcrdr);
144 retval = swjdp_transaction_endcheck(swjdp);
145 return retval;
149 int cortex_m3_write_debug_halt_mask(target_t *target, uint32_t mask_on, uint32_t mask_off)
151 /* get pointers to arch-specific information */
152 armv7m_common_t *armv7m = target->arch_info;
153 cortex_m3_common_t *cortex_m3 = armv7m->arch_info;
154 swjdp_common_t *swjdp = &armv7m->swjdp_info;
156 /* mask off status bits */
157 cortex_m3->dcb_dhcsr &= ~((0xFFFF << 16) | mask_off);
158 /* create new register mask */
159 cortex_m3->dcb_dhcsr |= DBGKEY | C_DEBUGEN | mask_on;
161 return mem_ap_write_atomic_u32(swjdp, DCB_DHCSR, cortex_m3->dcb_dhcsr);
164 int cortex_m3_clear_halt(target_t *target)
166 /* get pointers to arch-specific information */
167 armv7m_common_t *armv7m = target->arch_info;
168 cortex_m3_common_t *cortex_m3 = armv7m->arch_info;
169 swjdp_common_t *swjdp = &armv7m->swjdp_info;
171 /* clear step if any */
172 cortex_m3_write_debug_halt_mask(target, C_HALT, C_STEP);
174 /* Read Debug Fault Status Register */
175 mem_ap_read_atomic_u32(swjdp, NVIC_DFSR, &cortex_m3->nvic_dfsr);
176 /* Write Debug Fault Status Register to enable processing to resume ?? Try with and without this !! */
177 mem_ap_write_atomic_u32(swjdp, NVIC_DFSR, cortex_m3->nvic_dfsr);
178 LOG_DEBUG(" NVIC_DFSR 0x%" PRIx32 "", cortex_m3->nvic_dfsr);
180 return ERROR_OK;
183 int cortex_m3_single_step_core(target_t *target)
185 /* get pointers to arch-specific information */
186 armv7m_common_t *armv7m = target->arch_info;
187 cortex_m3_common_t *cortex_m3 = armv7m->arch_info;
188 swjdp_common_t *swjdp = &armv7m->swjdp_info;
189 uint32_t dhcsr_save;
191 /* backup dhcsr reg */
192 dhcsr_save = cortex_m3->dcb_dhcsr;
194 /* mask interrupts if not done already */
195 if (!(cortex_m3->dcb_dhcsr & C_MASKINTS))
196 mem_ap_write_atomic_u32(swjdp, DCB_DHCSR, DBGKEY | C_MASKINTS | C_HALT | C_DEBUGEN);
197 mem_ap_write_atomic_u32(swjdp, DCB_DHCSR, DBGKEY | C_MASKINTS | C_STEP | C_DEBUGEN);
198 LOG_DEBUG(" ");
200 /* restore dhcsr reg */
201 cortex_m3->dcb_dhcsr = dhcsr_save;
202 cortex_m3_clear_halt(target);
204 return ERROR_OK;
207 int cortex_m3_exec_opcode(target_t *target,uint32_t opcode, int len /* MODE, r0_invalue, &r0_outvalue */ )
209 /* get pointers to arch-specific information */
210 armv7m_common_t *armv7m = target->arch_info;
211 swjdp_common_t *swjdp = &armv7m->swjdp_info;
212 uint32_t savedram;
213 int retvalue;
215 mem_ap_read_u32(swjdp, 0x20000000, &savedram);
216 mem_ap_write_u32(swjdp, 0x20000000, opcode);
217 cortexm3_dap_write_coreregister_u32(swjdp, 0x20000000, 15);
218 cortex_m3_single_step_core(target);
219 armv7m->core_cache->reg_list[15].dirty = armv7m->core_cache->reg_list[15].valid;
220 retvalue = mem_ap_write_atomic_u32(swjdp, 0x20000000, savedram);
222 return retvalue;
225 #if 0
226 /* Enable interrupts */
227 int cortex_m3_cpsie(target_t *target, uint32_t IF)
229 return cortex_m3_exec_opcode(target, ARMV7M_T_CPSIE(IF), 2);
232 /* Disable interrupts */
233 int cortex_m3_cpsid(target_t *target, uint32_t IF)
235 return cortex_m3_exec_opcode(target, ARMV7M_T_CPSID(IF), 2);
237 #endif
239 int cortex_m3_endreset_event(target_t *target)
241 int i;
242 uint32_t dcb_demcr;
244 /* get pointers to arch-specific information */
245 armv7m_common_t *armv7m = target->arch_info;
246 cortex_m3_common_t *cortex_m3 = armv7m->arch_info;
247 swjdp_common_t *swjdp = &armv7m->swjdp_info;
248 cortex_m3_fp_comparator_t *fp_list = cortex_m3->fp_comparator_list;
249 cortex_m3_dwt_comparator_t *dwt_list = cortex_m3->dwt_comparator_list;
251 mem_ap_read_atomic_u32(swjdp, DCB_DEMCR, &dcb_demcr);
252 LOG_DEBUG("DCB_DEMCR = 0x%8.8" PRIx32 "",dcb_demcr);
254 /* this regsiter is used for emulated dcc channel */
255 mem_ap_write_u32(swjdp, DCB_DCRDR, 0);
257 /* Enable debug requests */
258 mem_ap_read_atomic_u32(swjdp, DCB_DHCSR, &cortex_m3->dcb_dhcsr);
259 if (!(cortex_m3->dcb_dhcsr & C_DEBUGEN))
260 mem_ap_write_u32(swjdp, DCB_DHCSR, DBGKEY | C_DEBUGEN);
262 /* clear any interrupt masking */
263 cortex_m3_write_debug_halt_mask(target, 0, C_MASKINTS);
265 /* Enable trace and dwt */
266 mem_ap_write_u32(swjdp, DCB_DEMCR, TRCENA | VC_HARDERR | VC_BUSERR);
267 /* Monitor bus faults */
268 mem_ap_write_u32(swjdp, NVIC_SHCSR, SHCSR_BUSFAULTENA);
270 /* Enable FPB */
271 target_write_u32(target, FP_CTRL, 3);
272 cortex_m3->fpb_enabled = 1;
274 /* Restore FPB registers */
275 for (i = 0; i < cortex_m3->fp_num_code + cortex_m3->fp_num_lit; i++)
277 target_write_u32(target, fp_list[i].fpcr_address, fp_list[i].fpcr_value);
280 /* Restore DWT registers */
281 for (i = 0; i < cortex_m3->dwt_num_comp; i++)
283 target_write_u32(target, dwt_list[i].dwt_comparator_address, dwt_list[i].comp);
284 target_write_u32(target, dwt_list[i].dwt_comparator_address | 0x4, dwt_list[i].mask);
285 target_write_u32(target, dwt_list[i].dwt_comparator_address | 0x8, dwt_list[i].function);
287 swjdp_transaction_endcheck(swjdp);
289 armv7m_invalidate_core_regs(target);
291 /* make sure we have latest dhcsr flags */
292 mem_ap_read_atomic_u32(swjdp, DCB_DHCSR, &cortex_m3->dcb_dhcsr);
294 return ERROR_OK;
297 int cortex_m3_examine_debug_reason(target_t *target)
299 /* get pointers to arch-specific information */
300 armv7m_common_t *armv7m = target->arch_info;
301 cortex_m3_common_t *cortex_m3 = armv7m->arch_info;
303 /* THIS IS NOT GOOD, TODO - better logic for detection of debug state reason */
304 /* only check the debug reason if we don't know it already */
306 if ((target->debug_reason != DBG_REASON_DBGRQ)
307 && (target->debug_reason != DBG_REASON_SINGLESTEP))
309 /* INCOMPLETE */
311 if (cortex_m3->nvic_dfsr & DFSR_BKPT)
313 target->debug_reason = DBG_REASON_BREAKPOINT;
314 if (cortex_m3->nvic_dfsr & DFSR_DWTTRAP)
315 target->debug_reason = DBG_REASON_WPTANDBKPT;
317 else if (cortex_m3->nvic_dfsr & DFSR_DWTTRAP)
318 target->debug_reason = DBG_REASON_WATCHPOINT;
321 return ERROR_OK;
324 int cortex_m3_examine_exception_reason(target_t *target)
326 uint32_t shcsr, except_sr, cfsr = -1, except_ar = -1;
328 /* get pointers to arch-specific information */
329 armv7m_common_t *armv7m = target->arch_info;
330 swjdp_common_t *swjdp = &armv7m->swjdp_info;
332 mem_ap_read_u32(swjdp, NVIC_SHCSR, &shcsr);
333 switch (armv7m->exception_number)
335 case 2: /* NMI */
336 break;
337 case 3: /* Hard Fault */
338 mem_ap_read_atomic_u32(swjdp, NVIC_HFSR, &except_sr);
339 if (except_sr & 0x40000000)
341 mem_ap_read_u32(swjdp, NVIC_CFSR, &cfsr);
343 break;
344 case 4: /* Memory Management */
345 mem_ap_read_u32(swjdp, NVIC_CFSR, &except_sr);
346 mem_ap_read_u32(swjdp, NVIC_MMFAR, &except_ar);
347 break;
348 case 5: /* Bus Fault */
349 mem_ap_read_u32(swjdp, NVIC_CFSR, &except_sr);
350 mem_ap_read_u32(swjdp, NVIC_BFAR, &except_ar);
351 break;
352 case 6: /* Usage Fault */
353 mem_ap_read_u32(swjdp, NVIC_CFSR, &except_sr);
354 break;
355 case 11: /* SVCall */
356 break;
357 case 12: /* Debug Monitor */
358 mem_ap_read_u32(swjdp, NVIC_DFSR, &except_sr);
359 break;
360 case 14: /* PendSV */
361 break;
362 case 15: /* SysTick */
363 break;
364 default:
365 except_sr = 0;
366 break;
368 swjdp_transaction_endcheck(swjdp);
369 LOG_DEBUG("%s SHCSR 0x%" PRIx32 ", SR 0x%" PRIx32 ", CFSR 0x%" PRIx32 ", AR 0x%" PRIx32 "", armv7m_exception_string(armv7m->exception_number), \
370 shcsr, except_sr, cfsr, except_ar);
371 return ERROR_OK;
374 int cortex_m3_debug_entry(target_t *target)
376 int i;
377 uint32_t xPSR;
378 int retval;
380 /* get pointers to arch-specific information */
381 armv7m_common_t *armv7m = target->arch_info;
382 cortex_m3_common_t *cortex_m3 = armv7m->arch_info;
383 swjdp_common_t *swjdp = &armv7m->swjdp_info;
385 LOG_DEBUG(" ");
386 if (armv7m->pre_debug_entry)
387 armv7m->pre_debug_entry(target);
389 cortex_m3_clear_halt(target);
390 mem_ap_read_atomic_u32(swjdp, DCB_DHCSR, &cortex_m3->dcb_dhcsr);
392 if ((retval = armv7m->examine_debug_reason(target)) != ERROR_OK)
393 return retval;
395 /* Examine target state and mode */
396 /* First load register acessible through core debug port*/
397 for (i = 0; i < ARMV7M_PRIMASK; i++)
399 if (!armv7m->core_cache->reg_list[i].valid)
400 armv7m->read_core_reg(target, i);
403 xPSR = buf_get_u32(armv7m->core_cache->reg_list[ARMV7M_xPSR].value, 0, 32);
405 #ifdef ARMV7_GDB_HACKS
406 /* copy real xpsr reg for gdb, setting thumb bit */
407 buf_set_u32(armv7m_gdb_dummy_cpsr_value, 0, 32, xPSR);
408 buf_set_u32(armv7m_gdb_dummy_cpsr_value, 5, 1, 1);
409 armv7m_gdb_dummy_cpsr_reg.valid = armv7m->core_cache->reg_list[ARMV7M_xPSR].valid;
410 armv7m_gdb_dummy_cpsr_reg.dirty = armv7m->core_cache->reg_list[ARMV7M_xPSR].dirty;
411 #endif
413 /* For IT instructions xPSR must be reloaded on resume and clear on debug exec */
414 if (xPSR & 0xf00)
416 armv7m->core_cache->reg_list[ARMV7M_xPSR].dirty = armv7m->core_cache->reg_list[ARMV7M_xPSR].valid;
417 cortex_m3_store_core_reg_u32(target, ARMV7M_REGISTER_CORE_GP, 16, xPSR &~ 0xff);
420 /* Now we can load SP core registers */
421 for (i = ARMV7M_PRIMASK; i < ARMV7NUMCOREREGS; i++)
423 if (!armv7m->core_cache->reg_list[i].valid)
424 armv7m->read_core_reg(target, i);
427 /* Are we in an exception handler */
428 if (xPSR & 0x1FF)
430 armv7m->core_mode = ARMV7M_MODE_HANDLER;
431 armv7m->exception_number = (xPSR & 0x1FF);
433 else
435 armv7m->core_mode = buf_get_u32(armv7m->core_cache->reg_list[ARMV7M_CONTROL].value, 0, 1);
436 armv7m->exception_number = 0;
439 if (armv7m->exception_number)
441 cortex_m3_examine_exception_reason(target);
444 LOG_DEBUG("entered debug state in core mode: %s at PC 0x%" PRIx32 ", target->state: %s",
445 armv7m_mode_strings[armv7m->core_mode],
446 *(uint32_t*)(armv7m->core_cache->reg_list[15].value),
447 Jim_Nvp_value2name_simple( nvp_target_state, target->state )->name);
449 if (armv7m->post_debug_entry)
450 armv7m->post_debug_entry(target);
452 return ERROR_OK;
455 int cortex_m3_poll(target_t *target)
457 int retval;
458 enum target_state prev_target_state = target->state;
460 /* get pointers to arch-specific information */
461 armv7m_common_t *armv7m = target->arch_info;
462 cortex_m3_common_t *cortex_m3 = armv7m->arch_info;
463 swjdp_common_t *swjdp = &armv7m->swjdp_info;
465 /* Read from Debug Halting Control and Status Register */
466 retval = mem_ap_read_atomic_u32(swjdp, DCB_DHCSR, &cortex_m3->dcb_dhcsr);
467 if (retval != ERROR_OK)
469 target->state = TARGET_UNKNOWN;
470 return retval;
473 if (cortex_m3->dcb_dhcsr & S_RESET_ST)
475 /* check if still in reset */
476 mem_ap_read_atomic_u32(swjdp, DCB_DHCSR, &cortex_m3->dcb_dhcsr);
478 if (cortex_m3->dcb_dhcsr & S_RESET_ST)
480 target->state = TARGET_RESET;
481 return ERROR_OK;
485 if (target->state == TARGET_RESET)
487 /* Cannot switch context while running so endreset is called with target->state == TARGET_RESET */
488 LOG_DEBUG("Exit from reset with dcb_dhcsr 0x%" PRIx32 "", cortex_m3->dcb_dhcsr);
489 cortex_m3_endreset_event(target);
490 target->state = TARGET_RUNNING;
491 prev_target_state = TARGET_RUNNING;
494 if (cortex_m3->dcb_dhcsr & S_HALT)
496 target->state = TARGET_HALTED;
498 if ((prev_target_state == TARGET_RUNNING) || (prev_target_state == TARGET_RESET))
500 if ((retval = cortex_m3_debug_entry(target)) != ERROR_OK)
501 return retval;
503 target_call_event_callbacks(target, TARGET_EVENT_HALTED);
505 if (prev_target_state == TARGET_DEBUG_RUNNING)
507 LOG_DEBUG(" ");
508 if ((retval = cortex_m3_debug_entry(target)) != ERROR_OK)
509 return retval;
511 target_call_event_callbacks(target, TARGET_EVENT_DEBUG_HALTED);
516 if (cortex_m3->dcb_dhcsr & S_SLEEP)
517 target->state = TARGET_SLEEP;
520 #if 0
521 /* Read Debug Fault Status Register, added to figure out the lockup when running flashtest.script */
522 mem_ap_read_atomic_u32(swjdp, NVIC_DFSR, &cortex_m3->nvic_dfsr);
523 LOG_DEBUG("dcb_dhcsr 0x%x, nvic_dfsr 0x%x, target->state: %s", cortex_m3->dcb_dhcsr, cortex_m3->nvic_dfsr, Jim_Nvp_value2name_simple( nvp_target_state, target->state )->name );
524 #endif
526 return ERROR_OK;
529 int cortex_m3_halt(target_t *target)
531 LOG_DEBUG("target->state: %s",
532 Jim_Nvp_value2name_simple(nvp_target_state, target->state )->name);
534 if (target->state == TARGET_HALTED)
536 LOG_DEBUG("target was already halted");
537 return ERROR_OK;
540 if (target->state == TARGET_UNKNOWN)
542 LOG_WARNING("target was in unknown state when halt was requested");
545 if (target->state == TARGET_RESET)
547 if ((jtag_get_reset_config() & RESET_SRST_PULLS_TRST) && jtag_get_srst())
549 LOG_ERROR("can't request a halt while in reset if nSRST pulls nTRST");
550 return ERROR_TARGET_FAILURE;
552 else
554 /* we came here in a reset_halt or reset_init sequence
555 * debug entry was already prepared in cortex_m3_prepare_reset_halt()
557 target->debug_reason = DBG_REASON_DBGRQ;
559 return ERROR_OK;
563 /* Write to Debug Halting Control and Status Register */
564 cortex_m3_write_debug_halt_mask(target, C_HALT, 0);
566 target->debug_reason = DBG_REASON_DBGRQ;
568 return ERROR_OK;
571 int cortex_m3_soft_reset_halt(struct target_s *target)
573 /* get pointers to arch-specific information */
574 armv7m_common_t *armv7m = target->arch_info;
575 cortex_m3_common_t *cortex_m3 = armv7m->arch_info;
576 swjdp_common_t *swjdp = &armv7m->swjdp_info;
577 uint32_t dcb_dhcsr = 0;
578 int retval, timeout = 0;
580 /* Enter debug state on reset, cf. end_reset_event() */
581 mem_ap_write_u32(swjdp, DCB_DEMCR, TRCENA | VC_HARDERR | VC_BUSERR | VC_CORERESET);
583 /* Request a reset */
584 mem_ap_write_atomic_u32(swjdp, NVIC_AIRCR, AIRCR_VECTKEY | AIRCR_VECTRESET);
585 target->state = TARGET_RESET;
587 /* registers are now invalid */
588 armv7m_invalidate_core_regs(target);
590 while (timeout < 100)
592 retval = mem_ap_read_atomic_u32(swjdp, DCB_DHCSR, &dcb_dhcsr);
593 if (retval == ERROR_OK)
595 mem_ap_read_atomic_u32(swjdp, NVIC_DFSR, &cortex_m3->nvic_dfsr);
596 if ((dcb_dhcsr & S_HALT) && (cortex_m3->nvic_dfsr & DFSR_VCATCH))
598 LOG_DEBUG("system reset-halted, dcb_dhcsr 0x%" PRIx32 ", nvic_dfsr 0x%" PRIx32 "", dcb_dhcsr, cortex_m3->nvic_dfsr);
599 cortex_m3_poll(target);
600 return ERROR_OK;
602 else
603 LOG_DEBUG("waiting for system reset-halt, dcb_dhcsr 0x%" PRIx32 ", %i ms", dcb_dhcsr, timeout);
605 timeout++;
606 alive_sleep(1);
609 return ERROR_OK;
612 int cortex_m3_resume(struct target_s *target, int current, uint32_t address, int handle_breakpoints, int debug_execution)
614 /* get pointers to arch-specific information */
615 armv7m_common_t *armv7m = target->arch_info;
616 breakpoint_t *breakpoint = NULL;
617 uint32_t resume_pc;
619 if (target->state != TARGET_HALTED)
621 LOG_WARNING("target not halted");
622 return ERROR_TARGET_NOT_HALTED;
625 if (!debug_execution)
627 target_free_all_working_areas(target);
628 cortex_m3_enable_breakpoints(target);
629 cortex_m3_enable_watchpoints(target);
632 if (debug_execution)
634 /* Disable interrupts */
635 /* We disable interrupts in the PRIMASK register instead of masking with C_MASKINTS,
636 * This is probably the same issue as Cortex-M3 Errata 377493:
637 * C_MASKINTS in parallel with disabled interrupts can cause local faults to not be taken. */
638 buf_set_u32(armv7m->core_cache->reg_list[ARMV7M_PRIMASK].value, 0, 32, 1);
639 armv7m->core_cache->reg_list[ARMV7M_PRIMASK].dirty = 1;
640 armv7m->core_cache->reg_list[ARMV7M_PRIMASK].valid = 1;
642 /* Make sure we are in Thumb mode */
643 buf_set_u32(armv7m->core_cache->reg_list[ARMV7M_xPSR].value, 0, 32,
644 buf_get_u32(armv7m->core_cache->reg_list[ARMV7M_xPSR].value, 0, 32) | (1 << 24));
645 armv7m->core_cache->reg_list[ARMV7M_xPSR].dirty = 1;
646 armv7m->core_cache->reg_list[ARMV7M_xPSR].valid = 1;
649 /* current = 1: continue on current pc, otherwise continue at <address> */
650 if (!current)
652 buf_set_u32(armv7m->core_cache->reg_list[15].value, 0, 32, address);
653 armv7m->core_cache->reg_list[15].dirty = 1;
654 armv7m->core_cache->reg_list[15].valid = 1;
657 resume_pc = buf_get_u32(armv7m->core_cache->reg_list[15].value, 0, 32);
659 armv7m_restore_context(target);
661 /* the front-end may request us not to handle breakpoints */
662 if (handle_breakpoints)
664 /* Single step past breakpoint at current address */
665 if ((breakpoint = breakpoint_find(target, resume_pc)))
667 LOG_DEBUG("unset breakpoint at 0x%8.8" PRIx32 "", breakpoint->address);
668 cortex_m3_unset_breakpoint(target, breakpoint);
669 cortex_m3_single_step_core(target);
670 cortex_m3_set_breakpoint(target, breakpoint);
674 /* Restart core */
675 cortex_m3_write_debug_halt_mask(target, 0, C_HALT);
677 target->debug_reason = DBG_REASON_NOTHALTED;
679 /* registers are now invalid */
680 armv7m_invalidate_core_regs(target);
681 if (!debug_execution)
683 target->state = TARGET_RUNNING;
684 target_call_event_callbacks(target, TARGET_EVENT_RESUMED);
685 LOG_DEBUG("target resumed at 0x%" PRIx32 "", resume_pc);
687 else
689 target->state = TARGET_DEBUG_RUNNING;
690 target_call_event_callbacks(target, TARGET_EVENT_DEBUG_RESUMED);
691 LOG_DEBUG("target debug resumed at 0x%" PRIx32 "", resume_pc);
694 return ERROR_OK;
697 /* int irqstepcount = 0; */
698 int cortex_m3_step(struct target_s *target, int current, uint32_t address, int handle_breakpoints)
700 /* get pointers to arch-specific information */
701 armv7m_common_t *armv7m = target->arch_info;
702 cortex_m3_common_t *cortex_m3 = armv7m->arch_info;
703 swjdp_common_t *swjdp = &armv7m->swjdp_info;
704 breakpoint_t *breakpoint = NULL;
706 if (target->state != TARGET_HALTED)
708 LOG_WARNING("target not halted");
709 return ERROR_TARGET_NOT_HALTED;
712 /* current = 1: continue on current pc, otherwise continue at <address> */
713 if (!current)
714 buf_set_u32(armv7m->core_cache->reg_list[15].value, 0, 32, address);
716 /* the front-end may request us not to handle breakpoints */
717 if (handle_breakpoints)
718 if ((breakpoint = breakpoint_find(target, buf_get_u32(armv7m->core_cache->reg_list[15].value, 0, 32))))
719 cortex_m3_unset_breakpoint(target, breakpoint);
721 target->debug_reason = DBG_REASON_SINGLESTEP;
723 armv7m_restore_context(target);
725 target_call_event_callbacks(target, TARGET_EVENT_RESUMED);
727 /* set step and clear halt */
728 cortex_m3_write_debug_halt_mask(target, C_STEP, C_HALT);
729 mem_ap_read_atomic_u32(swjdp, DCB_DHCSR, &cortex_m3->dcb_dhcsr);
731 /* registers are now invalid */
732 armv7m_invalidate_core_regs(target);
734 if (breakpoint)
735 cortex_m3_set_breakpoint(target, breakpoint);
737 LOG_DEBUG("target stepped dcb_dhcsr = 0x%" PRIx32 " nvic_icsr = 0x%" PRIx32 "", cortex_m3->dcb_dhcsr, cortex_m3->nvic_icsr);
739 cortex_m3_debug_entry(target);
740 target_call_event_callbacks(target, TARGET_EVENT_HALTED);
742 LOG_DEBUG("target stepped dcb_dhcsr = 0x%" PRIx32 " nvic_icsr = 0x%" PRIx32 "", cortex_m3->dcb_dhcsr, cortex_m3->nvic_icsr);
743 return ERROR_OK;
746 int cortex_m3_assert_reset(target_t *target)
748 armv7m_common_t *armv7m = target->arch_info;
749 cortex_m3_common_t *cortex_m3 = armv7m->arch_info;
750 swjdp_common_t *swjdp = &armv7m->swjdp_info;
751 int assert_srst = 1;
753 LOG_DEBUG("target->state: %s",
754 Jim_Nvp_value2name_simple( nvp_target_state, target->state )->name );
756 enum reset_types jtag_reset_config = jtag_get_reset_config();
757 if (!(jtag_reset_config & RESET_HAS_SRST))
759 LOG_ERROR("Can't assert SRST");
760 return ERROR_FAIL;
763 /* Enable debug requests */
764 mem_ap_read_atomic_u32(swjdp, DCB_DHCSR, &cortex_m3->dcb_dhcsr);
765 if (!(cortex_m3->dcb_dhcsr & C_DEBUGEN))
766 mem_ap_write_u32(swjdp, DCB_DHCSR, DBGKEY | C_DEBUGEN);
768 mem_ap_write_u32(swjdp, DCB_DCRDR, 0 );
770 if (!target->reset_halt)
772 /* Set/Clear C_MASKINTS in a separate operation */
773 if (cortex_m3->dcb_dhcsr & C_MASKINTS)
774 mem_ap_write_atomic_u32(swjdp, DCB_DHCSR, DBGKEY | C_DEBUGEN | C_HALT);
776 /* clear any debug flags before resuming */
777 cortex_m3_clear_halt(target);
779 /* clear C_HALT in dhcsr reg */
780 cortex_m3_write_debug_halt_mask(target, 0, C_HALT);
782 /* Enter debug state on reset, cf. end_reset_event() */
783 mem_ap_write_u32(swjdp, DCB_DEMCR, TRCENA | VC_HARDERR | VC_BUSERR);
785 else
787 /* Enter debug state on reset, cf. end_reset_event() */
788 mem_ap_write_atomic_u32(swjdp, DCB_DEMCR, TRCENA | VC_HARDERR | VC_BUSERR | VC_CORERESET);
791 /* following hack is to handle luminary reset
792 * when srst is asserted the luminary device seesm to also clear the debug registers
793 * which does not match the armv7 debug TRM */
795 if (strcmp(target->variant, "lm3s") == 0)
797 /* get revision of lm3s target, only early silicon has this issue
798 * Fury Rev B, DustDevil Rev B, Tempest all ok */
800 uint32_t did0;
802 if (target_read_u32(target, 0x400fe000, &did0) == ERROR_OK)
804 switch ((did0 >> 16) & 0xff)
806 case 0:
807 /* all Sandstorm suffer issue */
808 assert_srst = 0;
809 break;
811 case 1:
812 case 3:
813 /* only Fury/DustDevil rev A suffer reset problems */
814 if (((did0 >> 8) & 0xff) == 0)
815 assert_srst = 0;
816 break;
821 if (assert_srst)
823 /* default to asserting srst */
824 if (jtag_reset_config & RESET_SRST_PULLS_TRST)
826 jtag_add_reset(1, 1);
828 else
830 jtag_add_reset(0, 1);
833 else
835 /* this causes the luminary device to reset using the watchdog */
836 mem_ap_write_atomic_u32(swjdp, NVIC_AIRCR, AIRCR_VECTKEY | AIRCR_SYSRESETREQ);
837 LOG_DEBUG("Using Luminary Reset: SYSRESETREQ");
840 /* I do not know why this is necessary, but it fixes strange effects
841 * (step/resume cause a NMI after reset) on LM3S6918 -- Michael Schwingen */
842 uint32_t tmp;
843 mem_ap_read_atomic_u32(swjdp, NVIC_AIRCR, &tmp);
847 target->state = TARGET_RESET;
848 jtag_add_sleep(50000);
850 armv7m_invalidate_core_regs(target);
852 if (target->reset_halt)
854 int retval;
855 if ((retval = target_halt(target)) != ERROR_OK)
856 return retval;
859 return ERROR_OK;
862 int cortex_m3_deassert_reset(target_t *target)
864 LOG_DEBUG("target->state: %s",
865 Jim_Nvp_value2name_simple(nvp_target_state, target->state )->name);
867 /* deassert reset lines */
868 jtag_add_reset(0, 0);
870 return ERROR_OK;
873 void cortex_m3_enable_breakpoints(struct target_s *target)
875 breakpoint_t *breakpoint = target->breakpoints;
877 /* set any pending breakpoints */
878 while (breakpoint)
880 if (breakpoint->set == 0)
881 cortex_m3_set_breakpoint(target, breakpoint);
882 breakpoint = breakpoint->next;
886 int cortex_m3_set_breakpoint(struct target_s *target, breakpoint_t *breakpoint)
888 int retval;
889 int fp_num = 0;
890 uint32_t hilo;
892 /* get pointers to arch-specific information */
893 armv7m_common_t *armv7m = target->arch_info;
894 cortex_m3_common_t *cortex_m3 = armv7m->arch_info;
896 cortex_m3_fp_comparator_t * comparator_list = cortex_m3->fp_comparator_list;
898 if (breakpoint->set)
900 LOG_WARNING("breakpoint already set");
901 return ERROR_OK;
904 if (cortex_m3->auto_bp_type)
906 breakpoint->type = (breakpoint->address < 0x20000000) ? BKPT_HARD : BKPT_SOFT;
909 if (breakpoint->type == BKPT_HARD)
911 while (comparator_list[fp_num].used && (fp_num < cortex_m3->fp_num_code))
912 fp_num++;
913 if (fp_num >= cortex_m3->fp_num_code)
915 LOG_DEBUG("ERROR Can not find free FP Comparator");
916 LOG_WARNING("ERROR Can not find free FP Comparator");
917 exit(-1);
919 breakpoint->set = fp_num + 1;
920 hilo = (breakpoint->address & 0x2) ? FPCR_REPLACE_BKPT_HIGH : FPCR_REPLACE_BKPT_LOW;
921 comparator_list[fp_num].used = 1;
922 comparator_list[fp_num].fpcr_value = (breakpoint->address & 0x1FFFFFFC) | hilo | 1;
923 target_write_u32(target, comparator_list[fp_num].fpcr_address, comparator_list[fp_num].fpcr_value);
924 LOG_DEBUG("fpc_num %i fpcr_value 0x%" PRIx32 "", fp_num, comparator_list[fp_num].fpcr_value);
925 if (!cortex_m3->fpb_enabled)
927 LOG_DEBUG("FPB wasn't enabled, do it now");
928 target_write_u32(target, FP_CTRL, 3);
931 else if (breakpoint->type == BKPT_SOFT)
933 uint8_t code[4];
934 buf_set_u32(code, 0, 32, ARMV7M_T_BKPT(0x11));
935 if ((retval = target_read_memory(target, breakpoint->address & 0xFFFFFFFE, breakpoint->length, 1, breakpoint->orig_instr)) != ERROR_OK)
937 return retval;
939 if ((retval = target_write_memory(target, breakpoint->address & 0xFFFFFFFE, breakpoint->length, 1, code)) != ERROR_OK)
941 return retval;
943 breakpoint->set = 0x11; /* Any nice value but 0 */
946 return ERROR_OK;
949 int cortex_m3_unset_breakpoint(struct target_s *target, breakpoint_t *breakpoint)
951 int retval;
952 /* get pointers to arch-specific information */
953 armv7m_common_t *armv7m = target->arch_info;
954 cortex_m3_common_t *cortex_m3 = armv7m->arch_info;
955 cortex_m3_fp_comparator_t * comparator_list = cortex_m3->fp_comparator_list;
957 if (!breakpoint->set)
959 LOG_WARNING("breakpoint not set");
960 return ERROR_OK;
963 if (breakpoint->type == BKPT_HARD)
965 int fp_num = breakpoint->set - 1;
966 if ((fp_num < 0) || (fp_num >= cortex_m3->fp_num_code))
968 LOG_DEBUG("Invalid FP Comparator number in breakpoint");
969 return ERROR_OK;
971 comparator_list[fp_num].used = 0;
972 comparator_list[fp_num].fpcr_value = 0;
973 target_write_u32(target, comparator_list[fp_num].fpcr_address, comparator_list[fp_num].fpcr_value);
975 else
977 /* restore original instruction (kept in target endianness) */
978 if (breakpoint->length == 4)
980 if ((retval = target_write_memory(target, breakpoint->address & 0xFFFFFFFE, 4, 1, breakpoint->orig_instr)) != ERROR_OK)
982 return retval;
985 else
987 if ((retval = target_write_memory(target, breakpoint->address & 0xFFFFFFFE, 2, 1, breakpoint->orig_instr)) != ERROR_OK)
989 return retval;
993 breakpoint->set = 0;
995 return ERROR_OK;
998 int cortex_m3_add_breakpoint(struct target_s *target, breakpoint_t *breakpoint)
1000 /* get pointers to arch-specific information */
1001 armv7m_common_t *armv7m = target->arch_info;
1002 cortex_m3_common_t *cortex_m3 = armv7m->arch_info;
1004 if (cortex_m3->auto_bp_type)
1006 breakpoint->type = (breakpoint->address < 0x20000000) ? BKPT_HARD : BKPT_SOFT;
1007 #ifdef ARMV7_GDB_HACKS
1008 if (breakpoint->length != 2) {
1009 /* XXX Hack: Replace all breakpoints with length != 2 with
1010 * a hardware breakpoint. */
1011 breakpoint->type = BKPT_HARD;
1012 breakpoint->length = 2;
1014 #endif
1017 if ((breakpoint->type == BKPT_HARD) && (breakpoint->address >= 0x20000000))
1019 LOG_INFO("flash patch comparator requested outside code memory region");
1020 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1023 if ((breakpoint->type == BKPT_SOFT) && (breakpoint->address < 0x20000000))
1025 LOG_INFO("soft breakpoint requested in code (flash) memory region");
1026 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1029 if ((breakpoint->type == BKPT_HARD) && (cortex_m3->fp_code_available < 1))
1031 LOG_INFO("no flash patch comparator unit available for hardware breakpoint");
1032 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1035 if ((breakpoint->length != 2))
1037 LOG_INFO("only breakpoints of two bytes length supported");
1038 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1041 if (breakpoint->type == BKPT_HARD)
1042 cortex_m3->fp_code_available--;
1043 cortex_m3_set_breakpoint(target, breakpoint);
1045 return ERROR_OK;
1048 int cortex_m3_remove_breakpoint(struct target_s *target, breakpoint_t *breakpoint)
1050 /* get pointers to arch-specific information */
1051 armv7m_common_t *armv7m = target->arch_info;
1052 cortex_m3_common_t *cortex_m3 = armv7m->arch_info;
1054 if (target->state != TARGET_HALTED)
1056 LOG_WARNING("target not halted");
1057 return ERROR_TARGET_NOT_HALTED;
1060 if (cortex_m3->auto_bp_type)
1062 breakpoint->type = (breakpoint->address < 0x20000000) ? BKPT_HARD : BKPT_SOFT;
1065 if (breakpoint->set)
1067 cortex_m3_unset_breakpoint(target, breakpoint);
1070 if (breakpoint->type == BKPT_HARD)
1071 cortex_m3->fp_code_available++;
1073 return ERROR_OK;
1076 int cortex_m3_set_watchpoint(struct target_s *target, watchpoint_t *watchpoint)
1078 int dwt_num = 0;
1079 uint32_t mask, temp;
1081 /* get pointers to arch-specific information */
1082 armv7m_common_t *armv7m = target->arch_info;
1083 cortex_m3_common_t *cortex_m3 = armv7m->arch_info;
1084 cortex_m3_dwt_comparator_t * comparator_list = cortex_m3->dwt_comparator_list;
1086 if (watchpoint->set)
1088 LOG_WARNING("watchpoint already set");
1089 return ERROR_OK;
1092 if (watchpoint->mask == 0xffffffffu)
1094 while (comparator_list[dwt_num].used && (dwt_num < cortex_m3->dwt_num_comp))
1095 dwt_num++;
1096 if (dwt_num >= cortex_m3->dwt_num_comp)
1098 LOG_DEBUG("ERROR Can not find free DWT Comparator");
1099 LOG_WARNING("ERROR Can not find free DWT Comparator");
1100 return -1;
1102 watchpoint->set = dwt_num + 1;
1103 mask = 0;
1104 temp = watchpoint->length;
1105 while (temp > 1)
1107 temp = temp / 2;
1108 mask++;
1110 comparator_list[dwt_num].used = 1;
1111 comparator_list[dwt_num].comp = watchpoint->address;
1112 comparator_list[dwt_num].mask = mask;
1113 comparator_list[dwt_num].function = watchpoint->rw + 5;
1114 target_write_u32(target, comparator_list[dwt_num].dwt_comparator_address, comparator_list[dwt_num].comp);
1115 target_write_u32(target, comparator_list[dwt_num].dwt_comparator_address|0x4, comparator_list[dwt_num].mask);
1116 target_write_u32(target, comparator_list[dwt_num].dwt_comparator_address|0x8, comparator_list[dwt_num].function);
1117 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);
1119 else
1121 LOG_WARNING("Cannot watch data values"); /* Move this test to add_watchpoint */
1122 return ERROR_OK;
1125 return ERROR_OK;
1129 int cortex_m3_unset_watchpoint(struct target_s *target, watchpoint_t *watchpoint)
1131 /* get pointers to arch-specific information */
1132 armv7m_common_t *armv7m = target->arch_info;
1133 cortex_m3_common_t *cortex_m3 = armv7m->arch_info;
1134 cortex_m3_dwt_comparator_t * comparator_list = cortex_m3->dwt_comparator_list;
1135 int dwt_num;
1137 if (!watchpoint->set)
1139 LOG_WARNING("watchpoint not set");
1140 return ERROR_OK;
1143 dwt_num = watchpoint->set - 1;
1145 if ((dwt_num < 0) || (dwt_num >= cortex_m3->dwt_num_comp))
1147 LOG_DEBUG("Invalid DWT Comparator number in watchpoint");
1148 return ERROR_OK;
1150 comparator_list[dwt_num].used = 0;
1151 comparator_list[dwt_num].function = 0;
1152 target_write_u32(target, comparator_list[dwt_num].dwt_comparator_address|0x8, comparator_list[dwt_num].function);
1154 watchpoint->set = 0;
1156 return ERROR_OK;
1159 int cortex_m3_add_watchpoint(struct target_s *target, watchpoint_t *watchpoint)
1161 /* get pointers to arch-specific information */
1162 armv7m_common_t *armv7m = target->arch_info;
1163 cortex_m3_common_t *cortex_m3 = armv7m->arch_info;
1165 if (target->state != TARGET_HALTED)
1167 LOG_WARNING("target not halted");
1168 return ERROR_TARGET_NOT_HALTED;
1171 if (cortex_m3->dwt_comp_available < 1)
1173 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1176 if ((watchpoint->length != 1) && (watchpoint->length != 2) && (watchpoint->length != 4))
1178 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1181 cortex_m3->dwt_comp_available--;
1183 return ERROR_OK;
1186 int cortex_m3_remove_watchpoint(struct target_s *target, watchpoint_t *watchpoint)
1188 /* get pointers to arch-specific information */
1189 armv7m_common_t *armv7m = target->arch_info;
1190 cortex_m3_common_t *cortex_m3 = armv7m->arch_info;
1192 if (target->state != TARGET_HALTED)
1194 LOG_WARNING("target not halted");
1195 return ERROR_TARGET_NOT_HALTED;
1198 if (watchpoint->set)
1200 cortex_m3_unset_watchpoint(target, watchpoint);
1203 cortex_m3->dwt_comp_available++;
1205 return ERROR_OK;
1208 void cortex_m3_enable_watchpoints(struct target_s *target)
1210 watchpoint_t *watchpoint = target->watchpoints;
1212 /* set any pending watchpoints */
1213 while (watchpoint)
1215 if (watchpoint->set == 0)
1216 cortex_m3_set_watchpoint(target, watchpoint);
1217 watchpoint = watchpoint->next;
1221 int cortex_m3_load_core_reg_u32(struct target_s *target, enum armv7m_regtype type, uint32_t num, uint32_t * value)
1223 int retval;
1224 /* get pointers to arch-specific information */
1225 armv7m_common_t *armv7m = target->arch_info;
1226 swjdp_common_t *swjdp = &armv7m->swjdp_info;
1228 if ((type == ARMV7M_REGISTER_CORE_GP) && (num <= ARMV7M_PSP))
1230 /* read a normal core register */
1231 retval = cortexm3_dap_read_coreregister_u32(swjdp, value, num);
1233 if (retval != ERROR_OK)
1235 LOG_ERROR("JTAG failure %i",retval);
1236 return ERROR_JTAG_DEVICE_ERROR;
1238 LOG_DEBUG("load from core reg %i value 0x%" PRIx32 "",(int)num,*value);
1240 else if (type == ARMV7M_REGISTER_CORE_SP) /* Special purpose core register */
1242 /* read other registers */
1243 cortexm3_dap_read_coreregister_u32(swjdp, value, 20);
1245 switch (num)
1247 case 19:
1248 *value = buf_get_u32((uint8_t*)value, 0, 8);
1249 break;
1251 case 20:
1252 *value = buf_get_u32((uint8_t*)value, 8, 8);
1253 break;
1255 case 21:
1256 *value = buf_get_u32((uint8_t*)value, 16, 8);
1257 break;
1259 case 22:
1260 *value = buf_get_u32((uint8_t*)value, 24, 8);
1261 break;
1264 LOG_DEBUG("load from special reg %i value 0x%" PRIx32 "", (int)num, *value);
1266 else
1268 return ERROR_INVALID_ARGUMENTS;
1271 return ERROR_OK;
1274 int cortex_m3_store_core_reg_u32(struct target_s *target, enum armv7m_regtype type, uint32_t num, uint32_t value)
1276 int retval;
1277 uint32_t reg;
1279 /* get pointers to arch-specific information */
1280 armv7m_common_t *armv7m = target->arch_info;
1281 swjdp_common_t *swjdp = &armv7m->swjdp_info;
1283 #ifdef ARMV7_GDB_HACKS
1284 /* If the LR register is being modified, make sure it will put us
1285 * in "thumb" mode, or an INVSTATE exception will occur. This is a
1286 * hack to deal with the fact that gdb will sometimes "forge"
1287 * return addresses, and doesn't set the LSB correctly (i.e., when
1288 * printing expressions containing function calls, it sets LR = 0.) */
1290 if (num == 14)
1291 value |= 0x01;
1292 #endif
1294 if ((type == ARMV7M_REGISTER_CORE_GP) && (num <= ARMV7M_PSP))
1296 retval = cortexm3_dap_write_coreregister_u32(swjdp, value, num);
1297 if (retval != ERROR_OK)
1299 LOG_ERROR("JTAG failure %i", retval);
1300 armv7m->core_cache->reg_list[num].dirty = armv7m->core_cache->reg_list[num].valid;
1301 return ERROR_JTAG_DEVICE_ERROR;
1303 LOG_DEBUG("write core reg %i value 0x%" PRIx32 "", (int)num, value);
1305 else if (type == ARMV7M_REGISTER_CORE_SP) /* Special purpose core register */
1307 /* write other registers */
1309 cortexm3_dap_read_coreregister_u32(swjdp, &reg, 20);
1311 switch (num)
1313 case 19:
1314 buf_set_u32((uint8_t*)&reg, 0, 8, value);
1315 break;
1317 case 20:
1318 buf_set_u32((uint8_t*)&reg, 8, 8, value);
1319 break;
1321 case 21:
1322 buf_set_u32((uint8_t*)&reg, 16, 8, value);
1323 break;
1325 case 22:
1326 buf_set_u32((uint8_t*)&reg, 24, 8, value);
1327 break;
1330 cortexm3_dap_write_coreregister_u32(swjdp, reg, 20);
1332 LOG_DEBUG("write special reg %i value 0x%" PRIx32 " ", (int)num, value);
1334 else
1336 return ERROR_INVALID_ARGUMENTS;
1339 return ERROR_OK;
1342 int cortex_m3_read_memory(struct target_s *target, uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
1344 /* get pointers to arch-specific information */
1345 armv7m_common_t *armv7m = target->arch_info;
1346 swjdp_common_t *swjdp = &armv7m->swjdp_info;
1347 int retval;
1349 /* sanitize arguments */
1350 if (((size != 4) && (size != 2) && (size != 1)) || (count == 0) || !(buffer))
1351 return ERROR_INVALID_ARGUMENTS;
1353 /* cortex_m3 handles unaligned memory access */
1355 switch (size)
1357 case 4:
1358 retval = mem_ap_read_buf_u32(swjdp, buffer, 4 * count, address);
1359 break;
1360 case 2:
1361 retval = mem_ap_read_buf_u16(swjdp, buffer, 2 * count, address);
1362 break;
1363 case 1:
1364 retval = mem_ap_read_buf_u8(swjdp, buffer, count, address);
1365 break;
1366 default:
1367 LOG_ERROR("BUG: we shouldn't get here");
1368 exit(-1);
1371 return retval;
1374 int cortex_m3_write_memory(struct target_s *target, uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
1376 /* get pointers to arch-specific information */
1377 armv7m_common_t *armv7m = target->arch_info;
1378 swjdp_common_t *swjdp = &armv7m->swjdp_info;
1379 int retval;
1381 /* sanitize arguments */
1382 if (((size != 4) && (size != 2) && (size != 1)) || (count == 0) || !(buffer))
1383 return ERROR_INVALID_ARGUMENTS;
1385 switch (size)
1387 case 4:
1388 retval = mem_ap_write_buf_u32(swjdp, buffer, 4 * count, address);
1389 break;
1390 case 2:
1391 retval = mem_ap_write_buf_u16(swjdp, buffer, 2 * count, address);
1392 break;
1393 case 1:
1394 retval = mem_ap_write_buf_u8(swjdp, buffer, count, address);
1395 break;
1396 default:
1397 LOG_ERROR("BUG: we shouldn't get here");
1398 exit(-1);
1401 return retval;
1404 int cortex_m3_bulk_write_memory(target_t *target, uint32_t address, uint32_t count, uint8_t *buffer)
1406 return cortex_m3_write_memory(target, address, 4, count, buffer);
1409 void cortex_m3_build_reg_cache(target_t *target)
1411 armv7m_build_reg_cache(target);
1414 int cortex_m3_init_target(struct command_context_s *cmd_ctx, struct target_s *target)
1416 cortex_m3_build_reg_cache(target);
1417 return ERROR_OK;
1420 int cortex_m3_examine(struct target_s *target)
1422 int retval;
1423 uint32_t cpuid, fpcr, dwtcr, ictr;
1424 int i;
1426 /* get pointers to arch-specific information */
1427 armv7m_common_t *armv7m = target->arch_info;
1428 cortex_m3_common_t *cortex_m3 = armv7m->arch_info;
1429 swjdp_common_t *swjdp = &armv7m->swjdp_info;
1431 if ((retval = ahbap_debugport_init(swjdp)) != ERROR_OK)
1432 return retval;
1434 if (!target_was_examined(target))
1436 target_set_examined(target);
1438 /* Read from Device Identification Registers */
1439 if ((retval = target_read_u32(target, CPUID, &cpuid)) != ERROR_OK)
1440 return retval;
1442 if (((cpuid >> 4) & 0xc3f) == 0xc23)
1443 LOG_DEBUG("CORTEX-M3 processor detected");
1444 LOG_DEBUG("cpuid: 0x%8.8" PRIx32 "", cpuid);
1446 target_read_u32(target, NVIC_ICTR, &ictr);
1447 cortex_m3->intlinesnum = (ictr & 0x1F) + 1;
1448 cortex_m3->intsetenable = calloc(cortex_m3->intlinesnum, 4);
1449 for (i = 0; i < cortex_m3->intlinesnum; i++)
1451 target_read_u32(target, NVIC_ISE0 + 4 * i, cortex_m3->intsetenable + i);
1452 LOG_DEBUG("interrupt enable[%i] = 0x%8.8" PRIx32 "", i, cortex_m3->intsetenable[i]);
1455 /* Setup FPB */
1456 target_read_u32(target, FP_CTRL, &fpcr);
1457 cortex_m3->auto_bp_type = 1;
1458 cortex_m3->fp_num_code = ((fpcr >> 8) & 0x70) | ((fpcr >> 4) & 0xF); /* bits [14:12] and [7:4] */
1459 cortex_m3->fp_num_lit = (fpcr >> 8) & 0xF;
1460 cortex_m3->fp_code_available = cortex_m3->fp_num_code;
1461 cortex_m3->fp_comparator_list = calloc(cortex_m3->fp_num_code + cortex_m3->fp_num_lit, sizeof(cortex_m3_fp_comparator_t));
1462 cortex_m3->fpb_enabled = fpcr & 1;
1463 for (i = 0; i < cortex_m3->fp_num_code + cortex_m3->fp_num_lit; i++)
1465 cortex_m3->fp_comparator_list[i].type = (i < cortex_m3->fp_num_code) ? FPCR_CODE : FPCR_LITERAL;
1466 cortex_m3->fp_comparator_list[i].fpcr_address = FP_COMP0 + 4 * i;
1468 LOG_DEBUG("FPB fpcr 0x%" PRIx32 ", numcode %i, numlit %i", fpcr, cortex_m3->fp_num_code, cortex_m3->fp_num_lit);
1470 /* Setup DWT */
1471 target_read_u32(target, DWT_CTRL, &dwtcr);
1472 cortex_m3->dwt_num_comp = (dwtcr >> 28) & 0xF;
1473 cortex_m3->dwt_comp_available = cortex_m3->dwt_num_comp;
1474 cortex_m3->dwt_comparator_list = calloc(cortex_m3->dwt_num_comp, sizeof(cortex_m3_dwt_comparator_t));
1475 for (i = 0; i < cortex_m3->dwt_num_comp; i++)
1477 cortex_m3->dwt_comparator_list[i].dwt_comparator_address = DWT_COMP0 + 0x10 * i;
1481 return ERROR_OK;
1484 int cortex_m3_quit(void)
1487 return ERROR_OK;
1490 int cortex_m3_dcc_read(swjdp_common_t *swjdp, uint8_t *value, uint8_t *ctrl)
1492 uint16_t dcrdr;
1494 mem_ap_read_buf_u16( swjdp, (uint8_t*)&dcrdr, 1, DCB_DCRDR);
1495 *ctrl = (uint8_t)dcrdr;
1496 *value = (uint8_t)(dcrdr >> 8);
1498 LOG_DEBUG("data 0x%x ctrl 0x%x", *value, *ctrl);
1500 /* write ack back to software dcc register
1501 * signify we have read data */
1502 if (dcrdr & (1 << 0))
1504 dcrdr = 0;
1505 mem_ap_write_buf_u16( swjdp, (uint8_t*)&dcrdr, 1, DCB_DCRDR);
1508 return ERROR_OK;
1511 int cortex_m3_target_request_data(target_t *target, uint32_t size, uint8_t *buffer)
1513 armv7m_common_t *armv7m = target->arch_info;
1514 swjdp_common_t *swjdp = &armv7m->swjdp_info;
1515 uint8_t data;
1516 uint8_t ctrl;
1517 uint32_t i;
1519 for (i = 0; i < (size * 4); i++)
1521 cortex_m3_dcc_read(swjdp, &data, &ctrl);
1522 buffer[i] = data;
1525 return ERROR_OK;
1528 int cortex_m3_handle_target_request(void *priv)
1530 target_t *target = priv;
1531 if (!target_was_examined(target))
1532 return ERROR_OK;
1533 armv7m_common_t *armv7m = target->arch_info;
1534 swjdp_common_t *swjdp = &armv7m->swjdp_info;
1536 if (!target->dbg_msg_enabled)
1537 return ERROR_OK;
1539 if (target->state == TARGET_RUNNING)
1541 uint8_t data;
1542 uint8_t ctrl;
1544 cortex_m3_dcc_read(swjdp, &data, &ctrl);
1546 /* check if we have data */
1547 if (ctrl & (1 << 0))
1549 uint32_t request;
1551 /* we assume target is quick enough */
1552 request = data;
1553 cortex_m3_dcc_read(swjdp, &data, &ctrl);
1554 request |= (data << 8);
1555 cortex_m3_dcc_read(swjdp, &data, &ctrl);
1556 request |= (data << 16);
1557 cortex_m3_dcc_read(swjdp, &data, &ctrl);
1558 request |= (data << 24);
1559 target_request(target, request);
1563 return ERROR_OK;
1566 int cortex_m3_init_arch_info(target_t *target, cortex_m3_common_t *cortex_m3, jtag_tap_t *tap)
1568 int retval;
1569 armv7m_common_t *armv7m;
1570 armv7m = &cortex_m3->armv7m;
1572 armv7m_init_arch_info(target, armv7m);
1574 /* prepare JTAG information for the new target */
1575 cortex_m3->jtag_info.tap = tap;
1576 cortex_m3->jtag_info.scann_size = 4;
1578 armv7m->swjdp_info.dp_select_value = -1;
1579 armv7m->swjdp_info.ap_csw_value = -1;
1580 armv7m->swjdp_info.ap_tar_value = -1;
1581 armv7m->swjdp_info.jtag_info = &cortex_m3->jtag_info;
1582 armv7m->swjdp_info.memaccess_tck = 8;
1583 armv7m->swjdp_info.tar_autoincr_block = (1 << 12); /* Cortex-M3 has 4096 bytes autoincrement range */
1585 /* initialize arch-specific breakpoint handling */
1587 cortex_m3->common_magic = CORTEX_M3_COMMON_MAGIC;
1588 cortex_m3->arch_info = NULL;
1590 /* register arch-specific functions */
1591 armv7m->examine_debug_reason = cortex_m3_examine_debug_reason;
1593 armv7m->pre_debug_entry = NULL;
1594 armv7m->post_debug_entry = NULL;
1596 armv7m->pre_restore_context = NULL;
1597 armv7m->post_restore_context = NULL;
1599 armv7m->arch_info = cortex_m3;
1600 armv7m->load_core_reg_u32 = cortex_m3_load_core_reg_u32;
1601 armv7m->store_core_reg_u32 = cortex_m3_store_core_reg_u32;
1603 target_register_timer_callback(cortex_m3_handle_target_request, 1, 1, target);
1605 if ((retval = arm_jtag_setup_connection(&cortex_m3->jtag_info)) != ERROR_OK)
1607 return retval;
1610 return ERROR_OK;
1613 int cortex_m3_target_create(struct target_s *target, Jim_Interp *interp)
1615 cortex_m3_common_t *cortex_m3 = calloc(1,sizeof(cortex_m3_common_t));
1617 cortex_m3_init_arch_info(target, cortex_m3, target->tap);
1619 return ERROR_OK;
1622 int cortex_m3_register_commands(struct command_context_s *cmd_ctx)
1624 int retval;
1625 command_t *cortex_m3_cmd;
1627 retval = armv7m_register_commands(cmd_ctx);
1629 cortex_m3_cmd = register_command(cmd_ctx, NULL, "cortex_m3", NULL, COMMAND_ANY, "cortex_m3 specific commands");
1630 register_command(cmd_ctx, cortex_m3_cmd, "maskisr", handle_cortex_m3_mask_interrupts_command, COMMAND_EXEC, "mask cortex_m3 interrupts ['on'|'off']");
1632 return retval;
1635 int handle_cortex_m3_mask_interrupts_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1637 target_t *target = get_current_target(cmd_ctx);
1638 armv7m_common_t *armv7m = target->arch_info;
1639 cortex_m3_common_t *cortex_m3 = armv7m->arch_info;
1641 if (target->state != TARGET_HALTED)
1643 command_print(cmd_ctx, "target must be stopped for \"%s\" command", cmd);
1644 return ERROR_OK;
1647 if (argc > 0)
1649 if (!strcmp(args[0], "on"))
1651 cortex_m3_write_debug_halt_mask(target, C_HALT|C_MASKINTS, 0);
1653 else if (!strcmp(args[0], "off"))
1655 cortex_m3_write_debug_halt_mask(target, C_HALT, C_MASKINTS);
1657 else
1659 command_print(cmd_ctx, "usage: cortex_m3 maskisr ['on'|'off']");
1663 command_print(cmd_ctx, "cortex_m3 interrupt mask %s",
1664 (cortex_m3->dcb_dhcsr & C_MASKINTS) ? "on" : "off");
1666 return ERROR_OK;