fabriziop / EEWL

The EEWL library allows to extend the EEPROM life by distributing data writes along a circular buffer.
GNU Lesser General Public License v3.0
10 stars 1 forks source link

Power failure during put() #2

Closed lmagyar closed 1 year ago

lmagyar commented 1 year ago

If I understand correctly the source:

If power is lost during the previous steps, the only block marked valid will store invalid data.

Shouldn't we:

And modify the begin() method:

And 1 more question:

fabriziop commented 1 year ago

If I understand correctly the source:

* the current valid block is marked invalid

* new block is marked valid

* then new data is written

If power is lost during the previous steps, the only block marked valid will store invalid data.

You are right. At present, EEWL is not power down safe. I imagined a usage scenario where it is possible to have an impending power down signal / interrupt sufficient to avoid unsafe write conditions.

Shouldn't we:

* write new data

* mark new block valid

* mark old block invalid

And modify the begin() method:

* 0 or 1 valid block: OK

* 2 valid blocks:

  * if they are next to each other, use the last and invalidate the first
  * if they are at the 2 end of the buffer (first and last block is marked valid), use the first and invalidate the last

Your proposed improvement is interesting, i will implement it in EEWL.

* do not allow to use block size <= 2 ~= 1~ **UPDATE:** in case of 2 blocks we should version the validation mark byte to know which is the later, so the moving zero bit in the marker byte can be used to determine which of the 2 blocks has a later version

Not clear to me why a block size > 2 is needed.

And 1 more question:

* why is the marker byte's 0 bit continuously "moving around" (`blk_mark <<= 1; ...`), why can't we use the same marker for all blocks? like 0x55 or 0xaa or 0x5a?

The scope is to distribute the zero bit writing along the whole byte in the hope to reduce EEPROM wearing at bit level. I am not sure that is the case for EEPROM, it may be that cell wear happens independently of the type written data: 0s or 1s.

lmagyar commented 1 year ago

i will implement it in EEWL

This way this lib will be an power failure safe EEPROM storage also.

Not clear to me why a block size > 2 is needed.

Oh, that is a typo. :) I meant block num. And now I slightly modified the "algorithm" in the original comment.

fabriziop commented 1 year ago

Done: commit f7f0eb91592637d01dd9467c533463cdefc4fe03