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
This commit is contained in:
Matthew Kennedy 2020-03-07 07:23:30 -08:00 committed by GitHub
parent 9316272f90
commit e32be1799d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 22 additions and 22 deletions

View File

@ -35,13 +35,6 @@ extern persistent_config_container_s persistentState;
extern engine_configuration_s *engineConfiguration; 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/ * https://sourceforge.net/p/rusefi/tickets/335/
* *
@ -72,22 +65,29 @@ void writeToFlashIfPending() {
writeToFlashNow(); writeToFlashNow();
} }
// Erase and write a copy of the configuration at the specified address
template <typename TStorage>
int eraseAndFlashCopy(flashaddr_t storageAddress, const TStorage& data)
{
flashErase(storageAddress, sizeof(TStorage));
return flashWrite(storageAddress, reinterpret_cast<const char*>(&data), sizeof(TStorage));
}
void writeToFlashNow(void) { void writeToFlashNow(void) {
scheduleMsg(logger, " !!!!!!!!!!!!!!!!!!!! BE SURE NOT WRITE WITH IGNITION ON !!!!!!!!!!!!!!!!!!!!"); 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; persistentState.version = FLASH_DATA_VERSION;
scheduleMsg(logger, "flash compatible with %d", persistentState.version); persistentState.value = flashStateCrc(&persistentState);
crc_t crcResult = flashStateCrc(&persistentState);
persistentState.value = crcResult; // Flash two copies
scheduleMsg(logger, "Reseting flash: size=%d", PERSISTENT_SIZE); int result1 = eraseAndFlashCopy(getFlashAddrFirstCopy(), persistentState);
flashErase(getFlashAddrFirstCopy(), PERSISTENT_SIZE); int result2 = eraseAndFlashCopy(getFlashAddrSecondCopy(), persistentState);
scheduleMsg(logger, "Flashing with CRC=%d", crcResult);
efitimems_t nowMs = currentTimeMillis(); // handle success/failure
int result = flashWrite(getFlashAddrFirstCopy(), (const char *) &persistentState, PERSISTENT_SIZE); bool isSuccess = (result1 == FLASH_RETURN_SUCCESS) && (result2 == FLASH_RETURN_SUCCESS);
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;
if (isSuccess) { if (isSuccess) {
scheduleMsg(logger, FLASH_SUCCESS_MSG); scheduleMsg(logger, FLASH_SUCCESS_MSG);
} else { } else {
@ -114,11 +114,11 @@ persisted_configuration_state_e flashState;
static persisted_configuration_state_e doReadConfiguration(flashaddr_t address, Logging * logger) { static persisted_configuration_state_e doReadConfiguration(flashaddr_t address, Logging * logger) {
printMsg(logger, "readFromFlash %x", address); printMsg(logger, "readFromFlash %x", address);
flashRead(address, (char *) &persistentState, PERSISTENT_SIZE); flashRead(address, (char *) &persistentState, sizeof(persistentState));
if (!isValidCrc(&persistentState)) { if (!isValidCrc(&persistentState)) {
return CRC_FAILED; 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; return INCOMPATIBLE_VERSION;
} else { } else {
return PC_OK; return PC_OK;