2022-01-09 20:51:16 +01:00
|
|
|
#include "config.h"
|
|
|
|
|
2022-01-31 09:26:10 +01:00
|
|
|
#if PCB_REVISION >= 12
|
|
|
|
I2C_eeprom ee(0x50, EEPROM_SIZE_BYTES);
|
|
|
|
#endif
|
|
|
|
|
2022-01-09 20:51:16 +01:00
|
|
|
LubeConfig_t LubeConfig;
|
2022-01-19 22:23:36 +01:00
|
|
|
persistenceData_t PersistenceData;
|
|
|
|
uint16_t eePersistenceMarker = 0;
|
|
|
|
uint16_t eeVersion = 0; // inc
|
2022-01-31 09:26:10 +01:00
|
|
|
boolean eeAvailable = false;
|
2022-01-19 22:23:36 +01:00
|
|
|
|
|
|
|
const uint16_t startofLubeConfig = sizeof(eePersistenceMarker);
|
|
|
|
const uint16_t startofPersistence = sizeof(LubeConfig);
|
2022-01-09 20:51:16 +01:00
|
|
|
|
2022-01-31 09:26:10 +01:00
|
|
|
#if PCB_REVISION >= 12
|
|
|
|
void InitEEPROM()
|
|
|
|
{
|
|
|
|
ee.begin();
|
|
|
|
if (ee.isConnected())
|
|
|
|
{
|
|
|
|
eeAvailable = true;
|
|
|
|
Serial.println("EEPROM Initialized...");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Serial.println("ERROR: Can't find eeprom...");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2022-01-09 20:51:16 +01:00
|
|
|
void StoreConfig_EEPROM()
|
|
|
|
{
|
|
|
|
LubeConfig.checksum = 0;
|
|
|
|
LubeConfig.checksum = Checksum_EEPROM((uint8_t *)&LubeConfig, sizeof(LubeConfig));
|
|
|
|
|
2022-01-31 09:26:10 +01:00
|
|
|
#if PCB_REVISION >= 12
|
|
|
|
if (eeAvailable)
|
|
|
|
{
|
|
|
|
ee.updateBlock(startofLubeConfig, (uint8_t *)&LubeConfig, sizeof(LubeConfig));
|
|
|
|
}
|
|
|
|
#else
|
2022-01-09 20:51:16 +01:00
|
|
|
EEPROM.begin(512);
|
2022-01-19 22:23:36 +01:00
|
|
|
EEPROM.put(startofLubeConfig, LubeConfig);
|
2022-01-09 20:51:16 +01:00
|
|
|
EEPROM.commit();
|
|
|
|
EEPROM.end();
|
2022-01-31 09:26:10 +01:00
|
|
|
#endif
|
2022-01-09 20:51:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void GetConfig_EEPROM()
|
|
|
|
{
|
2022-01-31 09:26:10 +01:00
|
|
|
#if PCB_REVISION >= 12
|
|
|
|
if (eeAvailable)
|
|
|
|
{
|
|
|
|
ee.readBlock(startofLubeConfig, (uint8_t *)&LubeConfig, sizeof(LubeConfig));
|
|
|
|
}
|
|
|
|
#else
|
2022-01-09 20:51:16 +01:00
|
|
|
EEPROM.begin(512);
|
2022-01-19 22:23:36 +01:00
|
|
|
EEPROM.get(startofLubeConfig, LubeConfig);
|
2022-01-09 20:51:16 +01:00
|
|
|
EEPROM.end();
|
2022-01-31 09:26:10 +01:00
|
|
|
#endif
|
2022-01-09 20:51:16 +01:00
|
|
|
|
|
|
|
uint32_t checksum = LubeConfig.checksum;
|
|
|
|
LubeConfig.checksum = 0;
|
|
|
|
|
|
|
|
if (Checksum_EEPROM((uint8_t *)&LubeConfig, sizeof(LubeConfig)) == checksum)
|
2022-01-19 22:23:36 +01:00
|
|
|
Serial.printf("CFG EEPROM Checksum OK\n");
|
|
|
|
else
|
|
|
|
Serial.printf("CFG EEPROM Checksum BAD\n");
|
|
|
|
LubeConfig.checksum = checksum;
|
|
|
|
}
|
|
|
|
|
|
|
|
void StorePersistence_EEPROM()
|
|
|
|
{
|
|
|
|
if (PersistenceData.writeCycleCounter == 0xFFFF)
|
2022-01-31 09:26:10 +01:00
|
|
|
MovePersistencePage_EEPROM(false);
|
2022-01-09 20:51:16 +01:00
|
|
|
else
|
2022-01-19 22:23:36 +01:00
|
|
|
PersistenceData.writeCycleCounter++;
|
|
|
|
|
|
|
|
uint16_t PersistenceDataAddress = startofPersistence + eePersistenceMarker;
|
|
|
|
PersistenceData.checksum = 0;
|
|
|
|
PersistenceData.checksum = Checksum_EEPROM((uint8_t *)&PersistenceData, sizeof(PersistenceData));
|
|
|
|
|
2022-01-31 09:26:10 +01:00
|
|
|
#if PCB_REVISION >= 12
|
|
|
|
if (eeAvailable)
|
|
|
|
{
|
|
|
|
ee.updateBlock(PersistenceDataAddress, (uint8_t *)&PersistenceData, sizeof(PersistenceData));
|
|
|
|
}
|
|
|
|
#else
|
2022-01-19 22:23:36 +01:00
|
|
|
EEPROM.put(PersistenceDataAddress, PersistenceData);
|
|
|
|
EEPROM.commit();
|
|
|
|
EEPROM.end();
|
2022-01-31 09:26:10 +01:00
|
|
|
#endif
|
2022-01-19 22:23:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void GetPersistence_EEPROM()
|
|
|
|
{
|
2022-01-31 09:26:10 +01:00
|
|
|
#if PCB_REVISION >= 12
|
|
|
|
if (eeAvailable)
|
|
|
|
{
|
|
|
|
eePersistenceMarker = (ee.readByte(0) << 8) | ee.readByte(1);
|
|
|
|
Serial.printf("get EE eePersistenceMarker: 0x%04X\n", eePersistenceMarker);
|
|
|
|
}
|
|
|
|
#else
|
2022-01-19 22:23:36 +01:00
|
|
|
EEPROM.begin(512);
|
|
|
|
EEPROM.get(0, eePersistenceMarker);
|
2022-01-31 09:26:10 +01:00
|
|
|
#endif
|
|
|
|
|
2022-01-19 22:23:36 +01:00
|
|
|
uint16_t PersistenceDataAddress = startofPersistence + eePersistenceMarker;
|
2022-01-31 09:26:10 +01:00
|
|
|
Serial.printf("get EE PersistenceDataAddress: 0x%04X\n", PersistenceDataAddress);
|
2022-01-19 22:23:36 +01:00
|
|
|
|
2022-01-31 09:26:10 +01:00
|
|
|
#if PCB_REVISION >= 12
|
|
|
|
if (eeAvailable)
|
|
|
|
{
|
|
|
|
ee.readBlock(PersistenceDataAddress, (uint8_t *)&PersistenceData, sizeof(PersistenceData));
|
|
|
|
}
|
|
|
|
#else
|
2022-01-19 22:23:36 +01:00
|
|
|
EEPROM.get(PersistenceDataAddress, PersistenceData);
|
|
|
|
EEPROM.end();
|
2022-01-31 09:26:10 +01:00
|
|
|
#endif
|
2022-01-19 22:23:36 +01:00
|
|
|
|
|
|
|
uint32_t checksum = PersistenceData.checksum;
|
|
|
|
PersistenceData.checksum = 0;
|
|
|
|
|
|
|
|
if (Checksum_EEPROM((uint8_t *)&PersistenceData, sizeof(PersistenceData)) == checksum)
|
|
|
|
{
|
|
|
|
Serial.printf("Persistance EEPROM Checksum OK\n");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Serial.printf("Persistance EEPROM Checksum BAD\n");
|
|
|
|
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 PCB_REVISION >= 12
|
|
|
|
ee.updateByte(0, (uint8_t)(eePersistenceMarker >> 8));
|
|
|
|
ee.updateByte(1, (uint8_t)(eePersistenceMarker & 0xFF));
|
|
|
|
#else
|
|
|
|
EEPROM.begin(512);
|
|
|
|
EEPROM.put(0, eePersistenceMarker);
|
|
|
|
#endif
|
|
|
|
Serial.printf("Moving PDS-Page\n");
|
|
|
|
Serial.printf("PersistenceData.writeCycleCounter: 0x%04X\n", PersistenceData.writeCycleCounter);
|
|
|
|
Serial.printf("eePersistenceMarker: 0x%04X\n", eePersistenceMarker);
|
|
|
|
Serial.printf("sizeof(PersistenceData): 0x%04X\n", sizeof(PersistenceData));
|
|
|
|
}
|
|
|
|
|
2022-01-09 20:51:16 +01:00
|
|
|
uint32_t Checksum_EEPROM(uint8_t const *data, size_t len)
|
|
|
|
{
|
|
|
|
if (data == NULL)
|
|
|
|
return 0;
|
|
|
|
uint32_t crc = 0;
|
|
|
|
while (len--)
|
|
|
|
{
|
|
|
|
crc ^= *data++;
|
|
|
|
for (uint8_t k = 0; k < 8; k++)
|
|
|
|
crc = crc & 1 ? (crc >> 1) ^ 0xb2 : crc >> 1;
|
|
|
|
}
|
|
|
|
return crc ^ 0xff;
|
2022-01-31 09:26:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void dumpEEPROM(uint16_t memoryAddress, uint16_t length)
|
|
|
|
{
|
|
|
|
const int BLOCK_TO_LENGTH = 16;
|
|
|
|
Serial.print("\nAddr. ");
|
|
|
|
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++)
|
|
|
|
{
|
|
|
|
if (memoryAddress % BLOCK_TO_LENGTH == 0)
|
|
|
|
Serial.printf("\n0x%05X:", memoryAddress);
|
|
|
|
Serial.printf(" %02X", ee.readByte(memoryAddress));
|
|
|
|
memoryAddress++;
|
|
|
|
}
|
|
|
|
Serial.println();
|
2022-01-09 20:51:16 +01:00
|
|
|
}
|