More fixes to SaveKey data file; make sure file is always 32KB.

This commit is contained in:
Stephen Anthony 2020-10-28 23:37:25 -02:30
parent b9bb99aa4c
commit e92a3882e7
2 changed files with 15 additions and 13 deletions

View File

@ -50,16 +50,26 @@ MT24LC256::MT24LC256(const FilesystemNode& eepromfile, const System& system,
myDataFile(eepromfile) myDataFile(eepromfile)
{ {
// Load the data from an external file (if it exists) // Load the data from an external file (if it exists)
bool fileValid = false;
try try
{ {
// Get length of file; it must be 32768 // A valid file must be 32768 bytes; otherwise we create a new one
if(myDataFile.read(myData) == FLASH_SIZE) if(myDataFile.read(myData) == FLASH_SIZE)
myDataFileExists = true; fileValid = true;
} }
catch(...) catch(...)
{ {
myDataFileExists = false; fileValid = false;
}
if(!fileValid)
{
// Work around a bug in XCode 11.2 with -O0 and -O1
const uInt8 initialValue = INITIAL_VALUE;
myData = make_unique<uInt8[]>(FLASH_SIZE); myData = make_unique<uInt8[]>(FLASH_SIZE);
std::fill_n(myData.get(), FLASH_SIZE, initialValue);
myDataChanged = true;
} }
// Then initialize the I2C state // Then initialize the I2C state
@ -72,7 +82,7 @@ MT24LC256::MT24LC256(const FilesystemNode& eepromfile, const System& system,
MT24LC256::~MT24LC256() MT24LC256::~MT24LC256()
{ {
// Save EEPROM data to external file only when necessary // Save EEPROM data to external file only when necessary
if(!myDataFileExists || myDataChanged) if(myDataChanged)
{ {
try { myDataFile.write(myData, FLASH_SIZE); } try { myDataFile.write(myData, FLASH_SIZE); }
catch(...) { } catch(...) { }
@ -167,9 +177,6 @@ bool MT24LC256::isPageUsed(uInt32 page) const
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void MT24LC256::jpee_init() void MT24LC256::jpee_init()
{ {
// Work around a bug in XCode 11.2 with -O0 and -O1
const uInt8 initialValue = INITIAL_VALUE;
jpee_sdat = 1; jpee_sdat = 1;
jpee_address = 0; jpee_address = 0;
jpee_state=0; jpee_state=0;
@ -177,8 +184,6 @@ void MT24LC256::jpee_init()
jpee_pagemask = PAGE_SIZE - 1; jpee_pagemask = PAGE_SIZE - 1;
jpee_smallmode = 0; jpee_smallmode = 0;
jpee_logmode = -1; jpee_logmode = -1;
if(!myDataFileExists)
std::fill_n(myData.get(), FLASH_SIZE, initialValue);
} }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

View File

@ -113,9 +113,6 @@ class MT24LC256
// The file containing the EEPROM data // The file containing the EEPROM data
FilesystemNode myDataFile; FilesystemNode myDataFile;
// Indicates if a valid EEPROM data file exists/was successfully loaded
bool myDataFileExists{false};
// Indicates if the EEPROM has changed since class invocation // Indicates if the EEPROM has changed since class invocation
bool myDataChanged{false}; bool myDataChanged{false};