Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EEPROMClass _dirty #2217

Closed
pgScorpio opened this issue Jul 3, 2016 · 4 comments
Closed

EEPROMClass _dirty #2217

pgScorpio opened this issue Jul 3, 2016 · 4 comments

Comments

@pgScorpio
Copy link

Hardware

Hardware: n.a.
Core Version: 2.2.0

Description

1:
There is no way to undo/revert any changes after a begin.
Currently the only way would be to call begin again, but... See 2:

I suggest adding:

bool EEPROMClass::revert() {
  if (_data && _size)
  {
    noInterrupts();
    spi_flash_read(_sector * SPI_FLASH_SEC_SIZE, reinterpret_cast<uint32_t*>(_data), _size);
    _dirty = false;
    interrupts();
    return true;
  }

  return false;
}

2:
void EEPROMClass::begin(...) Does read _data from eeprom but does not clear the _dirty flag.
Also it would be handy if EEPROMClass::begin(...) would return the _data pointer.

I suggest:

uint8_t * EEPROMClass::begin(size_t size /* =  SPI_FLASH_SEC_SIZE */) {
  if (_data) {
    delete[] _data;
    _data = NULL;
  }
  _size = 0;
  _dirty = false;

  if ( (size > 0) && (size <= SPI_FLASH_SEC_SIZE) ) {
     size = (size + 3) & (~3); 
    _data = new uint8_t[size];
    if ( _data)  {
      _size = size;
      noInterrupts();
      spi_flash_read(_sector * SPI_FLASH_SEC_SIZE, reinterpret_cast<uint32_t*>(_data), _size);
      interrupts();
    }
  }
  return &_data[0];
}

3:
EEPROMClass::getDataPtr() Does always set _dirty. This is not wanted if we are using the pointer only for reading....

I suggest:

uint8_t * EEPROMClass::getDataPtr(bool forWrite /* = true */) {
  if (forWrite) _dirty = true;
  return &_data[0];
}

with the default forWrite = true getDataPtr() would still be compatible with the current version but when just reading getDataPtr(false) would not set _dirty to true

@devyte
Copy link
Collaborator

devyte commented Oct 16, 2017

For read-only of _data, a method that returns a const ptr could be implemented, which doesn't set _dirty.

@devyte
Copy link
Collaborator

devyte commented Nov 19, 2017

@pgScorpio Please test PR #3849 .
EEPROM::begin() now clears dirty flag
EEPROM::getConstDataPtr() returns a pointer to const data for read-only, and it doesn't set dirty.

@dunk8888
Copy link

Hi,what does this EEPROM.getDataPtr(); actualy do? What is happening bellow,i want to port a sketch to Arduino uno and the uno eeprom library doesnt support this EEPROM.getDataPtr();.

All i know its a pointer but why is it needed.thanks

EEPROM.begin(EEPROM_SIZE);
memcpy(&ref,EEPROM.getDataPtr()+EEPROM_PATTERNS_START+1+patternIndex*sizeof(PatternReference),sizeof(PatternReference));
EEPROM.end();

@devyte
Copy link
Collaborator

devyte commented Jan 15, 2018

In our case EEPROM is emulated on flash. To avoid rewriting on every byte, there is a buffer kept in memory, so when you write to an index in the eeprom, you really just write into the buffer. Then when you call commit, the buffer gets written to flash all at once.
The getPtr() method gets the underying pointer to the mem buffer. It's an esp-specific method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants