diff --git a/firmware/controllers/flash_main.cpp b/firmware/controllers/flash_main.cpp index 251140a50d..b2852f66f5 100644 --- a/firmware/controllers/flash_main.cpp +++ b/firmware/controllers/flash_main.cpp @@ -100,22 +100,17 @@ static void doResetConfiguration(void) { resetConfigurationExt(logger, engineConfiguration->engineType PASS_ENGINE_PARAMETER); } -typedef enum { - OK = 0, - CRC_FAILED = 1, - INCOMPATIBLE_VERSION = 2, - RESET_REQUESTED = 3 -} persisted_configuration_state_e; - persisted_configuration_state_e flashState; -void readFromFlash(void) { - efiAssertVoid(getRemainingStack(chThdSelf()) > 256, "read f"); - printMsg(logger, "readFromFlash()"); +/** + * this method could and should be executed before we have any + * connectivity so no console output here + */ +persisted_configuration_state_e readConfiguration(void) { + efiAssert(getRemainingStack(chThdSelf()) > 256, "read f", PC_ERROR); flashRead(FLASH_ADDR, (char *) &persistentState, PERSISTENT_SIZE); persisted_configuration_state_e result; - if (!isValidCrc(&persistentState)) { result = CRC_FAILED; resetConfigurationExt(logger, DEFAULT_ENGINE_TYPE PASS_ENGINE_PARAMETER); @@ -131,6 +126,15 @@ void readFromFlash(void) { } // we can only change the state after the CRC check engineConfiguration->firmwareVersion = getRusEfiVersion(); + return result; +} + +void readFromFlash(void) { + printMsg(logger, "readFromFlash()"); + readConfiguration(); + + persisted_configuration_state_e result = readConfiguration(); + if (result == CRC_FAILED) { printMsg(logger, "Need to reset flash to default due to CRC"); diff --git a/firmware/controllers/flash_main.h b/firmware/controllers/flash_main.h index d8d79fed1b..b6472b0c5a 100644 --- a/firmware/controllers/flash_main.h +++ b/firmware/controllers/flash_main.h @@ -13,6 +13,15 @@ #define FLASH_DATA_VERSION 9200 +typedef enum { + OK = 0, + CRC_FAILED = 1, + INCOMPATIBLE_VERSION = 2, + RESET_REQUESTED = 3, + PC_ERROR = 4 +} persisted_configuration_state_e; + +persisted_configuration_state_e readConfiguration(void); void readFromFlash(void); void initFlash(Logging *sharedLogger); diff --git a/firmware/rusefi.cpp b/firmware/rusefi.cpp index c5cd78c3c6..e093210c13 100644 --- a/firmware/rusefi.cpp +++ b/firmware/rusefi.cpp @@ -166,6 +166,12 @@ void applyNewConfiguration(void) { void runRusEfi(void) { efiAssertVoid(getRemainingStack(chThdSelf()) > 512, "init s"); + /** + * First thing is reading configuration from flash memory. + * In order to have complete flexibility configuration has to go before anything else. + */ + readConfiguration(); + msObjectInit(&firmwareErrorMessageStream, errorMessageBuffer, sizeof(errorMessageBuffer), 0); #if EFI_ENGINE_CONTROL || defined(__DOXYGEN__)