217 lines
5.2 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
#if PCB_REVISION >= 12
I2C_eeprom ee(0x50, EEPROM_SIZE_BYTES);
#endif
2022-01-09 20:51:16 +01:00
LubeConfig_t LubeConfig;
persistenceData_t PersistenceData;
uint16_t eePersistenceMarker = 0;
uint16_t eeVersion = 0; // inc
2022-01-31 09:26:10 +01:00
boolean eeAvailable = false;
const uint16_t startofLubeConfig = sizeof(eePersistenceMarker);
2022-02-04 21:24:15 +01:00
const uint16_t startofPersistence = sizeof(LubeConfig) + sizeof(eePersistenceMarker);
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-01-31 09:26:10 +01:00
Serial.println("ERROR: Can't find eeprom...");
}
#endif
2022-02-04 21:24:15 +01:00
void EEPROM_Process()
{
switch (globals.requestEEAction)
{
case EE_CFG_SAVE:
StoreConfig_EEPROM();
break;
case EE_CFG_LOAD:
GetConfig_EEPROM();
break;
case EE_PDS_SAVE:
StorePersistence_EEPROM();
break;
case EE_PDS_LOAD:
GetPersistence_EEPROM();
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;
ee.writeBlock(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);
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);
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)
{
Serial.printf("CFG EEPROM Checksum BAD\n");
2022-02-04 21:24:15 +01:00
FormatConfig_EEPROM();
}
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
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
EEPROM.put(PersistenceDataAddress, PersistenceData);
EEPROM.commit();
EEPROM.end();
2022-01-31 09:26:10 +01:00
#endif
}
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
EEPROM.begin(512);
EEPROM.get(0, eePersistenceMarker);
2022-01-31 09:26:10 +01:00
#endif
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
EEPROM.get(PersistenceDataAddress, PersistenceData);
EEPROM.end();
2022-01-31 09:26:10 +01:00
#endif
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)
{
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
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;
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)
{
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");
Serial.print("\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
}