218 lines
5.0 KiB
C++
Raw Normal View History

2022-01-09 20:51:16 +01:00
#include "config.h"
2022-01-31 09:26:10 +01:00
I2C_eeprom ee(0x50, EEPROM_SIZE_BYTES);
2022-01-09 20:51:16 +01:00
LubeConfig_t LubeConfig;
persistenceData_t PersistenceData;
uint16_t eePersistenceMarker = 0;
const uint16_t eeVersion = 1; // inc
2022-01-31 09:26:10 +01:00
boolean eeAvailable = false;
const uint16_t startofLubeConfig = 16;
const uint16_t startofPersistence = 16 + sizeof(LubeConfig) + (sizeof(LubeConfig) % 16);
2022-01-09 20:51:16 +01:00
boolean checkEEPROMavailable();
2022-01-31 09:26:10 +01:00
void InitEEPROM()
{
ee.begin();
if (!checkEEPROMavailable())
2022-03-09 23:05:17 +01:00
{
globals.systemStatus = sysStat_Error;
return;
}
GetConfig_EEPROM();
if (LubeConfig.EEPROM_Version != eeVersion)
{
FormatConfig_EEPROM();
globals.systemStatus = sysStat_Error;
MaintainDTC(DTC_EEPROM_VERSION_BAD, true);
return;
2022-03-09 23:05:17 +01:00
}
2022-01-31 09:26:10 +01:00
}
2022-02-04 21:24:15 +01:00
void EEPROM_Process()
{
switch (globals.requestEEAction)
{
case EE_CFG_SAVE:
StoreConfig_EEPROM();
globals.requestEEAction = EE_IDLE;
2022-02-04 21:24:15 +01:00
break;
case EE_CFG_LOAD:
GetConfig_EEPROM();
globals.requestEEAction = EE_IDLE;
2022-02-04 21:24:15 +01:00
break;
case EE_PDS_SAVE:
StorePersistence_EEPROM();
globals.requestEEAction = EE_IDLE;
2022-02-04 21:24:15 +01:00
break;
case EE_PDS_LOAD:
GetPersistence_EEPROM();
globals.requestEEAction = EE_IDLE;
2022-02-04 21:24:15 +01:00
break;
case EE_IDLE:
default:
globals.requestEEAction = EE_IDLE;
}
}
2022-01-09 20:51:16 +01:00
void StoreConfig_EEPROM()
{
LubeConfig.checksum = 0;
LubeConfig.checksum = Checksum_EEPROM((uint8_t *)&LubeConfig, sizeof(LubeConfig));
if (!checkEEPROMavailable())
2022-02-04 21:24:15 +01:00
return;
ee.updateBlock(startofLubeConfig, (uint8_t *)&LubeConfig, sizeof(LubeConfig));
2022-01-09 20:51:16 +01:00
}
void GetConfig_EEPROM()
{
if (!checkEEPROMavailable())
2022-02-04 21:24:15 +01:00
return;
2022-02-04 21:24:15 +01:00
ee.readBlock(startofLubeConfig, (uint8_t *)&LubeConfig, sizeof(LubeConfig));
2022-01-09 20:51:16 +01:00
uint32_t checksum = LubeConfig.checksum;
LubeConfig.checksum = 0;
2022-02-04 21:24:15 +01:00
if (Checksum_EEPROM((uint8_t *)&LubeConfig, sizeof(LubeConfig)) != checksum)
{
2022-03-09 23:05:17 +01:00
MaintainDTC(DTC_EEPROM_CFG_BAD, true);
2022-02-04 21:24:15 +01:00
FormatConfig_EEPROM();
}
LubeConfig.checksum = checksum;
}
2022-03-08 23:03:10 +01:00
uint16_t getPersistanceAddress()
{
return startofPersistence + eePersistenceMarker;
}
void StorePersistence_EEPROM()
{
if (PersistenceData.writeCycleCounter >= 0xFFF0)
2022-01-31 09:26:10 +01:00
MovePersistencePage_EEPROM(false);
2022-01-09 20:51:16 +01:00
else
PersistenceData.writeCycleCounter++;
PersistenceData.checksum = 0;
PersistenceData.checksum = Checksum_EEPROM((uint8_t *)&PersistenceData, sizeof(PersistenceData));
if (!checkEEPROMavailable())
2022-02-04 21:24:15 +01:00
return;
2022-03-08 23:03:10 +01:00
ee.updateBlock(getPersistanceAddress(), (uint8_t *)&PersistenceData, sizeof(PersistenceData));
}
void GetPersistence_EEPROM()
{
if (!checkEEPROMavailable())
2022-02-04 21:24:15 +01:00
return;
2022-01-31 09:26:10 +01:00
eePersistenceMarker = (ee.readByte(0) << 8) | ee.readByte(1);
2022-03-08 23:03:10 +01:00
ee.readBlock(getPersistanceAddress(), (uint8_t *)&PersistenceData, sizeof(PersistenceData));
uint32_t checksum = PersistenceData.checksum;
PersistenceData.checksum = 0;
2022-02-04 21:24:15 +01:00
if (Checksum_EEPROM((uint8_t *)&PersistenceData, sizeof(PersistenceData)) != checksum)
{
2022-03-09 23:05:17 +01:00
MaintainDTC(DTC_EEPROM_PDS_BAD, true);
FormatPersistence_EEPROM();
}
PersistenceData.checksum = checksum;
}
void FormatConfig_EEPROM()
{
LubeConfig_t defaults;
LubeConfig = defaults;
StoreConfig_EEPROM();
}
void FormatPersistence_EEPROM()
{
persistenceData_t defaults;
PersistenceData = defaults;
eePersistenceMarker = 0;
StorePersistence_EEPROM();
2022-01-09 20:51:16 +01:00
}
2022-01-31 09:26:10 +01:00
void MovePersistencePage_EEPROM(boolean reset)
{
eePersistenceMarker = reset ? sizeof(PersistenceData) : eePersistenceMarker + sizeof(PersistenceData);
PersistenceData.writeCycleCounter = 0;
if (!checkEEPROMavailable())
2022-02-04 21:24:15 +01:00
return;
2022-01-31 09:26:10 +01:00
ee.updateByte(0, (uint8_t)(eePersistenceMarker >> 8));
ee.updateByte(1, (uint8_t)(eePersistenceMarker & 0xFF));
}
2022-01-09 20:51:16 +01:00
uint32_t Checksum_EEPROM(uint8_t const *data, size_t len)
{
if (data == NULL)
return 0;
2022-03-08 21:23:27 +01:00
uint32_t crc, mask;
crc = 0xFFFFFFFF;
2022-01-09 20:51:16 +01:00
while (len--)
{
crc ^= *data++;
for (uint8_t k = 0; k < 8; k++)
2022-03-08 21:23:27 +01:00
{
mask = -(crc & 1);
crc = (crc >> 1) ^ (0xEDB88320 & mask);
}
2022-01-09 20:51:16 +01:00
}
2022-03-08 21:23:27 +01:00
return ~crc;
2022-01-31 09:26:10 +01:00
}
void dumpEEPROM(uint16_t memoryAddress, uint16_t length)
{
2022-02-04 21:24:15 +01:00
#define BLOCK_TO_LENGTH 16
if (!checkEEPROMavailable())
2022-02-04 21:24:15 +01:00
return;
char ascii_buf[BLOCK_TO_LENGTH + 1];
sprintf(ascii_buf, "%*s", BLOCK_TO_LENGTH, "ASCII");
2022-02-10 22:31:11 +01:00
Serial.print(PSTR("\nAddress "));
2022-01-31 09:26:10 +01:00
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++)
{
2022-02-04 21:24:15 +01:00
int blockpoint = memoryAddress % BLOCK_TO_LENGTH;
if (blockpoint == 0)
{
ascii_buf[BLOCK_TO_LENGTH] = 0;
Serial.printf(" %s", ascii_buf);
2022-01-31 09:26:10 +01:00
Serial.printf("\n0x%05X:", memoryAddress);
2022-02-04 21:24:15 +01:00
}
ascii_buf[blockpoint] = ee.readByte(memoryAddress);
Serial.printf(" %02X", ascii_buf[blockpoint]);
if (ascii_buf[blockpoint] < 0x20 || ascii_buf[blockpoint] > 0x7E)
ascii_buf[blockpoint] = '.';
2022-01-31 09:26:10 +01:00
memoryAddress++;
}
Serial.println();
}
boolean checkEEPROMavailable()
{
if (!ee.isConnected())
{
MaintainDTC(DTC_NO_EEPROM_FOUND, true);
return false;
}
return true;
2022-01-09 20:51:16 +01:00
}