Restructured Repo before adding Hardware-Files
This commit is contained in:
261
Software/src/config.cpp
Normal file
261
Software/src/config.cpp
Normal file
@@ -0,0 +1,261 @@
|
||||
#include "config.h"
|
||||
|
||||
I2C_eeprom ee(0x50, EEPROM_SIZE_BYTES);
|
||||
|
||||
configData_t ConfigData;
|
||||
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);
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
void EEPROM_Process()
|
||||
{
|
||||
|
||||
switch (globals.requestEEAction)
|
||||
{
|
||||
case EE_CFG_SAVE:
|
||||
StoreConfig_EEPROM();
|
||||
globals.requestEEAction = EE_IDLE;
|
||||
break;
|
||||
case EE_CFG_LOAD:
|
||||
GetConfig_EEPROM();
|
||||
globals.requestEEAction = EE_IDLE;
|
||||
break;
|
||||
case EE_PDS_SAVE:
|
||||
StorePersistence_EEPROM();
|
||||
globals.requestEEAction = EE_IDLE;
|
||||
break;
|
||||
case EE_PDS_LOAD:
|
||||
GetPersistence_EEPROM();
|
||||
globals.requestEEAction = EE_IDLE;
|
||||
break;
|
||||
case EE_IDLE:
|
||||
default:
|
||||
globals.requestEEAction = EE_IDLE;
|
||||
}
|
||||
}
|
||||
|
||||
void StoreConfig_EEPROM()
|
||||
{
|
||||
ConfigData.checksum = 0;
|
||||
ConfigData.checksum = Checksum_EEPROM((uint8_t *)&ConfigData, sizeof(ConfigData));
|
||||
if (!checkEEPROMavailable())
|
||||
return;
|
||||
|
||||
ee.updateBlock(startofConfig_Adress, (uint8_t *)&ConfigData, sizeof(ConfigData));
|
||||
}
|
||||
|
||||
void GetConfig_EEPROM()
|
||||
{
|
||||
if (!checkEEPROMavailable())
|
||||
return;
|
||||
|
||||
ee.readBlock(startofConfig_Adress, (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();
|
||||
}
|
||||
ConfigData.checksum = checksum;
|
||||
}
|
||||
|
||||
uint32_t getPersistanceAddress()
|
||||
{
|
||||
uint32_t eePersistenceMarker;
|
||||
ee.readBlock(persistencemarker_Adress, (uint8_t *)&eePersistenceMarker, sizeof(eePersistenceMarker));
|
||||
return eePersistenceMarker;
|
||||
}
|
||||
|
||||
void updatePersistanceAddress(uint32_t adress)
|
||||
{
|
||||
ee.updateBlock(persistencemarker_Adress, (uint8_t *)&adress, sizeof(adress));
|
||||
}
|
||||
|
||||
void StorePersistence_EEPROM()
|
||||
{
|
||||
if (PersistenceData.writeCycleCounter >= EEPROM_ENDURANCE)
|
||||
MovePersistencePage_EEPROM(false);
|
||||
else
|
||||
PersistenceData.writeCycleCounter++;
|
||||
|
||||
PersistenceData.checksum = 0;
|
||||
PersistenceData.checksum = Checksum_EEPROM((uint8_t *)&PersistenceData, sizeof(PersistenceData));
|
||||
|
||||
if (!checkEEPROMavailable())
|
||||
return;
|
||||
|
||||
ee.updateBlock(getPersistanceAddress(), (uint8_t *)&PersistenceData, sizeof(PersistenceData));
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
MaintainDTC(DTC_EEPROM_PDS_BAD, true);
|
||||
FormatPersistence_EEPROM();
|
||||
}
|
||||
PersistenceData.checksum = checksum;
|
||||
}
|
||||
|
||||
void FormatConfig_EEPROM()
|
||||
{
|
||||
configData_t defaults;
|
||||
ConfigData = defaults;
|
||||
ConfigData.EEPROM_Version = eeVersion;
|
||||
StoreConfig_EEPROM();
|
||||
}
|
||||
|
||||
void FormatPersistence_EEPROM()
|
||||
{
|
||||
persistenceData_t defaults;
|
||||
PersistenceData = defaults;
|
||||
updatePersistanceAddress(startofPersistence_Adress);
|
||||
StorePersistence_EEPROM();
|
||||
}
|
||||
|
||||
void MovePersistencePage_EEPROM(boolean reset)
|
||||
{
|
||||
if (!checkEEPROMavailable())
|
||||
return;
|
||||
|
||||
if (reset)
|
||||
{
|
||||
updatePersistanceAddress(startofPersistence_Adress);
|
||||
}
|
||||
else
|
||||
{
|
||||
uint32_t newPersistenceMarker = getPersistanceAddress() + sizeof(PersistenceData);
|
||||
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t GetEESize()
|
||||
{
|
||||
return ee.getDeviceSize();
|
||||
}
|
||||
|
||||
uint32_t Checksum_EEPROM(uint8_t const *data, size_t len)
|
||||
{
|
||||
if (data == NULL)
|
||||
return 0;
|
||||
uint32_t crc, mask;
|
||||
crc = 0xFFFFFFFF;
|
||||
|
||||
while (len--)
|
||||
{
|
||||
crc ^= *data++;
|
||||
for (uint8_t k = 0; k < 8; k++)
|
||||
{
|
||||
mask = -(crc & 1);
|
||||
crc = (crc >> 1) ^ (0xEDB88320 & mask);
|
||||
}
|
||||
}
|
||||
return ~crc;
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
char ascii_buf[BLOCK_TO_LENGTH + 1];
|
||||
sprintf(ascii_buf, "%*s", BLOCK_TO_LENGTH, "ASCII");
|
||||
Serial.print(PSTR("\nAddress "));
|
||||
for (int x = 0; x < BLOCK_TO_LENGTH; x++)
|
||||
Serial.printf("%3d", x);
|
||||
|
||||
memoryAddress = memoryAddress / BLOCK_TO_LENGTH * BLOCK_TO_LENGTH;
|
||||
length = (length + BLOCK_TO_LENGTH - 1) / BLOCK_TO_LENGTH * BLOCK_TO_LENGTH;
|
||||
|
||||
for (unsigned int i = 0; i < length; i++)
|
||||
{
|
||||
int blockpoint = memoryAddress % BLOCK_TO_LENGTH;
|
||||
if (blockpoint == 0)
|
||||
{
|
||||
ascii_buf[BLOCK_TO_LENGTH] = 0;
|
||||
Serial.printf(" %s", ascii_buf);
|
||||
Serial.printf("\n0x%05X:", memoryAddress);
|
||||
}
|
||||
ascii_buf[blockpoint] = ee.readByte(memoryAddress);
|
||||
Serial.printf(" %02X", ascii_buf[blockpoint]);
|
||||
if (ascii_buf[blockpoint] < 0x20 || ascii_buf[blockpoint] > 0x7E)
|
||||
ascii_buf[blockpoint] = '.';
|
||||
memoryAddress++;
|
||||
}
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
boolean checkEEPROMavailable()
|
||||
{
|
||||
if (!ee.isConnected())
|
||||
{
|
||||
MaintainDTC(DTC_NO_EEPROM_FOUND, true);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
Reference in New Issue
Block a user