added CV Storage EEPROM delayed commit

fixed debug for RP2040
This commit is contained in:
Alex Shepherd
2022-02-28 00:35:14 +13:00
parent 6f91d4f7d4
commit 5f1e271f2c
2 changed files with 29 additions and 1 deletions

View File

@@ -48,6 +48,7 @@
#include "NmraDcc.h"
#include "EEPROM.h"
#include <elapsedMillis.h>
// Uncomment to print DEBUG messages
// #define DEBUG_PRINT
@@ -242,8 +243,13 @@
#endif
#ifdef DEBUG_PRINT
#ifdef ARDUINO_ARCH_RP2040
#define DB_PRINT( x, ... ) { char dbgbuf[80]; sprintf( dbgbuf, (const char*) F( x ) , ##__VA_ARGS__ ) ; Serial.println( dbgbuf ); }
#define DB_PRINT_( x, ... ) { char dbgbuf[80]; sprintf( dbgbuf, (const char*) F( x ) , ##__VA_ARGS__ ) ; Serial.print( dbgbuf ); }
#else
#define DB_PRINT( x, ... ) { char dbgbuf[80]; sprintf_P( dbgbuf, (const char*) F( x ) , ##__VA_ARGS__ ) ; Serial.println( dbgbuf ); }
#define DB_PRINT_( x, ... ) { char dbgbuf[80]; sprintf_P( dbgbuf, (const char*) F( x ) , ##__VA_ARGS__ ) ; Serial.print( dbgbuf ); }
#endif
#else
#define DB_PRINT( x, ... ) ;
#define DB_PRINT_( x, ... ) ;
@@ -322,6 +328,11 @@ typedef struct
uint8_t TickCount;
uint8_t NestedIrqCount;
#endif
#if defined(ESP8266) || defined(ESP32) || defined(ARDUINO_ARCH_RP2040)
elapsedMillis millisSinceEEPromWrite = 0;
bool eepromChanged = false;
#endif
}
DCC_PROCESSOR_STATE ;
@@ -801,8 +812,10 @@ uint8_t readEEPROM (unsigned int CV)
void writeEEPROM (unsigned int CV, uint8_t Value)
{
EEPROM.write (CV, Value) ;
#if defined(ESP8266) || defined(ESP32) || defined(ARDUINO_ARCH_RP2040)
EEPROM.commit();
DccProcState.eepromChanged = true;
DccProcState.millisSinceEEPromWrite = 0;
#endif
}
@@ -1756,6 +1769,15 @@ uint8_t NmraDcc::process()
clearDccProcState (0) ;
}
}
#if defined(ESP8266) || defined(ESP32) || defined(ARDUINO_ARCH_RP2040)
if(DccProcState.eepromChanged && (DccProcState.millisSinceEEPromWrite >= EEPROM_COMMIT_DELAY_MS))
{
EEPROM.commit();
DccProcState.eepromChanged = false;
DB_PRINT ("process: EEPROM Commit");
}
#endif
if (DccRx.DataReady)
{
@@ -1785,6 +1807,8 @@ uint8_t NmraDcc::process()
execDccProcessor (&Msg);
return 1 ;
}
return 0 ;
};

View File

@@ -81,6 +81,10 @@ typedef struct
#define MAN_ID_DIY 0x0D
#define MAN_ID_SILICON_RAILWAY 0x21
#if defined(ESP8266) || defined(ESP32) || defined(ARDUINO_ARCH_RP2040)
#define EEPROM_COMMIT_DELAY_MS 3000
#endif
//--------------------------------------------------------------------------
// This section contains the Product/Version Id Codes for projects
//