ESP_EEPROM is data persistent when flashing new rev or new program?

I am using ESP-12F (ESP8266) to which I have connected a touch screen. I will have many copies of this project. The touch screen needs four "calibration constants", which I assume will be different for each copy. Thus, each copy needs to be calibrated. I would like to store these constants in EEPROM as opposed to modifying the program for each copy. Does the location of the "ESP_EEPROM" change each time a new program uploaded to the device?

I envision a calibration program being uploaded and run and having the results stored in EEPROM and then the operational program upload, reading this data. Possibly, I could embed the calibration program into the operational program and store the data in EEPROM and reading it back when there is a power failure. But what happens when I have a new revision of the operational program? Will I need to recalibrate every copy?

Thanks for your help and consideration.

OSD

This will help explain what is going on, there is no EEPROM, but... The ESP8266 doesn't have genuine EEPROM memory, so it emulates it using a section of flash memory. The amount of flash memory varies by model, from 512KiB to 4MiB. For example, the esp-01 has 512KiB or 1MiB of flash memory, and the esp-12f has 4MiB.

To save changed EEPROM data, the sector needs to be re-flashed. The EEPROM can handle 100,000 write/erase cycles for each position. However, reads are unlimited.

To read data from the EEPROM, you can use the EEPROM.read() function. The only limitation is that it can only read one byte of data at a time.

You need to add this line: EEPROM.commit(); That causes the data to be written in FLASH, after that it will remember even without power.

It is a fun part enjoy it!

Indeed this is a great answer, but it wasn't the one I was seeking.

This emulated EEPROM is flash memory, what I understand to be program memory. When a program is put into the ESP8266 it goes into program memory, which, if I understand correctly is flash memory. Therefore a program could overwrite the emulated EEPROM. Is the emulated EEPROM in the exact same physical location every program that has "EEPROM.begin(n);"? What if a program does not have the EEPROM library nor EEPROM.begin()? Is the emulated EEPROM replaced? Or is the same physical part of flash (program) memory always reserved for EEPROM?

What happens if you program the device with Tasmota and then come back to the Arduino IDE? Do all development systems reserve the exact same physical memory for EEPROM?

I hope you see my confusion and that you can lift the vail of mystery.

One last thing, is this documented by Espressif?

I do appreciate your reply

OSD

FYI: An ESP8266 uses external flash memory (i.e. it is not built into the ESP8266) , which typically has 512K, 1MB or 4MB of external memory depending on the board chosen.

The RAM in an ESP8266 is fixed ... typically after loading the Espressif SDK ready for WiFi and TCP/IP, there is commonly about 40K of RAM remaining for applications. It has 32 KiB instruction RAM . 32 KiB instruction cache RAM . 80 KiB user-data RAM . 16 KiB ETS system-data RAM . External QSPI flash: up to 16 MiB is supported (512 KiB to 4 MiB typically included) .

Simply put it forgets everything when it loses power. It is up to the hardware and software design to protect everything. When it is reset it loads from external memory, that is why some of the "protected" GPIOs are restricted during boot. Changing there level will cause misloads from the external memory.,

For my projects, I use a DS3231 RTC which has a 32kb eeprom. I use it with the uEEPROMLib library to read and wtite on it.

Probably not that is set up for I2C and the ESP is looking for SPI memory. Check the data to be sure I did not miss anything. That is 32K bits or 4K bytes if it is the module I am thinking of. Without you posting the links to modules with technical information that is a best guess.

Sorry about that, it is not 32kb but 32kB...

Thank you @gilshultz. I am using the ESP-12F which has 32Mbit (4MB) SPI flash.

My research leads me to believe that the emulated EEPROM is always in the same physical place in the external flash and that the Arduino IDE respects this location. But my experience leads me to doubt my research.

I do not know how to read the source code for ESP_EEPROM from which I draw my conclusions.

The comment (line 115) * Create an instance of the EEPROM class based on the default EEPROM flash sector. implies, by the use of the word default, that the location of the EEPROM has been specified in some document. While I don't understand the following code, I believe that it does create the emulated EEPROM in a fixed known location:

EEPROMClass::EEPROMClass(void) :
		_sector((((uint32_t) & _FS_end - 0x40200000) / SPI_FLASH_SEC_SIZE)), _data(
				0), _size(0), _bitmapSize(0), _bitmap(0), _offset(0), _dirty(
				false) {
}

(_FS_end is found in an unknown external C program while spi_flash_read and SPI_FLASH_SEC_SIZE are found in the Esprssif SDK)

Further, because it is the default EEPROM flash sector, I assume that the Arduino IDE respects that location.

But I don't know for certain and I'm hoping that one more knowledgeable than I can confirm or correct this.

From the topic name the question was, can you upload another sketch and maintain the settings in the (flash)EEPROM, Yes you can, there is a setting under TOOLS->Erase flash, where the options are

  • Sketch only
  • Sketch and Wifi Settings
  • All Sketch contents.

Obviously you need to select 'Sketch Only' , which is the default.

Thanks @Deva_Rishi ! This led me to the document on the Arduino/ESP8266 where I found the memory map of the flash. Indeed there is a specific area for the emulated EEPROM!

Whew!

-OSD

1 Like

The details of where the eeprom start is determined in the Arduino core for the 8266 can be found in

https://github.com/esp8266/Arduino/blob/master/tools/boards.txt.py

def flash_map (flashsize_kb, fs_kb = 0, conf_name = ''):

    # mapping:
    # flash | reserved | empty | spiffs | eeprom | rf-cal | sdk-wifi-settings

    spi = 0x40200000 # https://github.com/esp8266/esp8266-wiki/wiki/Memory-Map

    reserved = 4112
    eeprom_size_kb = 4
    rfcal_size_kb = 4
    sdkwifi_size_kb = 12
    fs_end = (flashsize_kb - sdkwifi_size_kb - rfcal_size_kb - eeprom_size_kb) * 1024

    # For legacy reasons (#6531), the EEPROM sector needs to be at the old
    # FS_end calculated without regards to block size
    eeprom_start = fs_end

It's also shown here
https://github.com/esp8266/Arduino/blob/master/cores/esp8266/FlashMap.h

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.