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>
This commit is contained in:
Jim Mussared 2020-10-15 20:09:04 +11:00 committed by Damien George
parent b137d064e9
commit 520bb88d70

View File

@ -189,23 +189,31 @@ class _Flash:
_FLASH_KEY1 = 0x45670123 _FLASH_KEY1 = 0x45670123
_FLASH_KEY2 = 0xCDEF89AB _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): 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() machine.idle()
def unlock(self): def unlock(self):
machine.mem32[stm.FLASH + stm.FLASH_KEYR] = _Flash._FLASH_KEY1 if machine.mem32[stm.FLASH + stm.FLASH_CR] & _Flash._FLASH_CR_LOCK_MASK:
machine.mem32[stm.FLASH + stm.FLASH_KEYR] = _Flash._FLASH_KEY2 # 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): 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): def erase_page(self, page):
assert 0 <= page <= 255 # 1MiB range (4k page) assert 0 <= page <= 255 # 1MiB range (4k page)
self.wait_not_busy() self.wait_not_busy()
cr = page << 3 | 1 << 1 # PNB # PER cr = page << 3 | 1 << 1 # PNB # PER
machine.mem32[stm.FLASH + stm.FLASH_CR] = cr 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() self.wait_not_busy()
machine.mem32[stm.FLASH + stm.FLASH_CR] = 0 machine.mem32[stm.FLASH + stm.FLASH_CR] = 0
@ -472,8 +480,6 @@ def resume():
elif status == 0: elif status == 0:
log("FUS update successful") log("FUS update successful")
_write_state(_STATE_CHECK_UPDATES) _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: elif result == 0:
# See below (for equivalent path for WS install -- we # See below (for equivalent path for WS install -- we
# sometimes see (255,0) right at the end). # sometimes see (255,0) right at the end).