Saving data to flash memory in wrong address

_ac88_
Posts: 2
Joined: Thu Jun 16, 2022 6:11 pm

Saving data to flash memory in wrong address

Postby _ac88_ » Thu Jun 16, 2022 6:14 pm

Hello.

I'm saving some data (strings and integers) in flash memory of my ESP32 (and it works well) and I have the following addresse's to the respective data:

Code: Select all

#define MAX_EEPROM_LEN 100
#define EEPROM_LOC1 100
#define EEPROM_LOC2 200
#define EEPROM_LOC3 300
#define EEPROM_LOC4 400
#define EEPROM_LOC5 500
#define EEPROM_LOC6 600
#define EEPROM_LOC7 700
#define EEPROM_LOC8 800
#define EEPROM_LOC9 900
#define EEPROM_LOC10 1000
Now, I'm trying to insert more 2 new values in 2 different locations, so I added this two lines

Code: Select all

#define EEPROM_LOC11 1100
#define EEPROM_LOC12 1200
The problem is that with this address location (1100 and 1200) the data is inserting in the address of EEPROM_LOC1 and EEPROM_LOC2, they are not on correct places.

I've tried to reduce the space between memory addresses, but it doesn't work anyway, it inserts in random slots that have already other data.

I found this :
With the ESP32 and the EEPROM library you can use up to 512 bytes in the flash memory. This means you have 512 different addresses, and you can save a value between 0 and 255 in each address position.

So... if I can use 512 different addresses, what is going wrong? :/

ESP_Sprite
Posts: 9050
Joined: Thu Nov 26, 2015 4:08 am

Re: Saving data to flash memory in wrong address

Postby ESP_Sprite » Fri Jun 17, 2022 1:46 am

The way addresses work is that they can only store one byte per address. If you want to store larger things, they'll occupy multiple bytes; e.g if I were to store "Hello" to address 32, it would occupy address 32 to 38 (5 bytes for the string, one for the terminating null).

You probably want to use the Preferences library instead; it's newer and aligns with how you're trying to use it better.

lbernstone
Posts: 671
Joined: Mon Jul 22, 2019 3:20 pm

Re: Saving data to flash memory in wrong address

Postby lbernstone » Fri Jun 17, 2022 4:46 am

The quote above is incorrect. The EEPROM is limited only by the size of your nvs partition, so you can easily store 2KB in there if you choose. You set the size when you call EEPROM.begin(size). AFAICT, all the write methods have bounds checking, so I suspect the issue is in your code, which you have not provided. If you use a uint8_t (unsigned short) to store your address, it will wrap around after 255. That is the only way I can see how data would end up in an incorrect location.
That said, EEPROM is a poor choice for new code that will only be run on a esp32. It is provided to allow libraries that are already using EEPROM internally to be ported to esp32. The Preferences library is much more robust and easy to use.

_ac88_
Posts: 2
Joined: Thu Jun 16, 2022 6:11 pm

Re: Saving data to flash memory in wrong address

Postby _ac88_ » Fri Jun 17, 2022 2:05 pm

lbernstone wrote:
Fri Jun 17, 2022 4:46 am
The quote above is incorrect. The EEPROM is limited only by the size of your nvs partition, so you can easily store 2KB in there if you choose. You set the size when you call EEPROM.begin(size). AFAICT, all the write methods have bounds checking, so I suspect the issue is in your code, which you have not provided. If you use a uint8_t (unsigned short) to store your address, it will wrap around after 255. That is the only way I can see how data would end up in an incorrect location.
That said, EEPROM is a poor choice for new code that will only be run on a esp32. It is provided to allow libraries that are already using EEPROM internally to be ported to esp32. The Preferences library is much more robust and easy to use.
I initialize the EEPROM.begin(size) with 4096.
I use a method that only save strings to the eeprom, this one:

Code: Select all

void writeStringEEPROM(char add, String data) {
  int _size = data.length();
  if (_size > MAX_EEPROM_LEN)
    return;
  int i;
  for (i = 0; i < _size; i++) {
    EEPROM.write(add + i, data[i]);
  }
  EEPROM.write(add + _size, '\0');  //Add termination null character for String Data
  EEPROM.commit();
}
However I'll look at Preferences library.

lbernstone
Posts: 671
Joined: Mon Jul 22, 2019 3:20 pm

Re: Saving data to flash memory in wrong address

Postby lbernstone » Sat Jun 18, 2022 4:22 am

char is an unsigned 8-bit, typically uses to represent a character. Why are you using the char type for something that is obviously a number?

Who is online

Users browsing this forum: Bing [Bot] and 100 guests