Unclear about the EEPROM API

Is this understanding correct?

If I repeatedly write to the same address in EEPROM ( e.g. EEPROM.write( 1, 2 ) ), the firmware will do some magic so that I don’t wear out the underlying flash memory. Therefore, I don’t have to do the magic myself and write my data to a different address every time, right?

So what’s that EEPROM.performPendingErase() all about then? If I don’t “see” the pages in the API, and in fact I don’t even know how many there are, how do I use this?

If I repeatedly write to the same address in EEPROM ( e.g. EEPROM.write( 1, 2 ) ), the firmware will do some magic so that I don't wear out the underlying flash memory. Therefore, I don't have to do the magic myself and write my data to a different address every time, right?

That is correct

So what's that EEPROM.performPendingErase() all about then? If I don't "see" the pages in the API, and in fact I don't even know how many there are, how do I use this?

See the note in the docs. It talks about when and how you'd use this.

It’s that note that is confusing … if I never invoke EEPROM.hasPendingErase() or EEPROM.performPendingErase(), presumably the firmware will perform the erase on its own some time (when?).

If I do invoke EEPROM.hasPendingErase(), will it not? How often do I have to invoke it? How long will it wait until it will do the erase anyway?

I’m unsure. Paging @mdma

1 Like

The firmware will delay performing the erase until it’s absolutely necessary. The purpose of those two functions is to allow the application to perform the erase since it stalls the MCU for a few hundred milliseconds.

1 Like

Given @mdma’s response (thanks @mdma!) can we mark this topic “solved”?

1 Like

Can I suggest you add something like this to the documentation: “The firmware will delay performing the erase until it’s absolutely necessary. However, because the erase stalls the MCU for a few hundred milliseconds, the following two API methods allow applications to check whether an erase is necessary in the future, and perform it at a time that is most convenience for the application.”

On second thought, this doesn’t give me any indication just how urgent it is to perform the erase. If I happily write into EEPROM once per hour/minute/whatever, hasPendingErase() will turn true at some point (how quickly?) Then it will stay true, and if I don’t do anything, the firmware will perform the erase at some point. Should I manually perform the erase as soon as it turns true, or wait for … how long? Some kind of level indicator would be useful, so I can say my app will run erase manually as soon as it hits 80% or such.

On third thought – sorry, I’m a little slow today, that must be from yesterday’s Christmas food – it seems I can provide that level indicator myself if I count my writes times size and compare with 2*EEPROM.length(). Right?

The point is that you perform the erase when it’s ok that your application stalls for a few hundred milliseconds. It has nothing to do with eeprom endurance per se, but merely giving you the opportunity to schedule the erase at a convenient moment (e.g. such as a before going to sleep, or during some other idle time.)

@mdma I think @jernstlun has a good point. When "hasPendingErase returns true how “full” is the EEPROM? How dire is the need to perform the erase?

It’s as full as the amount of bytes of data that has been stored in it. E.g. if you have written many many different values to index 1-5 then the eeprom will contain 5 bytes of data. The page erase flag is set at the point the eeprom switches to a new page.

Ok, so here is what I believe is correct. Please correct me if it’s not. Code sample (A):

for( int i=0 ; 1 ; ++i ) {
    EEPROM.write( i % EEPROM.length(), i % 256 );
}

Code sample (B):

for( int i=0 ; 1 ; ++i ) {
    EEPROM.write( 0, i % 256 );
}

For both code samples, the behavior regarding EEPROM.performPendingErase() is the same:

  • initially, EEPROM.hasPendingErase() returns false.
  • once i>=EEPROM.length(), EEPROM.hasPendingErase() returns true, and if I invoked EEPROM.performPendingErase(), EEPROM.hasPendingErase() would go back to false.
  • once i=2*EEPROM.length(), the firmware performs the erase on it own.
  • then, the behavior is the same again as when we started.

Right?