spi_flash: validate mcu configuration

Prior to attempting a flash, verify that the MCU is successfully
configured.

Signed-off-by:  Eric Callahan <arksine.code@gmail.com>
This commit is contained in:
Eric Callahan 2022-04-23 06:25:26 -04:00 committed by KevinOConnor
parent 5517a856cc
commit ccae1e3ec8
1 changed files with 21 additions and 5 deletions

View File

@ -119,6 +119,9 @@ FINALIZE_CFG_CMD = "finalize_config crc=%d"
class SPIFlashError(Exception):
pass
class MCUConfigError(SPIFlashError):
pass
class SPIDirect:
def __init__(self, ser):
self.oid = SPI_OID
@ -862,8 +865,7 @@ class MCUConnection:
self._serial.disconnect()
self.connected = False
def check_need_restart(self):
output("Checking Current MCU Configuration...")
def get_mcu_config(self):
# Iterate through backwards compatible response strings
for response in GET_CFG_RESPONSES:
try:
@ -875,7 +877,11 @@ class MCUConnection:
if response == GET_CFG_RESPONSES[-1]:
raise err
output("Trying fallback...")
params = get_cfg_cmd.send()
return get_cfg_cmd.send()
def check_need_restart(self):
output("Checking Current MCU Configuration...")
params = self.get_mcu_config()
output_line("Done")
if params['is_config'] or params['is_shutdown']:
output_line("MCU needs restart: is_config=%d, is_shutdown=%d"
@ -926,9 +932,12 @@ class MCUConnection:
self._serial.send(bus_cmd)
config_crc = zlib.crc32('\n'.join(cfg_cmds).encode()) & 0xffffffff
self._serial.send(FINALIZE_CFG_CMD % (config_crc,))
config = self.get_mcu_config()
if not config["is_config"] or config["is_shutdown"]:
raise MCUConfigError("Failed to configure MCU")
printfunc("Initializing SD Card and Mounting file system...")
self.fatfs = FatFS(self._serial)
self.reactor.pause(self.reactor.monotonic() + .5)
printfunc("Initializing SD Card and Mounting file system...")
try:
self.fatfs.mount(printfunc)
except OSError:
@ -1098,7 +1107,14 @@ class SPIFlash:
if not self.mcu_conn.connected:
self.mcu_conn.connect()
self.old_dictionary = self.mcu_conn.raw_dictionary
self.mcu_conn.configure_mcu(printfunc=output_line)
try:
self.mcu_conn.configure_mcu(printfunc=output_line)
except MCUConfigError:
output_line("MCU configuration failed, attempting restart")
self.need_upload = True
self.mcu_conn.reset()
self.task_complete = True
return
self.firmware_checksum = self.mcu_conn.sdcard_upload()
self.mcu_conn.reset()
self.task_complete = True