summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJim Mussared <jim.mussared@gmail.com>2020-10-15 20:09:04 +1100
committerDamien George <damien@micropython.org>2020-10-16 10:43:36 +1100
commit520bb88d70893287b2c2728259cd020ddc710eaf (patch)
treedf659a3ca4248199d26d34b196746d1a2cf7a2ae
parentb137d064e9e0bfebd2a59a9b312935031252e742 (diff)
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 <jim.mussared@gmail.com>
-rw-r--r--ports/stm32/boards/NUCLEO_WB55/rfcore_firmware.py20
1 files 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 53c96a552..de98d9743 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).