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
|
|
|
|
2022-02-04 22:26:26 +01:00
|
|
|
const uint16_t startofLubeConfig = 16;
|
|
|
|
const uint16_t startofPersistence = 16 + sizeof(LubeConfig) + (sizeof(LubeConfig) % 16);
|
2022-01-09 20:51:16 +01:00
|
|
|
|
2022-01-31 09:26:10 +01:00
|
|
|
#if PCB_REVISION >= 12
|
|
|
|
void InitEEPROM()
|
|
|
|
{
|
|
|
|
ee.begin();
|
2022-02-04 21:24:15 +01:00
|
|
|
if (!ee.isConnected())
|
2022-02-10 22:31:11 +01:00
|
|
|
Serial.println(PSTR("ERROR: Can't find eeprom..."));
|
2022-01-31 09:26:10 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2022-02-04 21:24:15 +01:00
|
|
|
void EEPROM_Process()
|
|
|
|
{
|
|
|
|
|
|
|
|
switch (globals.requestEEAction)
|
|
|
|
{
|
|
|
|
case EE_CFG_SAVE:
|
|
|
|
StoreConfig_EEPROM();
|
2022-03-08 22:32:01 +01:00
|
|
|
Serial.println("EE_CFG_SAVE");
|
|
|
|
globals.requestEEAction = EE_IDLE;
|
2022-02-04 21:24:15 +01:00
|
|
|
break;
|
|
|
|
case EE_CFG_LOAD:
|
|
|
|
GetConfig_EEPROM();
|
2022-03-08 22:32:01 +01:00
|
|
|
Serial.println("EE_CFG_LOAD");
|
|
|
|
globals.requestEEAction = EE_IDLE;
|
2022-02-04 21:24:15 +01:00
|
|
|
break;
|
|
|
|
case EE_PDS_SAVE:
|
|
|
|
StorePersistence_EEPROM();
|
2022-03-08 22:32:01 +01:00
|
|
|
Serial.println("EE_PDS_SAVE");
|
|
|
|
globals.requestEEAction = EE_IDLE;
|
2022-02-04 21:24:15 +01:00
|
|
|
break;
|
|
|
|
case EE_PDS_LOAD:
|
|
|
|
GetPersistence_EEPROM();
|
2022-03-08 22:32:01 +01:00
|
|
|
Serial.println("EE_PDS_LOAD");
|
|
|
|
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));
|
2022-01-31 09:26:10 +01:00
|
|
|
#if PCB_REVISION >= 12
|
2022-02-04 21:24:15 +01:00
|
|
|
if (!ee.isConnected())
|
|
|
|
return;
|
2022-02-04 22:26:26 +01:00
|
|
|
ee.updateBlock(startofLubeConfig, (uint8_t *)&LubeConfig, sizeof(LubeConfig));
|
2022-01-31 09:26:10 +01:00
|
|
|
#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
|
2022-02-04 21:24:15 +01:00
|
|
|
if (!ee.isConnected())
|
|
|
|
return;
|
|
|
|
ee.readBlock(startofLubeConfig, (uint8_t *)&LubeConfig, sizeof(LubeConfig));
|
2022-01-31 09:26:10 +01:00
|
|
|
#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;
|
|
|
|
|
2022-02-04 21:24:15 +01:00
|
|
|
if (Checksum_EEPROM((uint8_t *)&LubeConfig, sizeof(LubeConfig)) != checksum)
|
|
|
|
{
|
2022-02-10 22:31:11 +01:00
|
|
|
Serial.printf(PSTR("CFG EEPROM Checksum BAD\n"));
|
2022-02-04 21:24:15 +01:00
|
|
|
FormatConfig_EEPROM();
|
|
|
|
}
|
2022-01-19 22:23:36 +01:00
|
|
|
LubeConfig.checksum = checksum;
|
|
|
|
}
|
|
|
|
|
|
|
|
void StorePersistence_EEPROM()
|
|
|
|
{
|
2022-02-04 22:26:26 +01:00
|
|
|
if (PersistenceData.writeCycleCounter >= 0xFFF0)
|
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
|
2022-02-04 21:24:15 +01:00
|
|
|
if (!ee.isConnected())
|
|
|
|
return;
|
|
|
|
ee.updateBlock(PersistenceDataAddress, (uint8_t *)&PersistenceData, sizeof(PersistenceData));
|
2022-01-31 09:26:10 +01:00
|
|
|
#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
|
2022-02-04 21:24:15 +01:00
|
|
|
if (!ee.isConnected())
|
|
|
|
return;
|
|
|
|
eePersistenceMarker = (ee.readByte(0) << 8) | ee.readByte(1);
|
2022-01-31 09:26:10 +01:00
|
|
|
#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
|
|
|
#if PCB_REVISION >= 12
|
2022-02-04 21:24:15 +01:00
|
|
|
ee.readBlock(PersistenceDataAddress, (uint8_t *)&PersistenceData, sizeof(PersistenceData));
|
2022-01-31 09:26:10 +01:00
|
|
|
#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;
|
|
|
|
|
2022-02-04 21:24:15 +01:00
|
|
|
if (Checksum_EEPROM((uint8_t *)&PersistenceData, sizeof(PersistenceData)) != checksum)
|
2022-01-19 22:23:36 +01:00
|
|
|
{
|
2022-02-10 22:31:11 +01:00
|
|
|
Serial.printf(PSTR("Persistance EEPROM Checksum BAD\n"));
|
2022-01-19 22:23:36 +01:00
|
|
|
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
|
2022-02-04 21:24:15 +01:00
|
|
|
if (!ee.isConnected())
|
|
|
|
return;
|
2022-01-31 09:26:10 +01:00
|
|
|
ee.updateByte(0, (uint8_t)(eePersistenceMarker >> 8));
|
|
|
|
ee.updateByte(1, (uint8_t)(eePersistenceMarker & 0xFF));
|
|
|
|
#else
|
|
|
|
EEPROM.begin(512);
|
|
|
|
EEPROM.put(0, eePersistenceMarker);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
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 (!ee.isConnected())
|
|
|
|
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();
|
2022-01-09 20:51:16 +01:00
|
|
|
}
|