Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def reset(self, reset_type=None):
self.session.notify(Target.Event.PRE_RESET, self)
self._run_token += 1
if reset_type is Target.ResetType.HW:
self.session.probe.reset()
self.reinit_dap()
self.fpb.enable()
else:
if reset_type is Target.ResetType.SW_VECTRESET:
mask = CortexM.NVIC_AIRCR_VECTRESET
else:
mask = CortexM.NVIC_AIRCR_SYSRESETREQ
try:
self.write_memory(CortexM.NVIC_AIRCR, CortexM.NVIC_AIRCR_VECTKEY | mask)
self.flush()
except exceptions.TransferError:
self.flush()
with Timeout(5.0) as t_o:
while t_o.check():
try:
self._ap.dp.init()
self._ap.dp.power_up_debug()
dhcsr_reg = self.read32(CortexM.DHCSR)
if (dhcsr_reg & CortexM.S_RESET_ST) == 0:
break
self.flush()
except Exception as e:
print(f'must be integer betwin 0 and {len(daplinks) - 1}')
self.daplink = daplinks[n]
try:
self.daplink.open()
_dp = dap.DebugPort(self.daplink, None)
_dp.init()
_dp.power_up_debug()
_ap = ap.AHB_AP(_dp, 0)
_ap.init()
self.dap = cortex_m.CortexM(None, _ap)
self.dap._read_core_type()
print(f'IDCODE: 0x{_dp.dpidr:08X}')
print(f'CPU core is {cortex_m.CORE_TYPE_NAME[self.dap.core_type]}\n')
except Exception as e:
print('no chip found\n')
def reset_and_halt(self, reset_type=None):
"""! @brief Perform a reset and stop the core on the reset handler. """
catch_mode = 0
delegateResult = self.call_delegate('set_reset_catch', core=self, reset_type=reset_type)
# Save CortexM.DEMCR
demcr = self.read_memory(CortexM.DEMCR)
# enable the vector catch
if not delegateResult:
# This sequence is copied from the NXP LPC55S69_DFP debug sequence.
reset_vector = 0xFFFFFFFF
# Clear reset vector catch.
self.write32(CortexM.DEMCR, demcr & ~CortexM.DEMCR_VC_CORERESET)
# If the processor is in Secure state, we have to access the flash controller
# through the secure alias.
if self.get_security_state() == Target.SecurityState.SECURE:
base = PERIPHERAL_BASE_S
else:
base = PERIPHERAL_BASE_NS
self._run_token += 1
# Give the delegate a chance to overide reset. If the delegate returns True, then it
# handled the reset on its own.
if not self.call_delegate('will_reset', core=self, reset_type=reset_type):
self._perform_reset(reset_type)
self.call_delegate('did_reset', core=self, reset_type=reset_type)
# Now wait for the system to come out of reset. Keep reading the DHCSR until
# we get a good response with S_RESET_ST cleared, or we time out.
with timeout.Timeout(2.0) as t_o:
while t_o.check():
try:
dhcsr = self.read32(CortexM.DHCSR)
if (dhcsr & CortexM.S_RESET_ST) == 0:
break
except exceptions.TransferError:
self.flush()
sleep(0.01)
self.notify(Notification(event=Target.EVENT_POST_RESET, source=self))
def _map_to_vector_catch_mask(mask):
result = 0
if mask & Target.CATCH_HARD_FAULT:
result |= CortexM.DEMCR_VC_HARDERR
if mask & Target.CATCH_BUS_FAULT:
result |= CortexM.DEMCR_VC_BUSERR
if mask & Target.CATCH_MEM_FAULT:
result |= CortexM.DEMCR_VC_MMERR
if mask & Target.CATCH_INTERRUPT_ERR:
result |= CortexM.DEMCR_VC_INTERR
if mask & Target.CATCH_STATE_ERR:
result |= CortexM.DEMCR_VC_STATERR
if mask & Target.CATCH_CHECK_ERR:
result |= CortexM.DEMCR_VC_CHKERR
if mask & Target.CATCH_COPROCESSOR_ERR:
result |= CortexM.DEMCR_VC_NOCPERR
if mask & Target.CATCH_CORE_RESET:
result |= CortexM.DEMCR_VC_CORERESET
return result
def is_debug_trap(self):
debugEvents = self.read_memory(CortexM.DFSR) & (CortexM.DFSR_DWTTRAP | CortexM.DFSR_BKPT | CortexM.DFSR_HALTED)
return debugEvents != 0
return
self.notify(Notification(event=Target.EVENT_PRE_RUN, source=self, data=Target.RUN_TYPE_STEP))
self.clear_debug_cause_bits()
# Save previous interrupt mask state
interrupts_masked = (CortexM.C_MASKINTS & dhcsr) != 0
# Mask interrupts - C_HALT must be set when changing to C_MASKINTS
if not interrupts_masked and disable_interrupts:
self.write_memory(CortexM.DHCSR, CortexM.DBGKEY | CortexM.C_DEBUGEN | CortexM.C_HALT | CortexM.C_MASKINTS)
# Single step using current C_MASKINTS setting
if disable_interrupts or interrupts_masked:
self.write_memory(CortexM.DHCSR, CortexM.DBGKEY | CortexM.C_DEBUGEN | CortexM.C_MASKINTS | CortexM.C_STEP)
else:
self.write_memory(CortexM.DHCSR, CortexM.DBGKEY | CortexM.C_DEBUGEN | CortexM.C_STEP)
# Wait for halt to auto set (This should be done before the first read)
while not self.read_memory(CortexM.DHCSR) & CortexM.C_HALT:
pass
# Restore interrupt mask state
if not interrupts_masked and disable_interrupts:
# Unmask interrupts - C_HALT must be set when changing to C_MASKINTS
self.write_memory(CortexM.DHCSR, CortexM.DBGKEY | CortexM.C_DEBUGEN | CortexM.C_HALT)
self.flush()
self._run_token += 1