ESP32 Wrong EEPROM.get value read back from the flash memory

Thread Starter

zazas321

Joined Nov 29, 2015
936
Hello. I have a program where I must save some data to EEPROM before every reset cycle and I sucessfully write 14 bytes like that:

Code:
void write_14_byte_chunks(int eeprom_pointer){

  
  
  if (item_inside.quantity != last_quantity || NEW_SLAVE_ID != SLAVE_ID || strcmp (item_inside.serial,last_serial) != 0){
    Serial.println("There is a mismatch between old and new variables, writing EEPROM");

  int new_eeprom_pointer = (eeprom_pointer*14) ;
  
  if(item_inside.quantity == 0 )
    EEPROM.writeUShort(new_eeprom_pointer, 65535); //This will wryte 2 byte number
  else
    EEPROM.writeUShort(new_eeprom_pointer, item_inside.quantity); //This will wryte 2 byte number
  new_eeprom_pointer += sizeof(unsigned short);// increment addr by 2
    Serial.print("Address after writing quantity=");
    Serial.println(new_eeprom_pointer);
        
  EEPROM.writeString(new_eeprom_pointer, item_inside.serial);// this will write 10 byte string
  new_eeprom_pointer += 10;
  Serial.print("Address after writing Serial=");
  Serial.println(new_eeprom_pointer);




  if(NEW_SLAVE_ID == 0){
    EEPROM.write(new_eeprom_pointer, 65535);
    Serial.println("Writing 0xff to eeprom");
  }
  else{
    EEPROM.write(new_eeprom_pointer, NEW_SLAVE_ID);
    new_eeprom_pointer += sizeof(unsigned short);;// increment addr by 2
    Serial.print("Address after writing slave id=");
    Serial.println(new_eeprom_pointer);
  }
  
  EEPROM.commit();
  }
  else
  Serial.println("No need to write eeprom");

}


I write 3 values to the EEPROM:
quantity - 2 bytes
serial_number -10 bytes
device_id - 2 bytes
Therefore 14 bytes in total

For the following example, I am not writing anything to the first 12 bytes ( quantity and serial_number), but only value 2 to the device_id

1613370545136.png

After writing value 2 to the device id, I can confrim that it is sucesfully written as you can see from above.

After a reset, I must read back the values that I have written, however, I am not able to sucessfully read back the value from the EEPROM:

All I do, is :
EEPROM.get(12,SLAVE_ID);
Serial.print("SLAVE ID after get = ");
Serial.println(SLAVE_ID);

and the result is:
1613372066075.png

If I write value 4, it will read back:
1613372100182.png

Can someone help me understand what is going on here with my EEPROM?
 

Thread Starter

zazas321

Joined Nov 29, 2015
936
I have also developed a program solely to debug this problem:
Code:
/*
   EEPROM Write

   Stores random values into the EEPROM.
   These values will stay in the EEPROM when the board is
   turned off and may be retrieved later by another sketch.
*/

#include "EEPROM.h"

// the current address in the EEPROM (i.e. which byte
// we're going to write to next)



#define EEPROM_SIZE 4080
char serial[10];
int eeprom_pointer=0;
uint16_t slave_id = 0;
uint16_t new_slave_id = 0;




void setup()
{
  Serial.begin(115200);
  Serial.println("start...");
  if (!EEPROM.begin(EEPROM_SIZE))
  {
    Serial.println("failed to initialise EEPROM"); delay(1000000);
  }

strcpy(serial,"12345678");

//clear_all_eeprom();
//read_12_byte_chunks();
//write_12_byte_chunks();

eeprom_pointer = read_14_byte_chunks();
write_14_byte_chunks(eeprom_pointer);
}

void loop()
{
  // need to divide by 4 because analog inputs range from

}



