From 3c7ae8ca17606f63c35bf9bb0451668dd5ccb6d9 Mon Sep 17 00:00:00 2001 From: Matthew Kennedy Date: Sat, 7 Mar 2020 07:23:30 -0800 Subject: [PATCH] Simplify flash logic (#1154) * simplify flash stuff * put those back for now * and those * extra check * fix * crc the correct thing * undo crc changes * unwind --- firmware/controllers/flash_main.cpp | 44 ++++++++++++++--------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/firmware/controllers/flash_main.cpp b/firmware/controllers/flash_main.cpp index 8f93fad206..9bc8d2252b 100644 --- a/firmware/controllers/flash_main.cpp +++ b/firmware/controllers/flash_main.cpp @@ -35,13 +35,6 @@ extern persistent_config_container_s persistentState; extern engine_configuration_s *engineConfiguration; -/** - * this address needs to be above 'flash' region available for firmware - * todo: an ideal solution would be to define this address in the .ld / .icf mapping file - */ - -#define PERSISTENT_SIZE sizeof(persistent_config_container_s) - /** * https://sourceforge.net/p/rusefi/tickets/335/ * @@ -72,22 +65,29 @@ void writeToFlashIfPending() { writeToFlashNow(); } +// Erase and write a copy of the configuration at the specified address +template +int eraseAndFlashCopy(flashaddr_t storageAddress, const TStorage& data) +{ + flashErase(storageAddress, sizeof(TStorage)); + return flashWrite(storageAddress, reinterpret_cast(&data), sizeof(TStorage)); +} + void writeToFlashNow(void) { scheduleMsg(logger, " !!!!!!!!!!!!!!!!!!!! BE SURE NOT WRITE WITH IGNITION ON !!!!!!!!!!!!!!!!!!!!"); - persistentState.size = PERSISTENT_SIZE; + + // Set up the container + persistentState.size = sizeof(persistentState); persistentState.version = FLASH_DATA_VERSION; - scheduleMsg(logger, "flash compatible with %d", persistentState.version); - crc_t crcResult = flashStateCrc(&persistentState); - persistentState.value = crcResult; - scheduleMsg(logger, "Reseting flash: size=%d", PERSISTENT_SIZE); - flashErase(getFlashAddrFirstCopy(), PERSISTENT_SIZE); - scheduleMsg(logger, "Flashing with CRC=%d", crcResult); - efitimems_t nowMs = currentTimeMillis(); - int result = flashWrite(getFlashAddrFirstCopy(), (const char *) &persistentState, PERSISTENT_SIZE); - flashErase(getFlashAddrSecondCopy(), PERSISTENT_SIZE); - flashWrite(getFlashAddrSecondCopy(), (const char *) &persistentState, PERSISTENT_SIZE); - scheduleMsg(logger, "Flash programmed in %dms", currentTimeMillis() - nowMs); - bool isSuccess = result == FLASH_RETURN_SUCCESS; + persistentState.value = flashStateCrc(&persistentState); + + // Flash two copies + int result1 = eraseAndFlashCopy(getFlashAddrFirstCopy(), persistentState); + int result2 = eraseAndFlashCopy(getFlashAddrSecondCopy(), persistentState); + + // handle success/failure + bool isSuccess = (result1 == FLASH_RETURN_SUCCESS) && (result2 == FLASH_RETURN_SUCCESS); + if (isSuccess) { scheduleMsg(logger, FLASH_SUCCESS_MSG); } else { @@ -114,11 +114,11 @@ persisted_configuration_state_e flashState; static persisted_configuration_state_e doReadConfiguration(flashaddr_t address, Logging * logger) { printMsg(logger, "readFromFlash %x", address); - flashRead(address, (char *) &persistentState, PERSISTENT_SIZE); + flashRead(address, (char *) &persistentState, sizeof(persistentState)); if (!isValidCrc(&persistentState)) { return CRC_FAILED; - } else if (persistentState.version != FLASH_DATA_VERSION || persistentState.size != PERSISTENT_SIZE) { + } else if (persistentState.version != FLASH_DATA_VERSION || persistentState.size != sizeof(persistentState)) { return INCOMPATIBLE_VERSION; } else { return PC_OK;