Jim
Jim

Reputation: 43

ESP8266 EEPROM READ/WRITE - Write seems to happen before read of old value

I'm trying to write some code for the the ESP8266-12E that detects initial program load of a new version of the code. For this simplified version of my code (that still exhibits the behavior I'm seeing) there is no code in the loop() section.

I place my current version of the code in a const String pgmVersion. The code reads the EEPROM (actually flash for the ESP8266) and compares it to the current version of the code (pgmVersion). If they are different, then I know that I have a new version of the code. This is then followed by a write to EEPROM to save the current version pgmVersion so that the next time I boot this version will be the old version.

When I run the code with only the for loop for the eeprom read, I can see that the saved version is different than the current version (and can also see when they are the same). This seems to work properly.

However, when I run the full code that includes the write to eeprom, the read for loop always indicates that the saved version matches the current version and does not execute the eeprom write for loop. This happens consistently even when I run with a new value for the current version. This is simply baffling to me. I can remove power and then power up again and the new version data has been saved to eeprom so it seems that it is really being written.

Can anyone see what is wrong with my code or explain why the eeprom seems to be written without going through my eeprom write for loop? I've read lots of posts and online documentation and still can't figure this out.

Jim

#include <EEPROM.h>
const String pgmVersion = "00.04";
void setup() {
  Serial.begin(115200);
  EEPROM.begin(6);
  delay(500);
  char eepData;
  char pgmData;
  bool pgmMatch = true;
  for (unsigned int i = 0; i < pgmVersion.length(); i++) 
  {
    eepData = char(EEPROM.read(i));
    pgmData = pgmVersion.charAt(i);
    Serial.print("eepData = ");
    Serial.println(eepData);
    Serial.print("pgmVersion[i] = ");
    Serial.println(pgmData);
    
    if (eepData == pgmData)
    {
      Serial.println("eepData matches pgmData at index " + String(i));
    } else
    {
      Serial.println("eepData does NOT match pgmData at index " + String(i));
      pgmMatch = false;
    }
  }
    if (!pgmMatch)
    {
      Serial.println("Writing EEPROM");
      for (unsigned int i = 0; i < pgmVersion.length(); i++) 
      {
        pgmData = pgmVersion.charAt(i);
        EEPROM.write(i,pgmData);
        delay(10);
      }
      if (EEPROM.commit()) 
      {
        Serial.println("EEPROM successfully committed");
      } else 
      {
        Serial.println("ERROR! EEPROM commit failed");
      }
    }
}

void loop() {
  // put your main code here, to run repeatedly:
}

Upvotes: 0

Views: 961

Answers (1)

Jim
Jim

Reputation: 43

Ok, I've found out what's going on. The above code fails to work correctly as described in the original post when running under VS Code with PlatformIO. But works as it should when running under the Arduino IDE. (I did not originally post the #include <arduino.h> that is needed in that environment - my mistake!).

When running the code with the write loop included, it looks like the eeprom write gets executed before the write loop itself as when the saved pgm data and new pgm data are known to be different the comparison code says they are the same.

I tried just commenting out the line with the eeprom write for a case when the saved pgm data and new pgm data are known to be different. This resulted in the write loop being entered as it should (meaning it the code detected the saved and new pgm data were not the same).

So it looks like the VS Code version with PlatformIO reorders the code by hoisting the eeprom write somewhere or something with that effect. If that is actually the case, what is needed is a fix to some piece of platform code or some sort of barrier instruction to prevent this from happening. This is unfortunate as I do appreciate the extra function available in the VS Code / PlatformIO environment.

Upvotes: 0

Related Questions