-->
Page 1 of 3

EEPROM library and ESP-01

PostPosted: Wed Mar 23, 2022 5:48 am
by JPM06
Hello, everybody,

I am trying to use the <EEPROM.h> library on an ESP-01, without any success.
My program runs fine on a NodeMCU/ESP12, but on the ESP-01 the data is read as 0 ( not FF!) at each reset or restart.

Is there any mean to write directly in the memory, without using that library? I have to store and retrieve a single byte.

Thanks for any help on the subject.

Re: EEPROM library and ESP-01

PostPosted: Wed Mar 23, 2022 6:48 am
by Inq720
JPM06 wrote:Hello, everybody,

I am trying to use the <EEPROM.h> library on an ESP-01, without any success.
My program runs fine on a NodeMCU/ESP12, but on the ESP-01 the data is read as 0 ( not FF!) at each reset or restart.

Is there any mean to write directly in the memory, without using that library? I have to store and retrieve a single byte.

Thanks for any help on the subject.


I'll write this as if you're a beginner (for future searchers) although I think you are more advanced.

Before jumping off to roll your own flash memory routines...

CHECK
I know the EEPROM library works fine on a 1MB ESP-01. I just looked in my pile and don't have one of the 512K versions to check, but I would think it should work as it was written when the 512K units were the most common ones out there. Looking at the EEPROM code, it should work for the 512K versions as well. Are you sure you're using the correct Flash Size setting?
Untitled.png


OTHER LIBRARIES
There may be libraries on the Arduino Library Manager that might help you... I can't really make a suggestion. I wrote my own for InqPortal.

IF YOU JUST WANT TO MESS WITH FLASH FOR FUN
Flash isn't as easy as memory! You can't just save and re-save just one byte. First you have to allocate an entire 4KB sector. This is because to re-write, you must erase first, and you can only erase a whole sector.
  1. You need to get the Espressif documentation
  2. #include <spi_flash.h>
  3. Functions you'll need are spi_flash_erase_sector, spi_flash_write and spi_flash_read.
  4. You need to pick a sector to work in. EEPROM uses the 4th from the end. Therefore it changes for every memory configuration 512KB, 1MB, 4MB, 16MB. So if you are using a 512K ESP-01, you'd use sector = (512KB / 4KB) - 5 = sector 123.
  5. You should spi_flash_erase_sector the very first install of your Sketch.
  6. Use spi_flash_write to write your data.
  7. Next time you boot up use spi_flash_read to get your value.
  8. Before you write a changed value, you'll need to spi_flash_erase_sector again.
  9. Then spi_flash_write to write your data again.
  10. And just to make it just that bit more "fun", the read and write methods use memory offsets instead of sectors.
  11. All big fun! :shock: :?

Good Luck,
Inq

P.S. In this context: Fun = self abuse.

Re: EEPROM library and ESP-01

PostPosted: Wed Mar 23, 2022 7:15 am
by Pablo2048
Oh come on, don't scary him so much... ;-) Let's make things little bit simpler:
1. JPM06 can you better specify, what are you doing and what are you trying to achieve? Did you try to overwrite the (simulated) eeprom content and don't forget to use .commit() after that. Did this change the eeprom content?
2. you don't have to mess with spi_flash.h stuff - there is API in the ESP object to communicate with the flash (read, write, erase - see https://github.com/esp8266/Arduino/blob ... Esp.h#L172 )
3. you don't have to compute any numbers - they are pre-computed for every memory layout - see (at least) for inspiration here https://github.com/esp8266/Arduino/blob ... EEPROM.cpp
4. because of naturality of flash try to avoid erasing sector on every write, because this way the flash wear out relatively quickly and you are in danger of loosing the data in case of power failure when erasing the sector. Try to develop some kind of simple wear leveling.

Re: EEPROM library and ESP-01

PostPosted: Wed Mar 23, 2022 7:31 am
by Inq720
:lol:
Well... I didn't really try to scare him. I was just trying to kindly nudge him into the first two solutions. If I wanted to really scare him, I'd tell him about wear leveling and how if you write to same spot too many times, your whole ESP will become inoperative. Oh wait... you've taken care of that for me. :lol:

Thank you for the morning laugh.