Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_timeout_a(self):
s = time()
with Timeout(0.05) as to:
while to.check():
sleep(0.01)
assert to.did_time_out
assert (time() - s) >= 0.05
def _wait_for_flash_init(self):
# Wait until flash is inited.
with Timeout(MASS_ERASE_TIMEOUT) as to:
while to.check():
status = self.mdm_ap.read_reg(MDM_STATUS)
if status & MDM_STATUS_FLASH_READY:
break
sleep(0.01)
return not to.did_time_out
def reinit_dap(self):
with Timeout(2.0) as t_o:
while t_o.check():
try:
self._ap.dp.init()
self._ap.dp.power_up_debug()
self.flush()
break
except exceptions.TransferError:
self.flush()
else:
LOG.error("Failed to initialize DAP")
# This is ugly, but FPB gets disabled after HW Reset so breakpoints stop working
self.bp_manager._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:
dhcsr_reg = self.read32(CortexM.DHCSR)
if (dhcsr_reg & CortexM.S_RESET_ST) == 0:
break
except exceptions.TransferError:
self.flush()
self._ap.dp.init()
self._ap.dp.power_up_debug()
sleep(0.01)
self.session.notify(Target.EVENT_POST_RESET, self)
def acquire(self):
with Timeout(self.acquire_timeout) as t_o:
while t_o.check():
try:
self._ap.dp.init()
self._ap.dp.power_up_debug()
self.write32(self.IPC2_DATA_ADDR, 0)
self.write32(self.TEST_MODE_ADDR, self.TEST_MODE_VALUE)
self.flush()
break
except exceptions.TransferError:
pass
else:
LOG.warning("Failed to enter test mode")
# halt the target
if not delegateResult:
self.halt()
# Save CortexM.DEMCR
demcr = self.read_memory(CortexM.DEMCR)
# enable the vector catch
if not delegateResult:
self.write_memory(CortexM.DEMCR, demcr | CortexM.DEMCR_VC_CORERESET)
self.reset(reset_type)
# wait until the unit resets
with timeout.Timeout(2.0) as t_o:
while t_o.check():
if self.get_state() not in (Target.TARGET_RESET, Target.TARGET_RUNNING):
break
sleep(0.01)
# Make sure the thumb bit is set in XPSR in case the reset handler
# points to an invalid address.
xpsr = self.read_core_register('xpsr')
if xpsr & self.XPSR_THUMB == 0:
self.write_core_register('xpsr', xpsr | self.XPSR_THUMB)
self.call_delegate('clear_reset_catch', core=self, reset_type=reset_type)
# restore vector catch setting
self.write_memory(CortexM.DEMCR, demcr)
def wait_halted(self):
with Timeout(5.0) as t_o:
while t_o.check():
try:
if not self.is_running():
break
except exceptions.TransferError:
self.flush()
sleep(0.01)
else:
raise Exception("Timeout waiting for target halt")
def mass_erase(self):
# See Nordic Whitepaper nWP-027 for magic numbers and order of operations from the vendor
self.ctrl_ap.write_reg(CTRL_AP_ERASEALL, CTRL_AP_ERASEALL_ERASE)
with Timeout(MASS_ERASE_TIMEOUT) as to:
while to.check():
status = self.ctrl_ap.read_reg(CTRL_AP_ERASEALLSTATUS)
if status == CTRL_AP_ERASEALLSTATUS_READY:
break
sleep(0.1)
else:
# Timed out
LOG.error("Mass erase timeout waiting for ERASEALLSTATUS")
return False
self.ctrl_ap.write_reg(CTRL_AP_RESET, CTRL_AP_RESET_RESET)
self.ctrl_ap.write_reg(CTRL_AP_RESET, CTRL_AP_RESET_NORESET)
self.ctrl_ap.write_reg(CTRL_AP_ERASEALL, CTRL_AP_ERASEALL_NOOPERATION)
return True