Reset button for EEPROM?

Hi, have a Arduino Micro. I have a need to clear the EEPROM values stored, can I use the red reset button on the board to do this? I have no experience with programming here so more than a simple "yes" would be nice.....

No. You have to run an EEPROM clearing sketch. Why do you need to clear it?

No, you can not. That is what makes the EEPROM special - it will maintain it's values in case of a power down or reset event.

You need to clear the EEPROM with code. Write a function that uses the EEPROMClear function.

However, the sketch that was linked to stores zeros in EEPROM, but unwritten EEPROM locations actually contain all ones, or 0xFF.

That EEPROMClear page is typical Arduino Playground stuff. Terribad. I don't know where they dig up the people to write those articles.

An EEPROM full of 0's isn't cleared. An EEPROM full of 255's is cleared. The "blank" state of an EEPROM (same for flash) is 0xFF (255) in each cell. When you get a new Atmega chip, the EEPROM is filled with 0xFF (eg, every bit is a 1) - so if we want to clear it, we want to return it to that state, so we put a 255 in every cell. .

The reset button just restarts the code on the Arduino. Same thing happens when you open serial monitor, or go to upload a sketch (the serial adapter in the arduino board drives the wire connected to the button low, just like if you'd pressed it)

I agree with you, however, depending on the erased contents being all 255’s might be a bad software practice in some circumstances. Which is why I asked the OP why it is necessary to clear it.

Regards the playground, at least they were honest with the name. :slight_smile:

Yikes...good point. I assumed that it would erase the EEPROM, not write it to all 00s.

So, with Arduino, what is the best way to erase the EEPROM without using a write cycle? From Table 8-1 of the ATmega328P datasheet (yes, I know that's not the OP's processor) you certainly can erase only.

It doesn't look like avr-libc gives an erase only option. If you write 0xFF is it smart enough to do an erase only, and if it's overwriting 0xFF is it smart enough to do a write only?

Use that sketch, but modify it to write 255 instead of zero. It should be clear where to make that change.

Or write your own sketch and have it loop through the available eeprom addresses, reading them, and writing 255 to them. It's nothing magic.

That is assuming it matters.

Assuming 255=blank is an assumption - but it's valid as long as you don't write 255 to it (or they cell you check, at least), and don't upload your code to chips with garbage on their eeprom.

Well I was curious so I wrote a test function.

It seems that the avr-libc library just uses Erase/Write no matter of whether it can get away with just an erase or just a write.

void EEPROM_Data_Test(void)
{
  uint16_t address;
  uint8_t dump_row[16];
  uint8_t idx;
  uint32_t calc_time;

  LT_printf(F("Writing over blank EEPROM with Data\r\n"));
  address = 0;
  calc_time = micros();
  while (address < EEPROM_DATA_EEPROM_SIZE)
  {
    memset(dump_row, address & 0xFF, sizeof(dump_row));
    eeprom_write_block(dump_row, (void*)address, sizeof(dump_row));
    address += sizeof(dump_row);
  }
  calc_time = micros() - calc_time; // Calculate the time it took for function.
  LT_printf(F("Wrote %u bytes in %lu us for a time of %l.3u ms/btye\r\n"), EEPROM_DATA_EEPROM_SIZE, calc_time, (calc_time + (EEPROM_DATA_EEPROM_SIZE >> 1)) / EEPROM_DATA_EEPROM_SIZE);
  //=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  LT_printf(F("Writing over non-blank EEPROM with the Same Data\r\n"));
  address = 0;
  calc_time = micros();
  while (address < EEPROM_DATA_EEPROM_SIZE)
  {
    memset(dump_row, address & 0xFF, sizeof(dump_row));
    eeprom_write_block(dump_row, (void*)address, sizeof(dump_row));
    address += sizeof(dump_row);
  }
  calc_time = micros() - calc_time; // Calculate the time it took for function.
  LT_printf(F("Wrote %u bytes in %lu us for a time of %l.3u ms/btye\r\n"), EEPROM_DATA_EEPROM_SIZE, calc_time, (calc_time + (EEPROM_DATA_EEPROM_SIZE >> 1)) / EEPROM_DATA_EEPROM_SIZE);
  //=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  LT_printf(F("Writing over non-blank EEPROM with Different Data\r\n"));
  address = 0;
  calc_time = micros();
  while (address < EEPROM_DATA_EEPROM_SIZE)
  {
    memset(dump_row, (address + 1) & 0xFF, sizeof(dump_row));
    eeprom_write_block(dump_row, (void*)address, sizeof(dump_row));
    address += sizeof(dump_row);
  }
  calc_time = micros() - calc_time; // Calculate the time it took for function.
  LT_printf(F("Wrote %u bytes in %lu us for a time of %l.3u ms/btye\r\n"), EEPROM_DATA_EEPROM_SIZE, calc_time, (calc_time + (EEPROM_DATA_EEPROM_SIZE >> 1)) / EEPROM_DATA_EEPROM_SIZE);
  //=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  LT_printf(F("Starting EEPROM Erase\r\n"));
  address = 0;
  calc_time = micros();
  while (address < EEPROM_DATA_EEPROM_SIZE)
  {
    memset(dump_row, 0xFF, sizeof(dump_row));
    eeprom_write_block(dump_row, (void*)address, sizeof(dump_row));
    address += sizeof(dump_row);
  }
  calc_time = micros() - calc_time; // Calculate the time it took for function.
  LT_printf(F("Erased %u bytes in %lu us for a time of %l.3u ms/btye\r\n"), EEPROM_DATA_EEPROM_SIZE, calc_time, (calc_time + (EEPROM_DATA_EEPROM_SIZE >> 1)) / EEPROM_DATA_EEPROM_SIZE);
  //=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  LT_printf(F("Starting EEPROM Erase\r\n"));
  address = 0;
  calc_time = micros();
  while (address < EEPROM_DATA_EEPROM_SIZE)
  {
    memset(dump_row, 0xFF, sizeof(dump_row));
    eeprom_write_block(dump_row, (void*)address, sizeof(dump_row));
    address += sizeof(dump_row);
  }
  calc_time = micros() - calc_time; // Calculate the time it took for function.
  LT_printf(F("Erased %u bytes in %lu us for a time of %l.3u ms/btye\r\n"), EEPROM_DATA_EEPROM_SIZE, calc_time, (calc_time + (EEPROM_DATA_EEPROM_SIZE >> 1)) / EEPROM_DATA_EEPROM_SIZE);
}

This is the output from the code:

Writing over blank EEPROM with Data
Wrote 1024 bytes in 3488156 us for a time of 3.406 ms/btye
Writing over non-blank EEPROM with the Same Data
Wrote 1024 bytes in 3488000 us for a time of 3.406 ms/btye
Writing over non-blank EEPROM with Different Data
Wrote 1024 bytes in 3488836 us for a time of 3.407 ms/btye
Starting EEPROM Erase
Erased 1024 bytes in 3489684 us for a time of 3.408 ms/btye
Starting EEPROM Erase
Erased 1024 bytes in 3489668 us for a time of 3.408 ms/btye

So, it takes 3.4ms to do any of these eeprom writes, which according to Table 8-1 must be the Erase and Write Atomic operation.

With how infrequently I need to erase the EEPROM using one write unnecessarily isn’t the end of the world. Since the processor is capable of erasing without writing, however, it’s annoying that it isn’t available through the library.

To the OP: the code that you need to erase your EEPROM can be extracted from what I posted. You obviously don’t need to write three times and then erase twice. I was just doing that to see the the function call behaved any differently when overwriting the same thing

What are you talking about? The processor has no erase capability, and in your code you just use write operations to erase. I don’t see the purpose of all the elaboration. Table 8-1 refers to write times.

The playground code, modified by writing 0xFF’s, is a lot more straightforward, and does exactly the same thing.

The reason the avr library has no erase function, is because the processor doesn’t.

aarg:
What are you talking about? The processor has no erase capability, and in your code you just use write operations to erase. I don't see the purpose of all the elaboration. Table 8-1 refers to write times.

The playground code, modified by writing 0xFF's, is a lot more straightforward, and does exactly the same thing.

The reason the avr library has no erase function, is because the processor doesn't.

Which datasheet are you looking at? Both the ATmega328P (in Table 8-1) and ATmega32U4 (in Table 5-2) list 3 commands for EEPM0 and EEPM1: Write and Erase (3.4ms), Erase Only (1.8ms), and Write Only (1.8s).

The purpose of the code, is to test whether the avr library uses the Erase Only and Write Only commands when it can, or if it always uses the Write and Erase command. Since it always takes 3.4ms/byte to program, that shows it always uses the Write and Erase.

Atmel-2549-8-bit-AVR-Microcontroller-ATmega640-1280-1281-2560-2561_datasheet.pdf

aarg:
Atmel-2549-8-bit-AVR-Microcontroller-ATmega640-1280-1281-2560-2561_datasheet.pdf

OK, in that datasheet it's Table 9-1, but it still has all 3 commands for EEPM0 and EEPM1: Write and Erase (3.4ms), Erase Only (1.8ms), and Write Only (1.8s).

BTW - This 5 minute throttle on my posting is wicked annoying.

Figure 9-1. External Memory with Sector Select

doesn't seem like the right table. But I see I'm looking at the right datasheet for the Mega, not the Micro.

I fixed that problem by downloading all the sheets. I don't know how I got away with this for so long. :slight_smile: I guess there are so many similarities among the processors of that family.

Hmm...when I looked at http://www.atmel.com/images/atmel-2549-8-bit-avr-microcontroller-atmega640-1280-1281-2560-2561_datasheet.pdf, Table 9-1 is "EEPROM Mode Bits."

aarg:

Figure 9-1. External Memory with Sector Select

doesn't seem like the right table. But I see I'm looking at the right datasheet for the Mega, not the Micro.

Table 9-1 on page 35, not Figure 9-1.

BigBobby:
Hmm...when I looked at http://www.atmel.com/images/atmel-2549-8-bit-avr-microcontroller-atmega640-1280-1281-2560-2561_datasheet.pdf, Table 9-1 is "EEPROM Mode Bits."

Yes, that's what I get now. Yet I copied and pasted what you see in reply #13. So I have no idea what happened.

Anyway, perhaps rapid EEPROM erasure is not usually necessary, hence nobody bothered to implement it?

Hi, thanks guys, this is for my motorcycle fuel level driver, supplies a PWM voltage to the fuel gauge from a wheel rotation count from the speedo and gives a fuel level based off the mileage ridden. I will reset it on fuel fill up, and erase the EEPROM, and was wondering if the reset button could be used for that, I will just install a NO push button switch on a pin instead.

aarg:
Yes, that's what I get now. Yet I copied and pasted what you see in reply #13. So I have no idea what happened.

Anyway, perhaps rapid EEPROM erasure is not usually necessary, hence nobody bothered to implement it?

Reply #15 noticed that you were looking at the Figure instead of the Table.

And yes, it seems like the avr libraries didn't implement an erase function and they also didn't cleverly include it in the write function. I acknowledged in my earlier post that the effects aren't catestrophic. Hopefully all this didn't confuse the OP.

I'm from the time when this stuff had to be written yourself, and I certainly would have made my eeprom functions use just erase or write instead of erase/write if they could. 1.7 seconds means a lot on a mfg test fixture if you need to make 100,000 units that month. When you need to perform an operation that takes 3.4ms, it's worth a few instructions to see if you can make it take 1.7ms instead.

I think that I just showed someone with 4000+ posts something he didn't already know about the AVR, however. Do I deserve karma for that?

BigBobby:
Reply #15 noticed that you were looking at the Figure instead of the Table.

And yes, it seems like the avr libraries didn't implement an erase function and they also didn't cleverly include it in the write function. I acknowledged in my earlier post that the effects aren't catestrophic. Hopefully all this didn't confuse the OP.

I'm from the time when this stuff had to be written yourself, and I certainly would have made my eeprom functions use just erase or write instead of erase/write if they could. 1.7 seconds means a lot on a mfg test fixture if you need to make 100,000 units that month. When you need to perform an operation that takes 3.4ms, it's worth a few instructions to see if you can make it take 1.7ms instead.

I think that I just showed someone with 4000+ posts something he didn't already know about the AVR, however. Do I deserve karma for that?

No. It's too easy. I would certainly never claim to know everything about the AVR. I've barely scratched the surface. But avoiding excessive modesty, and having observed this effect in others, once you get to know a lot about something, it's easy to believe that when you think you know something, you must be right. I'm usually wiser over a period of time, and I'll realize I was wrong only while on the way home, or a grain of doubt will send me back to the books or code or whatever to make sure.

We could trade karma, but you missed your chance to see me beg for karma, because yesterday it was 199 and I really wanted to get over the 200 mark. But someone beat you to it.

Anyway, I don't get too uptight about being wrong. I don't want to be just silly, but sometimes it's a stepping stone to finding out something really interesting. Especially in groups.