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 bf6082b828
commit 3c7ae8ca17
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;
/**
* 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 <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) {
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;