From 9c933a55cf138cc0b4a6bb98fd6d996c88c431ac Mon Sep 17 00:00:00 2001 From: Chris Reed Date: Sun, 17 May 2020 12:41:29 -0500 Subject: [PATCH] Merge pull request #873 from flit/bugfix/flash_verify_init_failure Several flash related fixes --- pyocd/core/coresight_target.py | 2 +- pyocd/flash/builder.py | 6 +++++- pyocd/gdbserver/gdbserver.py | 13 +++++++------ pyocd/target/builtin/target_MKL28Z512xxx7.py | 4 +--- pyocd/tools/pyocd.py | 10 ++++++++-- 5 files changed, 22 insertions(+), 13 deletions(-) diff --git a/pyocd/core/coresight_target.py b/pyocd/core/coresight_target.py index bc8961407..3e599ac82 100644 --- a/pyocd/core/coresight_target.py +++ b/pyocd/core/coresight_target.py @@ -135,7 +135,6 @@ def add_core(self, core): def create_init_sequence(self): seq = CallSequence( ('load_svd', self.load_svd), - ('create_flash', self.create_flash), ('pre_connect', self.pre_connect), ('dp_init', self.dp.init_sequence), ('create_discoverer', self.create_discoverer), @@ -144,6 +143,7 @@ def create_init_sequence(self): ('halt_on_connect', self.perform_halt_on_connect), ('post_connect', self.post_connect), ('post_connect_hook', self.post_connect_hook), + ('create_flash', self.create_flash), ('notify', lambda : self.session.notify(Target.Event.POST_CONNECT, self)) ) diff --git a/pyocd/flash/builder.py b/pyocd/flash/builder.py index fd57f43e0..e10c51e69 100644 --- a/pyocd/flash/builder.py +++ b/pyocd/flash/builder.py @@ -218,7 +218,11 @@ def _enable_read_access(self): memory mapped and accessible. """ if not self.algo_inited_for_read: - self.flash.init(self.flash.Operation.VERIFY) + try: + self.flash.init(self.flash.Operation.VERIFY) + except FlashFailure: + # If initing for verify fails, then try again in erase mode. + self.flash.init(self.flash.Operation.ERASE) self.algo_inited_for_read = True def _build_sectors_and_pages(self, keep_unwritten): diff --git a/pyocd/gdbserver/gdbserver.py b/pyocd/gdbserver/gdbserver.py index 1595d7d4b..c5b81287e 100644 --- a/pyocd/gdbserver/gdbserver.py +++ b/pyocd/gdbserver/gdbserver.py @@ -714,12 +714,13 @@ def flash_op(self, data): elif b'FlashDone' in ops : # Only program if we received data. if self.flash_loader is not None: - # Write all buffered flash contents. - self.flash_loader.commit() - - # Set flash loader to None so that on the next flash command a new - # object is used. - self.flash_loader = None + try: + # Write all buffered flash contents. + self.flash_loader.commit() + finally: + # Set flash loader to None so that on the next flash command a new + # object is used. + self.flash_loader = None self.first_run_after_reset_or_flash = True if self.thread_provider is not None: diff --git a/pyocd/target/builtin/target_MKL28Z512xxx7.py b/pyocd/target/builtin/target_MKL28Z512xxx7.py index ada561776..faca9c5b4 100644 --- a/pyocd/target/builtin/target_MKL28Z512xxx7.py +++ b/pyocd/target/builtin/target_MKL28Z512xxx7.py @@ -132,14 +132,12 @@ def __init__(self, target): self._saved_firccsr = 0 self._saved_rccr = 0 - def prepare_target(self, operation, address=None, clock=0, reset=True): + def prepare_target(self): """! This function sets up target clocks to ensure that flash is clocked at the maximum of 24MHz. Doing so gets the best flash programming performance. The FIRC clock source is used so that there is no dependency on an external crystal frequency. """ - super(Flash_kl28z, self).init(operation, address, clock, reset) - # Enable FIRC. value = self.target.read32(SCG_FIRCCSR) self._saved_firccsr = value diff --git a/pyocd/tools/pyocd.py b/pyocd/tools/pyocd.py index 65ffcf32c..a63610bfc 100755 --- a/pyocd/tools/pyocd.py +++ b/pyocd/tools/pyocd.py @@ -1028,7 +1028,10 @@ def handle_savemem(self, args): region = self.session.target.memory_map.get_region_for_address(addr) flash_init_required = region is not None and region.is_flash and not region.is_powered_on_boot and region.flash is not None if flash_init_required: - region.flash.init(region.flash.Operation.VERIFY) + try: + region.flash.init(region.flash.Operation.VERIFY) + except exceptions.FlashFailure: + region.flash.init(region.flash.Operation.ERASE) data = bytearray(self.target.aps[self.selected_ap].read_memory_block8(addr, count)) @@ -1082,7 +1085,10 @@ def handle_compare(self, args): region = self.session.target.memory_map.get_region_for_address(addr) flash_init_required = region is not None and region.is_flash and not region.is_powered_on_boot and region.flash is not None if flash_init_required: - region.flash.init(region.flash.Operation.VERIFY) + try: + region.flash.init(region.flash.Operation.VERIFY) + except exceptions.FlashFailure: + region.flash.init(region.flash.Operation.ERASE) with open(filename, 'rb') as f: file_data = f.read(length)