diff --git a/Software/ChainLube/src/config.cpp b/Software/ChainLube/src/config.cpp index ea32262..dcebb32 100644 --- a/Software/ChainLube/src/config.cpp +++ b/Software/ChainLube/src/config.cpp @@ -11,34 +11,48 @@ uint16_t eeVersion = 0; // inc boolean eeAvailable = false; const uint16_t startofLubeConfig = sizeof(eePersistenceMarker); -const uint16_t startofPersistence = sizeof(LubeConfig); +const uint16_t startofPersistence = sizeof(LubeConfig) + sizeof(eePersistenceMarker); #if PCB_REVISION >= 12 void InitEEPROM() { ee.begin(); - if (ee.isConnected()) - { - eeAvailable = true; - Serial.println("EEPROM Initialized..."); - } - else - { + if (!ee.isConnected()) Serial.println("ERROR: Can't find eeprom..."); - } } #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() { LubeConfig.checksum = 0; LubeConfig.checksum = Checksum_EEPROM((uint8_t *)&LubeConfig, sizeof(LubeConfig)); - #if PCB_REVISION >= 12 - if (eeAvailable) - { - ee.updateBlock(startofLubeConfig, (uint8_t *)&LubeConfig, sizeof(LubeConfig)); - } + if (!ee.isConnected()) + return; + ee.writeBlock(startofLubeConfig, (uint8_t *)&LubeConfig, sizeof(LubeConfig)); #else EEPROM.begin(512); EEPROM.put(startofLubeConfig, LubeConfig); @@ -50,10 +64,9 @@ void StoreConfig_EEPROM() void GetConfig_EEPROM() { #if PCB_REVISION >= 12 - if (eeAvailable) - { - ee.readBlock(startofLubeConfig, (uint8_t *)&LubeConfig, sizeof(LubeConfig)); - } + if (!ee.isConnected()) + return; + ee.readBlock(startofLubeConfig, (uint8_t *)&LubeConfig, sizeof(LubeConfig)); #else EEPROM.begin(512); EEPROM.get(startofLubeConfig, LubeConfig); @@ -63,10 +76,11 @@ void GetConfig_EEPROM() uint32_t checksum = LubeConfig.checksum; LubeConfig.checksum = 0; - if (Checksum_EEPROM((uint8_t *)&LubeConfig, sizeof(LubeConfig)) == checksum) - Serial.printf("CFG EEPROM Checksum OK\n"); - else + if (Checksum_EEPROM((uint8_t *)&LubeConfig, sizeof(LubeConfig)) != checksum) + { Serial.printf("CFG EEPROM Checksum BAD\n"); + FormatConfig_EEPROM(); + } LubeConfig.checksum = checksum; } @@ -82,10 +96,9 @@ void StorePersistence_EEPROM() PersistenceData.checksum = Checksum_EEPROM((uint8_t *)&PersistenceData, sizeof(PersistenceData)); #if PCB_REVISION >= 12 - if (eeAvailable) - { - ee.updateBlock(PersistenceDataAddress, (uint8_t *)&PersistenceData, sizeof(PersistenceData)); - } + if (!ee.isConnected()) + return; + ee.updateBlock(PersistenceDataAddress, (uint8_t *)&PersistenceData, sizeof(PersistenceData)); #else EEPROM.put(PersistenceDataAddress, PersistenceData); EEPROM.commit(); @@ -96,24 +109,18 @@ void StorePersistence_EEPROM() void GetPersistence_EEPROM() { #if PCB_REVISION >= 12 - if (eeAvailable) - { - eePersistenceMarker = (ee.readByte(0) << 8) | ee.readByte(1); - Serial.printf("get EE eePersistenceMarker: 0x%04X\n", eePersistenceMarker); - } + if (!ee.isConnected()) + return; + eePersistenceMarker = (ee.readByte(0) << 8) | ee.readByte(1); #else EEPROM.begin(512); EEPROM.get(0, eePersistenceMarker); #endif uint16_t PersistenceDataAddress = startofPersistence + eePersistenceMarker; - Serial.printf("get EE PersistenceDataAddress: 0x%04X\n", PersistenceDataAddress); #if PCB_REVISION >= 12 - if (eeAvailable) - { - ee.readBlock(PersistenceDataAddress, (uint8_t *)&PersistenceData, sizeof(PersistenceData)); - } + ee.readBlock(PersistenceDataAddress, (uint8_t *)&PersistenceData, sizeof(PersistenceData)); #else EEPROM.get(PersistenceDataAddress, PersistenceData); EEPROM.end(); @@ -122,11 +129,7 @@ void GetPersistence_EEPROM() 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 + if (Checksum_EEPROM((uint8_t *)&PersistenceData, sizeof(PersistenceData)) != checksum) { Serial.printf("Persistance EEPROM Checksum BAD\n"); FormatPersistence_EEPROM(); @@ -155,16 +158,14 @@ void MovePersistencePage_EEPROM(boolean reset) PersistenceData.writeCycleCounter = 0; #if PCB_REVISION >= 12 + if (!ee.isConnected()) + return; 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)); } 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) { - const int BLOCK_TO_LENGTH = 16; - Serial.print("\nAddr. "); +#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 "); for (int x = 0; x < BLOCK_TO_LENGTH; 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++) { - 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(" %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++; } Serial.println(); diff --git a/Software/ChainLube/src/config.h b/Software/ChainLube/src/config.h index cca54bd..1772a87 100644 --- a/Software/ChainLube/src/config.h +++ b/Software/ChainLube/src/config.h @@ -8,6 +8,7 @@ #else #include #endif +#include "globals.h" #define EEPROM_SIZE_BYTES I2C_DEVICESIZE_24LC256 @@ -89,6 +90,7 @@ typedef struct #if PCB_REVISION >= 12 void InitEEPROM(); #endif +void EEPROM_Process(); void StoreConfig_EEPROM(); void GetConfig_EEPROM(); void StorePersistence_EEPROM(); diff --git a/Software/ChainLube/src/globals.h b/Software/ChainLube/src/globals.h index 31200ba..8934acb 100644 --- a/Software/ChainLube/src/globals.h +++ b/Software/ChainLube/src/globals.h @@ -13,13 +13,23 @@ typedef enum eSystem_Status sysStat_Shutdown } 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; uint8_t purgePulses= 0; tSystem_Status resumeStatus = sysStat_Startup; char systemStatustxt[16] = ""; -}Globals_t; + eEERequest requestEEAction = EE_IDLE; +} Globals_t; extern Globals_t globals; extern uint32_t TravelDistance_highRes; diff --git a/Software/ChainLube/src/main.cpp b/Software/ChainLube/src/main.cpp index 5ca084f..23d9454 100644 --- a/Software/ChainLube/src/main.cpp +++ b/Software/ChainLube/src/main.cpp @@ -58,6 +58,8 @@ void RemoteDebug_formatPersistence(); void RemotDebug_printSystemInfo(); void RemoteDebug_printWifiInfo(); void RemoteDebug_CheckEEPOM(); +void RemoteDebug_dumpConfig(); +void RemoteDebug_dumpPersistance(); void updateWebUITicker_callback(); void IRAM_ATTR trigger_ISR(); void LED_Process(uint8_t override = false, CRGB setColor = CRGB::White); @@ -183,6 +185,7 @@ void loop() Display_Process(); Button_Process(); LED_Process(); + EEPROM_Process(); ArduinoOTA.handle(); Debug.handle(); @@ -222,6 +225,12 @@ void processCmdRemoteDebug() dumpEEPROM(0, EEPROM_SIZE_BYTES); else if (lastCmd == "resetPageEE") MovePersistencePage_EEPROM(true); + else if (lastCmd == "dumpCFG") + RemoteDebug_dumpConfig(); + else if (lastCmd == "dumpPDS") + RemoteDebug_dumpPersistance(); + else if (lastCmd == "saveEE") + StoreConfig_EEPROM(); } void RemoteDebug_formatCFG() @@ -258,6 +267,35 @@ void RemotDebug_printSystemInfo() 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() { } diff --git a/Software/ChainLube/src/rmtdbghelp.h b/Software/ChainLube/src/rmtdbghelp.h index 55df231..2ca38e1 100644 --- a/Software/ChainLube/src/rmtdbghelp.h +++ b/Software/ChainLube/src/rmtdbghelp.h @@ -3,6 +3,9 @@ const char helpCmd[] = "sysinfo - System Info\r\n" "formatPDS - Format Persistence EEPROM Data\r\n" "formatCFG - Format Configuration EEPROM Data\r\n" "checkEE - Check EEPROM with checksum\r\n" - "dumpEE1k - This will dump the first 1kb of EEPROM to Serial\r\n" - "dumpEE - This will dump the whole EPPROM to Serial\r\n" - "resetPageEE - This will Reset the PersistenceData Page\r\n"; \ No newline at end of file + "dumpEE1k - dump the first 1kb of EEPROM to Serial\r\n" + "dumpEE - dump the whole EPPROM to Serial\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"; \ No newline at end of file diff --git a/Software/ChainLube/src/webui.cpp b/Software/ChainLube/src/webui.cpp index 3a5bc58..60c8201 100644 --- a/Software/ChainLube/src/webui.cpp +++ b/Software/ChainLube/src/webui.cpp @@ -95,13 +95,13 @@ void buttons_Callback(Control *sender, int type) if (sender->id == button_store) { - StoreConfig_EEPROM(); + globals.requestEEAction = EE_CFG_SAVE; ESPUI.print(label_storeStatus, "Successfully Stored Settings"); } if (sender->id == button_reload) { - GetConfig_EEPROM(); + globals.requestEEAction = EE_CFG_LOAD; ESPUI.print(label_storeStatus, "Successfully Reloaded Settings"); } }