migrated lot of stuff from Chainoiler-Project
This commit is contained in:
@@ -7,61 +7,63 @@ persistenceData_t PersistenceData;
|
||||
const uint16_t eeVersion = 1; // inc
|
||||
boolean eeAvailable = false;
|
||||
|
||||
const uint16_t persistencemarker_Adress = 0; // sizeof 4
|
||||
const uint16_t startofConfig_Adress = 16;
|
||||
const uint16_t startofPersistence_Adress = startofConfig_Adress + sizeof(ConfigData) + (sizeof(ConfigData) % 16);
|
||||
const uint16_t startofConfigData = 16;
|
||||
const uint16_t startofPersistence = 16 + sizeof(ConfigData) + (sizeof(ConfigData) % 16);
|
||||
|
||||
boolean checkEEPROMavailable();
|
||||
|
||||
void InitEEPROM()
|
||||
{
|
||||
ee.begin();
|
||||
if (!checkEEPROMavailable())
|
||||
{
|
||||
globals.systemStatus = sysStat_Error;
|
||||
MaintainDTC(DTC_NO_EEPROM_FOUND, true);
|
||||
return;
|
||||
}
|
||||
GetConfig_EEPROM();
|
||||
|
||||
if (ConfigData.EEPROM_Version != eeVersion)
|
||||
{
|
||||
FormatConfig_EEPROM();
|
||||
globals.systemStatus = sysStat_Error;
|
||||
MaintainDTC(DTC_EEPROM_VERSION_BAD, true);
|
||||
return;
|
||||
}
|
||||
|
||||
if (getPersistanceAddress() > ee.getDeviceSize())
|
||||
{
|
||||
FormatPersistence_EEPROM();
|
||||
globals.systemStatus = sysStat_Error;
|
||||
MaintainDTC(DTC_EEPROM_PDS_MARKER_INVALID, true);
|
||||
return;
|
||||
}
|
||||
GetPersistence_EEPROM();
|
||||
checkEEPROMavailable();
|
||||
}
|
||||
|
||||
void EEPROM_Process()
|
||||
{
|
||||
|
||||
switch (globals.requestEEAction)
|
||||
{
|
||||
case EE_CFG_SAVE:
|
||||
StoreConfig_EEPROM();
|
||||
globals.requestEEAction = EE_IDLE;
|
||||
Serial.println("Stored EEPROM CFG");
|
||||
break;
|
||||
case EE_CFG_LOAD:
|
||||
GetConfig_EEPROM();
|
||||
globals.requestEEAction = EE_IDLE;
|
||||
Serial.println("Loaded EEPROM CFG");
|
||||
break;
|
||||
case EE_CFG_FORMAT:
|
||||
FormatConfig_EEPROM();
|
||||
globals.requestEEAction = EE_IDLE;
|
||||
globals.systemStatus = sysStat_Shutdown;
|
||||
Serial.println("Formated EEPROM CFG");
|
||||
break;
|
||||
case EE_PDS_SAVE:
|
||||
StorePersistence_EEPROM();
|
||||
globals.requestEEAction = EE_IDLE;
|
||||
Serial.println("Stored EEPROM PDS");
|
||||
break;
|
||||
case EE_PDS_LOAD:
|
||||
GetPersistence_EEPROM();
|
||||
globals.requestEEAction = EE_IDLE;
|
||||
Serial.println("Loaded EEPROM PDS");
|
||||
break;
|
||||
case EE_PDS_FORMAT:
|
||||
FormatPersistence_EEPROM();
|
||||
globals.requestEEAction = EE_IDLE;
|
||||
Serial.println("Formated EEPROM PDS");
|
||||
break;
|
||||
case EE_FORMAT_ALL:
|
||||
FormatConfig_EEPROM();
|
||||
FormatPersistence_EEPROM();
|
||||
globals.requestEEAction = EE_IDLE;
|
||||
Serial.println("Formated EEPROM ALL");
|
||||
break;
|
||||
case EE_ALL_SAVE:
|
||||
StorePersistence_EEPROM();
|
||||
StoreConfig_EEPROM();
|
||||
globals.requestEEAction = EE_IDLE;
|
||||
Serial.println("Stored EEPROM ALL");
|
||||
break;
|
||||
case EE_IDLE:
|
||||
default:
|
||||
@@ -76,7 +78,7 @@ void StoreConfig_EEPROM()
|
||||
if (!checkEEPROMavailable())
|
||||
return;
|
||||
|
||||
ee.updateBlock(startofConfig_Adress, (uint8_t *)&ConfigData, sizeof(ConfigData));
|
||||
ee.updateBlock(startofConfigData, (uint8_t *)&ConfigData, sizeof(ConfigData));
|
||||
}
|
||||
|
||||
void GetConfig_EEPROM()
|
||||
@@ -84,34 +86,29 @@ void GetConfig_EEPROM()
|
||||
if (!checkEEPROMavailable())
|
||||
return;
|
||||
|
||||
ee.readBlock(startofConfig_Adress, (uint8_t *)&ConfigData, sizeof(ConfigData));
|
||||
ee.readBlock(startofConfigData, (uint8_t *)&ConfigData, sizeof(ConfigData));
|
||||
|
||||
uint32_t checksum = ConfigData.checksum;
|
||||
ConfigData.checksum = 0;
|
||||
|
||||
if (Checksum_EEPROM((uint8_t *)&ConfigData, sizeof(ConfigData)) != checksum)
|
||||
{
|
||||
MaintainDTC(DTC_EEPROM_CFG_BAD, true);
|
||||
FormatConfig_EEPROM();
|
||||
MaintainDTC(DTC_EEPROM_CFG_BAD, DTC_CRITICAL, true);
|
||||
}
|
||||
ConfigData.checksum = checksum;
|
||||
}
|
||||
|
||||
uint32_t getPersistanceAddress()
|
||||
{
|
||||
uint32_t eePersistenceMarker;
|
||||
ee.readBlock(persistencemarker_Adress, (uint8_t *)&eePersistenceMarker, sizeof(eePersistenceMarker));
|
||||
return eePersistenceMarker;
|
||||
}
|
||||
uint32_t ConfigSanityCheckResult = ConfigSanityCheck(false);
|
||||
|
||||
void updatePersistanceAddress(uint32_t adress)
|
||||
{
|
||||
ee.updateBlock(persistencemarker_Adress, (uint8_t *)&adress, sizeof(adress));
|
||||
if (ConfigSanityCheckResult > 0)
|
||||
{
|
||||
MaintainDTC(DTC_EEPROM_CFG_SANITY, DTC_WARN, true, ConfigSanityCheckResult);
|
||||
globals.requestEEAction = EE_CFG_SAVE;
|
||||
}
|
||||
}
|
||||
|
||||
void StorePersistence_EEPROM()
|
||||
{
|
||||
if (PersistenceData.writeCycleCounter >= EEPROM_ENDURANCE)
|
||||
if (PersistenceData.writeCycleCounter >= 0xFFF0)
|
||||
MovePersistencePage_EEPROM(false);
|
||||
else
|
||||
PersistenceData.writeCycleCounter++;
|
||||
@@ -122,7 +119,7 @@ void StorePersistence_EEPROM()
|
||||
if (!checkEEPROMavailable())
|
||||
return;
|
||||
|
||||
ee.updateBlock(getPersistanceAddress(), (uint8_t *)&PersistenceData, sizeof(PersistenceData));
|
||||
ee.updateBlock(globals.eePersistanceAdress, (uint8_t *)&PersistenceData, sizeof(PersistenceData));
|
||||
}
|
||||
|
||||
void GetPersistence_EEPROM()
|
||||
@@ -130,32 +127,42 @@ void GetPersistence_EEPROM()
|
||||
if (!checkEEPROMavailable())
|
||||
return;
|
||||
|
||||
ee.readBlock(getPersistanceAddress(), (uint8_t *)&PersistenceData, sizeof(PersistenceData));
|
||||
|
||||
uint32_t checksum = PersistenceData.checksum;
|
||||
PersistenceData.checksum = 0;
|
||||
|
||||
if (Checksum_EEPROM((uint8_t *)&PersistenceData, sizeof(PersistenceData)) != checksum)
|
||||
ee.readBlock(0, (uint8_t *)&globals.eePersistanceAdress, sizeof(globals.eePersistanceAdress));
|
||||
// if we got the StartAdress of Persistance and it's out of Range - we Reset it and store defaults
|
||||
// otherwise we Read from eeprom and check if everything is correct
|
||||
if (globals.eePersistanceAdress < startofPersistence || globals.eePersistanceAdress > ee.getDeviceSize())
|
||||
{
|
||||
MaintainDTC(DTC_EEPROM_PDS_BAD, true);
|
||||
MovePersistencePage_EEPROM(true);
|
||||
FormatPersistence_EEPROM();
|
||||
MaintainDTC(DTC_EEPROM_PDSADRESS_BAD, DTC_CRITICAL, true);
|
||||
}
|
||||
else
|
||||
{
|
||||
ee.readBlock(globals.eePersistanceAdress, (uint8_t *)&PersistenceData, sizeof(PersistenceData));
|
||||
|
||||
uint32_t checksum = PersistenceData.checksum;
|
||||
PersistenceData.checksum = 0;
|
||||
|
||||
if (Checksum_EEPROM((uint8_t *)&PersistenceData, sizeof(PersistenceData)) != checksum)
|
||||
{
|
||||
MaintainDTC(DTC_EEPROM_PDS_BAD, DTC_CRITICAL, true);
|
||||
}
|
||||
PersistenceData.checksum = checksum;
|
||||
}
|
||||
PersistenceData.checksum = checksum;
|
||||
}
|
||||
|
||||
void FormatConfig_EEPROM()
|
||||
{
|
||||
configData_t defaults;
|
||||
ConfigData = defaults;
|
||||
Serial.println("Formatting Config-Partition");
|
||||
ConfigData = ConfigData_defaults;
|
||||
ConfigData.EEPROM_Version = eeVersion;
|
||||
StoreConfig_EEPROM();
|
||||
}
|
||||
|
||||
void FormatPersistence_EEPROM()
|
||||
{
|
||||
persistenceData_t defaults;
|
||||
PersistenceData = defaults;
|
||||
updatePersistanceAddress(startofPersistence_Adress);
|
||||
Serial.println("Formatting Persistance-Partition");
|
||||
memset(&PersistenceData, 0, sizeof(PersistenceData));
|
||||
StorePersistence_EEPROM();
|
||||
}
|
||||
|
||||
@@ -164,31 +171,16 @@ void MovePersistencePage_EEPROM(boolean reset)
|
||||
if (!checkEEPROMavailable())
|
||||
return;
|
||||
|
||||
if (reset)
|
||||
{
|
||||
updatePersistanceAddress(startofPersistence_Adress);
|
||||
}
|
||||
else
|
||||
{
|
||||
uint32_t newPersistenceMarker = getPersistanceAddress() + sizeof(PersistenceData);
|
||||
globals.eePersistanceAdress = +sizeof(PersistenceData);
|
||||
PersistenceData.writeCycleCounter = 0;
|
||||
|
||||
// check if we reached the End of the EEPROM and Startover at the beginning
|
||||
if ((newPersistenceMarker + sizeof(PersistenceData)) > ee.getDeviceSize())
|
||||
{
|
||||
MaintainDTC(DTC_EEPROM_WORNOUT, true);
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
updatePersistanceAddress(newPersistenceMarker);
|
||||
PersistenceData.writeCycleCounter = 0;
|
||||
}
|
||||
// check if we reached the End of the EEPROM and Startover at the beginning
|
||||
if ((globals.eePersistanceAdress + sizeof(PersistenceData)) > ee.getDeviceSize() || reset)
|
||||
{
|
||||
globals.eePersistanceAdress = startofPersistence;
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t GetEESize()
|
||||
{
|
||||
return ee.getDeviceSize();
|
||||
ee.updateBlock(0, (uint8_t *)&globals.eePersistanceAdress, sizeof(globals.eePersistanceAdress));
|
||||
}
|
||||
|
||||
uint32_t Checksum_EEPROM(uint8_t const *data, size_t len)
|
||||
@@ -214,12 +206,6 @@ void dumpEEPROM(uint16_t memoryAddress, uint16_t length)
|
||||
{
|
||||
#define BLOCK_TO_LENGTH 16
|
||||
|
||||
if (length > ee.getDeviceSize())
|
||||
length = ee.getDeviceSize();
|
||||
|
||||
if (memoryAddress + length > ee.getDeviceSize())
|
||||
return;
|
||||
|
||||
if (!checkEEPROMavailable())
|
||||
return;
|
||||
|
||||
@@ -254,8 +240,24 @@ boolean checkEEPROMavailable()
|
||||
{
|
||||
if (!ee.isConnected())
|
||||
{
|
||||
MaintainDTC(DTC_NO_EEPROM_FOUND, true);
|
||||
MaintainDTC(DTC_NO_EEPROM_FOUND, DTC_CRITICAL, true);
|
||||
globals.systemStatus = sysStat_Error;
|
||||
return false;
|
||||
}
|
||||
MaintainDTC(DTC_NO_EEPROM_FOUND, DTC_CRITICAL, false);
|
||||
return true;
|
||||
}
|
||||
|
||||
uint32_t ConfigSanityCheck(bool autocorrect)
|
||||
{
|
||||
uint32_t setting_reset_bits = 0;
|
||||
|
||||
if ((ConfigData.batteryType != BATTERY_LIPO_2S) || (ConfigData.batteryType != BATTERY_LIPO_3S))
|
||||
{
|
||||
setting_reset_bits = setting_reset_bits | (1 << 0);
|
||||
if (autocorrect)
|
||||
ConfigData.batteryType = ConfigData_defaults.batteryType;
|
||||
}
|
||||
|
||||
return setting_reset_bits;
|
||||
}
|
Reference in New Issue
Block a user