void write_14_byte_chunks(int eeprom_pointer){
  int new_eeprom_pointer = (eeprom_pointer*14) ;
  EEPROM.writeUShort(new_eeprom_pointer, 15); //This will wryte 2 byte number
  new_eeprom_pointer += sizeof(unsigned short);// increment addr by 2
  Serial.print("Quantity written=");
  Serial.println(15);
        
  EEPROM.writeString(new_eeprom_pointer, serial);// this will write 10 byte string
  new_eeprom_pointer += 10;
  Serial.print("Serial written=");
  Serial.println(serial);
  EEPROM.write(new_eeprom_pointer, 5);
  Serial.print("slave id written=5");
  EEPROM.commit();

}


void clear_all_eeprom(){
  for(int i=0; i<EEPROM_SIZE;i++){
    EEPROM.write(i,0xFF);// writing 0xFF to all eeprom positions 
    
  }
  EEPROM.commit();

}


int read_14_byte_chunks(){
  
  for(int j=0 ; j<=200 ; j++){
    Serial.print("NEXT CHUNK = ");
    Serial.println(j);
    int counter = 0;
    for(int i=0 ; i<14 ; i++){
      Serial.print("Value inside EEPROM ");
      Serial.print((j*14)+i);
      Serial.print(" = ");
      Serial.println(EEPROM.read((j*14)+i));
      if(EEPROM.read((j*14)+i) == 0xFF){
        Serial.println("0xFF Detected");
        counter++;
      } 
    }
    if(counter >=14){ //The EEPROM chunk is empty
      Serial.print("Found an empty chunk of EEPROM=");
      Serial.println(j);
      if(j>=1)
        find_last_values((j-1)*14);
      else
        find_last_values(0);
      return j;
    }
  
  }

}

void find_last_values(int last_eeprom_counter){
  uint16_t last_quantity=0;
  char last_serial[10];
  uint16_t last_slave_id1=0;
  Serial.print("Address of last eeprom start=");
  Serial.println(last_eeprom_counter);
  EEPROM.get(last_eeprom_counter,last_quantity);
  last_eeprom_counter += sizeof(unsigned short);
  EEPROM.get(last_eeprom_counter,last_serial);
  last_eeprom_counter += 10;
  EEPROM.get(last_eeprom_counter,last_slave_id1);
  
  Serial.print("Last_quantity = ");
  Serial.println(last_quantity);
  Serial.print("Last_serial = ");
  Serial.println(last_serial);
  Serial.print("last_slave_id1 = ");
  Serial.println(last_slave_id1);
  
  
}

every time i reset the device, I write the same data and progress to the next 14 byte chunk of the EEPROM. After the reset, I return 14bytes back and read the last set values. As you can see I can read back the quantity and serial values correctly but something is not right with the EEPROM and I cannot figure out!. I bet its something so silly and I cannot notice!
 

Thread Starter

zazas321

Joined Nov 29, 2015
936
I found the most ridiculous error:
where I am writing slave ID:


EEPROM.write must be replaced by EEPROM.writeUShort
 

ericgibbs

Joined Jan 29, 2010
18,842
hi zazas.
Similar problem in a test I was running, Line #8.
E
Code:
#include <EEPROM32_Rotate.h>

Serial.print("EEPPROM counter = ");
        Serial.println(eeprom_counter);
        if(eeprom_counter >= 996)
        eeprom_counter = 0;
           
        EEPROM.writeUShort(eeprom_counter, item_inside.quantity);               // 2^16 - 1
        eeprom_counter += sizeof(unsigned short);
        Serial.print("address after quantity=");
        Serial.println(eeprom_counter);
       
        EEPROM.writeString(eeprom_counter, item_inside.serial);
        eeprom_counter = eeprom_counter + 10;
        Serial.print("address after string=");
        Serial.println(eeprom_counter);
       
        //UPDATE THE eeprom counter
        EEPROM.write(0,eeprom_counter);
        EEPROM.commit();
       
        ESP.restart();
 

Thread Starter

zazas321

Joined Nov 29, 2015
936
Everything is good now. When writing to EEPROM need to be very carefull about the type of your variable. if you use EEPROM.writeUShort you must ensure you have declared the variable as uint16_t otherwise otherwise weird things will happen!
 
Top