# Unclear about the EEPROM API

**URL:** <https://community.particle.io/t/unclear-about-the-eeprom-api/28094>\
**Category:** General\
**Created:** [December 22, 2016, 12:56am UTC](https://community.particle.io/t/unclear-about-the-eeprom-api/28094 "2016-12-22T00:56:34Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![jernstlun](https://avatars.discourse-cdn.com/v4/letter/j/e274bd/32.png) [@jernstlun](https://community.particle.io/u/jernstlun)\
**Post date:** [December 22, 2016, 12:56am UTC](https://community.particle.io/t/unclear-about-the-eeprom-api/28094/1 "2016-12-22T00:56:34Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![harrisonhjones](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/harrisonhjones/32/4132_2.png) [@harrisonhjones](https://community.particle.io/u/harrisonhjones)\
**Post date:** [December 22, 2016, 4:51am UTC](https://community.particle.io/t/unclear-about-the-eeprom-api/28094/2 "2016-12-22T04:51:53Z")

</div>

> 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](https://docs.particle.io/reference/firmware/electron/#haspendingerase-) in the docs. It talks about when and how you'd use this.

---

<div class="post-metadata">

**Author:** ![jernstlun](https://avatars.discourse-cdn.com/v4/letter/j/e274bd/32.png) [@jernstlun](https://community.particle.io/u/jernstlun)\
**Post date:** [December 22, 2016, 5:07pm UTC](https://community.particle.io/t/unclear-about-the-eeprom-api/28094/3 "2016-12-22T17:07:10Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![harrisonhjones](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/harrisonhjones/32/4132_2.png) [@harrisonhjones](https://community.particle.io/u/harrisonhjones)\
**Post date:** [December 22, 2016, 9:22pm UTC](https://community.particle.io/t/unclear-about-the-eeprom-api/28094/4 "2016-12-22T21:22:41Z")

</div>

I’m unsure. Paging @mdma

---

<div class="post-metadata">

**Author:** ![mdma](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/mdma/32/1924_2.png) [@mdma](https://community.particle.io/u/mdma)\
**Post date:** [December 26, 2016, 6:53am UTC](https://community.particle.io/t/unclear-about-the-eeprom-api/28094/5 "2016-12-26T06:53:28Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![harrisonhjones](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/harrisonhjones/32/4132_2.png) [@harrisonhjones](https://community.particle.io/u/harrisonhjones)\
**Post date:** [December 26, 2016, 4:39pm UTC](https://community.particle.io/t/unclear-about-the-eeprom-api/28094/6 "2016-12-26T16:39:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jernstlun](https://avatars.discourse-cdn.com/v4/letter/j/e274bd/32.png) [@jernstlun](https://community.particle.io/u/jernstlun)\
**Post date:** [December 26, 2016, 6:04pm UTC](https://community.particle.io/t/unclear-about-the-eeprom-api/28094/7 "2016-12-26T18:04:11Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![mdma](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/mdma/32/1924_2.png) [@mdma](https://community.particle.io/u/mdma)\
**Post date:** [December 26, 2016, 8:57pm UTC](https://community.particle.io/t/unclear-about-the-eeprom-api/28094/8 "2016-12-26T20:57:48Z")

</div>

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.)

---

<div class="post-metadata">

**Author:** ![harrisonhjones](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/harrisonhjones/32/4132_2.png) [@harrisonhjones](https://community.particle.io/u/harrisonhjones)\
**Post date:** [December 26, 2016, 10:17pm UTC](https://community.particle.io/t/unclear-about-the-eeprom-api/28094/9 "2016-12-26T22:17:48Z")

</div>

@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?

---

<div class="post-metadata">

**Author:** ![mdma](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/mdma/32/1924_2.png) [@mdma](https://community.particle.io/u/mdma)\
**Post date:** [December 27, 2016, 3:59am UTC](https://community.particle.io/t/unclear-about-the-eeprom-api/28094/10 "2016-12-27T03:59:46Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jernstlun](https://avatars.discourse-cdn.com/v4/letter/j/e274bd/32.png) [@jernstlun](https://community.particle.io/u/jernstlun)\
**Post date:** [December 27, 2016, 6:30pm UTC](https://community.particle.io/t/unclear-about-the-eeprom-api/28094/11 "2016-12-27T18:30:23Z")

</div>

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?
