Reworked EEPROM-Handling by Flag
This commit is contained in:
		| @@ -11,34 +11,48 @@ uint16_t eeVersion = 0; // inc | |||||||
| boolean eeAvailable = false; | boolean eeAvailable = false; | ||||||
|  |  | ||||||
| const uint16_t startofLubeConfig = sizeof(eePersistenceMarker); | const uint16_t startofLubeConfig = sizeof(eePersistenceMarker); | ||||||
| const uint16_t startofPersistence = sizeof(LubeConfig); | const uint16_t startofPersistence = sizeof(LubeConfig) + sizeof(eePersistenceMarker); | ||||||
|  |  | ||||||
| #if PCB_REVISION >= 12 | #if PCB_REVISION >= 12 | ||||||
| void InitEEPROM() | void InitEEPROM() | ||||||
| { | { | ||||||
|   ee.begin(); |   ee.begin(); | ||||||
|   if (ee.isConnected()) |   if (!ee.isConnected()) | ||||||
|   { |  | ||||||
|     eeAvailable = true; |  | ||||||
|     Serial.println("EEPROM Initialized..."); |  | ||||||
|   } |  | ||||||
|   else |  | ||||||
|   { |  | ||||||
|     Serial.println("ERROR: Can't find eeprom..."); |     Serial.println("ERROR: Can't find eeprom..."); | ||||||
| } | } | ||||||
| } |  | ||||||
| #endif | #endif | ||||||
|  |  | ||||||
|  | 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; | ||||||
|  |   } | ||||||
|  | } | ||||||
|  |  | ||||||
| void StoreConfig_EEPROM() | void StoreConfig_EEPROM() | ||||||
| { | { | ||||||
|   LubeConfig.checksum = 0; |   LubeConfig.checksum = 0; | ||||||
|   LubeConfig.checksum = Checksum_EEPROM((uint8_t *)&LubeConfig, sizeof(LubeConfig)); |   LubeConfig.checksum = Checksum_EEPROM((uint8_t *)&LubeConfig, sizeof(LubeConfig)); | ||||||
|  |  | ||||||
| #if PCB_REVISION >= 12 | #if PCB_REVISION >= 12 | ||||||
|   if (eeAvailable) |   if (!ee.isConnected()) | ||||||
|   { |     return; | ||||||
|     ee.updateBlock(startofLubeConfig, (uint8_t *)&LubeConfig, sizeof(LubeConfig)); |   ee.writeBlock(startofLubeConfig, (uint8_t *)&LubeConfig, sizeof(LubeConfig)); | ||||||
|   } |  | ||||||
| #else | #else | ||||||
|   EEPROM.begin(512); |   EEPROM.begin(512); | ||||||
|   EEPROM.put(startofLubeConfig, LubeConfig); |   EEPROM.put(startofLubeConfig, LubeConfig); | ||||||
| @@ -50,10 +64,9 @@ void StoreConfig_EEPROM() | |||||||
| void GetConfig_EEPROM() | void GetConfig_EEPROM() | ||||||
| { | { | ||||||
| #if PCB_REVISION >= 12 | #if PCB_REVISION >= 12 | ||||||
|   if (eeAvailable) |   if (!ee.isConnected()) | ||||||
|   { |     return; | ||||||
|   ee.readBlock(startofLubeConfig, (uint8_t *)&LubeConfig, sizeof(LubeConfig)); |   ee.readBlock(startofLubeConfig, (uint8_t *)&LubeConfig, sizeof(LubeConfig)); | ||||||
|   } |  | ||||||
| #else | #else | ||||||
|   EEPROM.begin(512); |   EEPROM.begin(512); | ||||||
|   EEPROM.get(startofLubeConfig, LubeConfig); |   EEPROM.get(startofLubeConfig, LubeConfig); | ||||||
| @@ -63,10 +76,11 @@ void GetConfig_EEPROM() | |||||||
|   uint32_t checksum = LubeConfig.checksum; |   uint32_t checksum = LubeConfig.checksum; | ||||||
|   LubeConfig.checksum = 0; |   LubeConfig.checksum = 0; | ||||||
|  |  | ||||||
|   if (Checksum_EEPROM((uint8_t *)&LubeConfig, sizeof(LubeConfig)) == checksum) |   if (Checksum_EEPROM((uint8_t *)&LubeConfig, sizeof(LubeConfig)) != checksum) | ||||||
|     Serial.printf("CFG EEPROM Checksum OK\n"); |   { | ||||||
|   else |  | ||||||
|     Serial.printf("CFG EEPROM Checksum BAD\n"); |     Serial.printf("CFG EEPROM Checksum BAD\n"); | ||||||
|  |     FormatConfig_EEPROM(); | ||||||
|  |   } | ||||||
|   LubeConfig.checksum = checksum; |   LubeConfig.checksum = checksum; | ||||||
| } | } | ||||||
|  |  | ||||||
| @@ -82,10 +96,9 @@ void StorePersistence_EEPROM() | |||||||
|   PersistenceData.checksum = Checksum_EEPROM((uint8_t *)&PersistenceData, sizeof(PersistenceData)); |   PersistenceData.checksum = Checksum_EEPROM((uint8_t *)&PersistenceData, sizeof(PersistenceData)); | ||||||
|  |  | ||||||
| #if PCB_REVISION >= 12 | #if PCB_REVISION >= 12 | ||||||
|   if (eeAvailable) |   if (!ee.isConnected()) | ||||||
|   { |     return; | ||||||
|   ee.updateBlock(PersistenceDataAddress, (uint8_t *)&PersistenceData, sizeof(PersistenceData)); |   ee.updateBlock(PersistenceDataAddress, (uint8_t *)&PersistenceData, sizeof(PersistenceData)); | ||||||
|   } |  | ||||||
| #else | #else | ||||||
|   EEPROM.put(PersistenceDataAddress, PersistenceData); |   EEPROM.put(PersistenceDataAddress, PersistenceData); | ||||||
|   EEPROM.commit(); |   EEPROM.commit(); | ||||||
| @@ -96,24 +109,18 @@ void StorePersistence_EEPROM() | |||||||
| void GetPersistence_EEPROM() | void GetPersistence_EEPROM() | ||||||
| { | { | ||||||
| #if PCB_REVISION >= 12 | #if PCB_REVISION >= 12 | ||||||
|   if (eeAvailable) |   if (!ee.isConnected()) | ||||||
|   { |     return; | ||||||
|   eePersistenceMarker = (ee.readByte(0) << 8) | ee.readByte(1); |   eePersistenceMarker = (ee.readByte(0) << 8) | ee.readByte(1); | ||||||
|     Serial.printf("get EE eePersistenceMarker: 0x%04X\n", eePersistenceMarker); |  | ||||||
|   } |  | ||||||
| #else | #else | ||||||
|   EEPROM.begin(512); |   EEPROM.begin(512); | ||||||
|   EEPROM.get(0, eePersistenceMarker); |   EEPROM.get(0, eePersistenceMarker); | ||||||
| #endif | #endif | ||||||
|  |  | ||||||
|   uint16_t PersistenceDataAddress = startofPersistence + eePersistenceMarker; |   uint16_t PersistenceDataAddress = startofPersistence + eePersistenceMarker; | ||||||
|   Serial.printf("get EE PersistenceDataAddress: 0x%04X\n", PersistenceDataAddress); |  | ||||||
|  |  | ||||||
| #if PCB_REVISION >= 12 | #if PCB_REVISION >= 12 | ||||||
|   if (eeAvailable) |  | ||||||
|   { |  | ||||||
|   ee.readBlock(PersistenceDataAddress, (uint8_t *)&PersistenceData, sizeof(PersistenceData)); |   ee.readBlock(PersistenceDataAddress, (uint8_t *)&PersistenceData, sizeof(PersistenceData)); | ||||||
|   } |  | ||||||
| #else | #else | ||||||
|   EEPROM.get(PersistenceDataAddress, PersistenceData); |   EEPROM.get(PersistenceDataAddress, PersistenceData); | ||||||
|   EEPROM.end(); |   EEPROM.end(); | ||||||
| @@ -122,11 +129,7 @@ void GetPersistence_EEPROM() | |||||||
|   uint32_t checksum = PersistenceData.checksum; |   uint32_t checksum = PersistenceData.checksum; | ||||||
|   PersistenceData.checksum = 0; |   PersistenceData.checksum = 0; | ||||||
|  |  | ||||||
|   if (Checksum_EEPROM((uint8_t *)&PersistenceData, sizeof(PersistenceData)) == checksum) |   if (Checksum_EEPROM((uint8_t *)&PersistenceData, sizeof(PersistenceData)) != checksum) | ||||||
|   { |  | ||||||
|     Serial.printf("Persistance EEPROM Checksum OK\n"); |  | ||||||
|   } |  | ||||||
|   else |  | ||||||
|   { |   { | ||||||
|     Serial.printf("Persistance EEPROM Checksum BAD\n"); |     Serial.printf("Persistance EEPROM Checksum BAD\n"); | ||||||
|     FormatPersistence_EEPROM(); |     FormatPersistence_EEPROM(); | ||||||
| @@ -155,16 +158,14 @@ void MovePersistencePage_EEPROM(boolean reset) | |||||||
|   PersistenceData.writeCycleCounter = 0; |   PersistenceData.writeCycleCounter = 0; | ||||||
|  |  | ||||||
| #if PCB_REVISION >= 12 | #if PCB_REVISION >= 12 | ||||||
|  |   if (!ee.isConnected()) | ||||||
|  |     return; | ||||||
|   ee.updateByte(0, (uint8_t)(eePersistenceMarker >> 8)); |   ee.updateByte(0, (uint8_t)(eePersistenceMarker >> 8)); | ||||||
|   ee.updateByte(1, (uint8_t)(eePersistenceMarker & 0xFF)); |   ee.updateByte(1, (uint8_t)(eePersistenceMarker & 0xFF)); | ||||||
| #else | #else | ||||||
|   EEPROM.begin(512); |   EEPROM.begin(512); | ||||||
|   EEPROM.put(0, eePersistenceMarker); |   EEPROM.put(0, eePersistenceMarker); | ||||||
| #endif | #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)); |  | ||||||
| } | } | ||||||
|  |  | ||||||
| uint32_t Checksum_EEPROM(uint8_t const *data, size_t len) | uint32_t Checksum_EEPROM(uint8_t const *data, size_t len) | ||||||
| @@ -183,8 +184,14 @@ uint32_t Checksum_EEPROM(uint8_t const *data, size_t len) | |||||||
|  |  | ||||||
| void dumpEEPROM(uint16_t memoryAddress, uint16_t length) | void dumpEEPROM(uint16_t memoryAddress, uint16_t length) | ||||||
| { | { | ||||||
|   const int BLOCK_TO_LENGTH = 16; | #define BLOCK_TO_LENGTH 16 | ||||||
|   Serial.print("\nAddr.   "); |  | ||||||
|  |   if (!ee.isConnected()) | ||||||
|  |     return; | ||||||
|  |  | ||||||
|  |   char ascii_buf[BLOCK_TO_LENGTH + 1]; | ||||||
|  |   sprintf(ascii_buf, "%*s", BLOCK_TO_LENGTH, "ASCII"); | ||||||
|  |   Serial.print("\nAddress "); | ||||||
|   for (int x = 0; x < BLOCK_TO_LENGTH; x++) |   for (int x = 0; x < BLOCK_TO_LENGTH; x++) | ||||||
|     Serial.printf("%3d", x); |     Serial.printf("%3d", x); | ||||||
|  |  | ||||||
| @@ -193,9 +200,17 @@ void dumpEEPROM(uint16_t memoryAddress, uint16_t length) | |||||||
|  |  | ||||||
|   for (unsigned int i = 0; i < length; i++) |   for (unsigned int i = 0; i < length; i++) | ||||||
|   { |   { | ||||||
|     if (memoryAddress % BLOCK_TO_LENGTH == 0) |     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); |       Serial.printf("\n0x%05X:", memoryAddress); | ||||||
|     Serial.printf(" %02X", ee.readByte(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++; |     memoryAddress++; | ||||||
|   } |   } | ||||||
|   Serial.println(); |   Serial.println(); | ||||||
|   | |||||||
| @@ -8,6 +8,7 @@ | |||||||
| #else | #else | ||||||
| #include <EEPROM.h> | #include <EEPROM.h> | ||||||
| #endif | #endif | ||||||
|  | #include "globals.h" | ||||||
|  |  | ||||||
| #define EEPROM_SIZE_BYTES I2C_DEVICESIZE_24LC256 | #define EEPROM_SIZE_BYTES I2C_DEVICESIZE_24LC256 | ||||||
|  |  | ||||||
| @@ -89,6 +90,7 @@ typedef struct | |||||||
| #if PCB_REVISION >= 12 | #if PCB_REVISION >= 12 | ||||||
| void InitEEPROM(); | void InitEEPROM(); | ||||||
| #endif | #endif | ||||||
|  | void EEPROM_Process(); | ||||||
| void StoreConfig_EEPROM(); | void StoreConfig_EEPROM(); | ||||||
| void GetConfig_EEPROM(); | void GetConfig_EEPROM(); | ||||||
| void StorePersistence_EEPROM(); | void StorePersistence_EEPROM(); | ||||||
|   | |||||||
| @@ -13,12 +13,22 @@ typedef enum eSystem_Status | |||||||
|   sysStat_Shutdown |   sysStat_Shutdown | ||||||
| } tSystem_Status; | } tSystem_Status; | ||||||
|  |  | ||||||
|  | typedef enum eEERequest | ||||||
|  | { | ||||||
|  |   EE_IDLE, | ||||||
|  |   EE_CFG_SAVE, | ||||||
|  |   EE_CFG_LOAD, | ||||||
|  |   EE_PDS_SAVE, | ||||||
|  |   EE_PDS_LOAD | ||||||
|  | } tEERequest; | ||||||
|  |  | ||||||
| typedef struct Globals_s { | typedef struct Globals_s | ||||||
|  | { | ||||||
|   tSystem_Status systemStatus = sysStat_Startup; |   tSystem_Status systemStatus = sysStat_Startup; | ||||||
|   uint8_t purgePulses= 0; |   uint8_t purgePulses= 0; | ||||||
|   tSystem_Status resumeStatus = sysStat_Startup; |   tSystem_Status resumeStatus = sysStat_Startup; | ||||||
|   char systemStatustxt[16] = ""; |   char systemStatustxt[16] = ""; | ||||||
|  |   eEERequest requestEEAction = EE_IDLE; | ||||||
| } Globals_t; | } Globals_t; | ||||||
|  |  | ||||||
| extern Globals_t globals; | extern Globals_t globals; | ||||||
|   | |||||||
| @@ -58,6 +58,8 @@ void RemoteDebug_formatPersistence(); | |||||||
| void RemotDebug_printSystemInfo(); | void RemotDebug_printSystemInfo(); | ||||||
| void RemoteDebug_printWifiInfo(); | void RemoteDebug_printWifiInfo(); | ||||||
| void RemoteDebug_CheckEEPOM(); | void RemoteDebug_CheckEEPOM(); | ||||||
|  | void RemoteDebug_dumpConfig(); | ||||||
|  | void RemoteDebug_dumpPersistance(); | ||||||
| void updateWebUITicker_callback(); | void updateWebUITicker_callback(); | ||||||
| void IRAM_ATTR trigger_ISR(); | void IRAM_ATTR trigger_ISR(); | ||||||
| void LED_Process(uint8_t override = false, CRGB setColor = CRGB::White); | void LED_Process(uint8_t override = false, CRGB setColor = CRGB::White); | ||||||
| @@ -183,6 +185,7 @@ void loop() | |||||||
|   Display_Process(); |   Display_Process(); | ||||||
|   Button_Process(); |   Button_Process(); | ||||||
|   LED_Process(); |   LED_Process(); | ||||||
|  |   EEPROM_Process(); | ||||||
|  |  | ||||||
|   ArduinoOTA.handle(); |   ArduinoOTA.handle(); | ||||||
|   Debug.handle(); |   Debug.handle(); | ||||||
| @@ -222,6 +225,12 @@ void processCmdRemoteDebug() | |||||||
|     dumpEEPROM(0, EEPROM_SIZE_BYTES); |     dumpEEPROM(0, EEPROM_SIZE_BYTES); | ||||||
|   else if (lastCmd == "resetPageEE") |   else if (lastCmd == "resetPageEE") | ||||||
|     MovePersistencePage_EEPROM(true); |     MovePersistencePage_EEPROM(true); | ||||||
|  |   else if (lastCmd == "dumpCFG") | ||||||
|  |     RemoteDebug_dumpConfig(); | ||||||
|  |   else if (lastCmd == "dumpPDS") | ||||||
|  |     RemoteDebug_dumpPersistance(); | ||||||
|  |   else if (lastCmd == "saveEE") | ||||||
|  |     StoreConfig_EEPROM(); | ||||||
| } | } | ||||||
|  |  | ||||||
| void RemoteDebug_formatCFG() | void RemoteDebug_formatCFG() | ||||||
| @@ -258,6 +267,35 @@ void RemotDebug_printSystemInfo() | |||||||
|   debugA("Git-Revison: %s", GIT_REV); |   debugA("Git-Revison: %s", GIT_REV); | ||||||
| } | } | ||||||
|  |  | ||||||
|  | void RemoteDebug_dumpConfig() | ||||||
|  | { | ||||||
|  |   debugA("DistancePerLube_Default: %d", LubeConfig.DistancePerLube_Default); | ||||||
|  |   debugA("DistancePerLube_Rain: %d", LubeConfig.DistancePerLube_Rain); | ||||||
|  |   debugA("tankCapacity_ml: %d", LubeConfig.tankCapacity_ml); | ||||||
|  |   debugA("amountPerDose_µl: %d", LubeConfig.amountPerDose_µl); | ||||||
|  |   debugA("TankRemindAtPercentage: %d", LubeConfig.TankRemindAtPercentage); | ||||||
|  |   debugA("PulsePerRevolution: %d", LubeConfig.PulsePerRevolution); | ||||||
|  |   debugA("TireWidth_mm: %d", LubeConfig.TireWidth_mm); | ||||||
|  |   debugA("TireWidthHeight_Ratio: %d", LubeConfig.TireWidth_mm); | ||||||
|  |   debugA("RimDiameter_Inch: %d", LubeConfig.RimDiameter_Inch); | ||||||
|  |   debugA("DistancePerRevolution_mm: %d", LubeConfig.DistancePerRevolution_mm); | ||||||
|  |   debugA("BleedingPulses: %d", LubeConfig.BleedingPulses); | ||||||
|  |   debugA("SpeedSource: %d", LubeConfig.SpeedSource); | ||||||
|  |   debugA("GPSBaudRate: %d", LubeConfig.GPSBaudRate); | ||||||
|  | #if PCB_REVISION == 13 | ||||||
|  |   debugA("CANSource: %d", LubeConfig.CANSource); | ||||||
|  | #endif | ||||||
|  |   debugA("checksum: 0x%08X", LubeConfig.checksum); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | void RemoteDebug_dumpPersistance() | ||||||
|  | { | ||||||
|  |   debugA("writeCycleCounter: %d", PersistenceData.writeCycleCounter); | ||||||
|  |   debugA("tankRemain_µl: %d", PersistenceData.tankRemain_µl); | ||||||
|  |   debugA("distanceTraveled_m: %d", PersistenceData.distanceTraveled_m); | ||||||
|  |   debugA("checksum: %d", PersistenceData.checksum); | ||||||
|  | } | ||||||
|  |  | ||||||
| void RemoteDebug_printWifiInfo() | void RemoteDebug_printWifiInfo() | ||||||
| { | { | ||||||
| } | } | ||||||
|   | |||||||
| @@ -3,6 +3,9 @@ const char helpCmd[] = "sysinfo     - System Info\r\n" | |||||||
|                        "formatPDS   - Format Persistence EEPROM Data\r\n" |                        "formatPDS   - Format Persistence EEPROM Data\r\n" | ||||||
|                        "formatCFG   - Format Configuration EEPROM Data\r\n" |                        "formatCFG   - Format Configuration EEPROM Data\r\n" | ||||||
|                        "checkEE     - Check EEPROM with checksum\r\n" |                        "checkEE     - Check EEPROM with checksum\r\n" | ||||||
|                        "dumpEE1k    - This will dump the first 1kb of EEPROM to Serial\r\n"   |                        "dumpEE1k    - dump the first 1kb of EEPROM to Serial\r\n"   | ||||||
|                        "dumpEE      - This will dump the whole EPPROM to Serial\r\n" |                        "dumpEE      - dump the whole EPPROM to Serial\r\n" | ||||||
|                        "resetPageEE - This will Reset the PersistenceData Page\r\n"; |                        "resetPageEE - Reset the PersistenceData Page\r\n" | ||||||
|  |                        "dumpCFG     - print Config struct\r\n" | ||||||
|  |                        "dumpPDS     - print PersistanceStruct\r\n" | ||||||
|  |                        "saveEE      - save EE-Data\r\n"; | ||||||
| @@ -95,13 +95,13 @@ void buttons_Callback(Control *sender, int type) | |||||||
|  |  | ||||||
|   if (sender->id == button_store) |   if (sender->id == button_store) | ||||||
|   { |   { | ||||||
|     StoreConfig_EEPROM(); |     globals.requestEEAction = EE_CFG_SAVE; | ||||||
|     ESPUI.print(label_storeStatus, "Successfully Stored Settings"); |     ESPUI.print(label_storeStatus, "Successfully Stored Settings"); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   if (sender->id == button_reload) |   if (sender->id == button_reload) | ||||||
|   { |   { | ||||||
|     GetConfig_EEPROM(); |     globals.requestEEAction = EE_CFG_LOAD; | ||||||
|     ESPUI.print(label_storeStatus, "Successfully Reloaded Settings"); |     ESPUI.print(label_storeStatus, "Successfully Reloaded Settings"); | ||||||
|   } |   } | ||||||
| } | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user