From 520bb88d70893287b2c2728259cd020ddc710eaf Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Thu, 15 Oct 2020 20:09:04 +1100 Subject: [PATCH] stm32/boards/NUCLEO_WB55/rfcore_firmware.py: Fix flash unlock. The flash can sometimes be in an already-unlocked state, and attempting to unlock it again will cause an immediate reset. So make _Flash.unlock() check FLASH_CR_LOCK to get the current state. Also fix some magic numbers for FLASH_CR_LOCK AND FLASH_CR_STRT. The machine.reset() could be removed because it no longer crashes now that the flash unlock is fixed. Signed-off-by: Jim Mussared --- .../boards/NUCLEO_WB55/rfcore_firmware.py | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/ports/stm32/boards/NUCLEO_WB55/rfcore_firmware.py b/ports/stm32/boards/NUCLEO_WB55/rfcore_firmware.py index 53c96a552a..de98d97430 100644 --- a/ports/stm32/boards/NUCLEO_WB55/rfcore_firmware.py +++ b/ports/stm32/boards/NUCLEO_WB55/rfcore_firmware.py @@ -189,23 +189,31 @@ class _Flash: _FLASH_KEY1 = 0x45670123 _FLASH_KEY2 = 0xCDEF89AB + _FLASH_CR_STRT_MASK = 1 << 16 + _FLASH_CR_LOCK_MASK = 1 << 31 + _FLASH_SR_BSY_MASK = 1 << 16 + def wait_not_busy(self): - while machine.mem32[stm.FLASH + stm.FLASH_SR] & 1 << 16: + while machine.mem32[stm.FLASH + stm.FLASH_SR] & _Flash._FLASH_SR_BSY_MASK: machine.idle() def unlock(self): - machine.mem32[stm.FLASH + stm.FLASH_KEYR] = _Flash._FLASH_KEY1 - machine.mem32[stm.FLASH + stm.FLASH_KEYR] = _Flash._FLASH_KEY2 + if machine.mem32[stm.FLASH + stm.FLASH_CR] & _Flash._FLASH_CR_LOCK_MASK: + # Only unlock if already locked (i.e. FLASH_CR_LOCK is set). + machine.mem32[stm.FLASH + stm.FLASH_KEYR] = _Flash._FLASH_KEY1 + machine.mem32[stm.FLASH + stm.FLASH_KEYR] = _Flash._FLASH_KEY2 + else: + log("Flash was already unlocked.") def lock(self): - machine.mem32[stm.FLASH + stm.FLASH_CR] = 1 << 31 # LOCK + machine.mem32[stm.FLASH + stm.FLASH_CR] = _Flash._FLASH_CR_LOCK_MASK def erase_page(self, page): assert 0 <= page <= 255 # 1MiB range (4k page) self.wait_not_busy() cr = page << 3 | 1 << 1 # PNB # PER machine.mem32[stm.FLASH + stm.FLASH_CR] = cr - machine.mem32[stm.FLASH + stm.FLASH_CR] = cr | 1 << 16 # STRT + machine.mem32[stm.FLASH + stm.FLASH_CR] = cr | _Flash._FLASH_CR_STRT_MASK self.wait_not_busy() machine.mem32[stm.FLASH + stm.FLASH_CR] = 0 @@ -472,8 +480,6 @@ def resume(): elif status == 0: log("FUS update successful") _write_state(_STATE_CHECK_UPDATES) - # Need to force a reset after FUS install otherwise a subsequent flash copy will fail. - machine.reset() elif result == 0: # See below (for equivalent path for WS install -- we # sometimes see (255,0) right at the end).