What am I missing or Did something recently change with the EEPROM.h library? I have a program that I initially developed on an Arduino Nano, but have successfully adapted the code for use on a number of BluePill clones. The code is used to store a handful of integer values in emulated EEPROM, so that the same data is available the next time the device powers on. It has worked fine (on the Bluepills) for several years, but this past week I made some changes to the code (unrelated to EEPROM), and now the EEPROM storage isn't working as I expect.
I have tested out the following EEPROM code which does much the same sort of thing I need working for my main program code:
Code: Select all
#include <EEPROM.h>
int Address0=0;
int Address2=2;
int Address4=4;
int Address6=6;
int Address8=8;
int Address10=10;
int Address12=12;
int TestData=0;
int ClearData=0;
int DisplayData;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
delay(5000);
ClearEEPROM();
DisplayEEPROM();
WriteEEPROMTest1();
DisplayEEPROM();
WriteEEPROMTest2();
DisplayEEPROM();
}
void loop() {
// put your main code here, to run repeatedly:
}
void ClearEEPROM()
{
//Clear EEPROM addresses
Serial.println("-----------------------------------------");
Serial.println("Clearing EEPROM...");
EEPROM.put(Address0,ClearData);
EEPROM.put(Address2,ClearData);
EEPROM.put(Address4,ClearData);
EEPROM.put(Address6,ClearData);
EEPROM.put(Address8,ClearData);
EEPROM.put(Address10,ClearData);
EEPROM.put(Address12,ClearData);
}
void WriteEEPROMTest1()
{
Serial.println("-----------------------------------------");
Serial.println("Writing single digit test values... ");
TestData=1;
EEPROM.put(Address0,TestData);
TestData=2;
EEPROM.put(Address2,TestData);
TestData=3;
EEPROM.put(Address4,TestData);
TestData=4;
EEPROM.put(Address6,TestData);
TestData=5;
EEPROM.put(Address8,TestData);
TestData=6;
EEPROM.put(Address10,TestData);
TestData=7;
EEPROM.put(Address12,TestData);
}
void WriteEEPROMTest2()
{
Serial.println("-----------------------------------------");
Serial.println("Writing 3-digit test values: ");
TestData=101;
EEPROM.put(Address0,TestData);
TestData=202;
EEPROM.put(Address2,TestData);
TestData=303;
EEPROM.put(Address4,TestData);
TestData=404;
EEPROM.put(Address6,TestData);
TestData=505;
EEPROM.put(Address8,TestData);
TestData=606;
EEPROM.put(Address10,TestData);
TestData=707;
EEPROM.put(Address12,TestData);
}
void DisplayEEPROM()
{
Serial.println("-----------------------------------------");
Serial.println("Stored EEPROM Data: ");
EEPROM.get(Address0,DisplayData);
Serial.print("First: ");Serial.println(DisplayData);
EEPROM.get(Address2,DisplayData);
Serial.print("Second: ");Serial.println(DisplayData);
EEPROM.get(Address4,DisplayData);
Serial.print("Third: ");Serial.println(DisplayData);
EEPROM.get(Address6,DisplayData);
Serial.print("Fourth: ");Serial.println(DisplayData);
EEPROM.get(Address8,DisplayData);
Serial.print("Fifth: ");Serial.println(DisplayData);
EEPROM.get(Address10,DisplayData);
Serial.print("Sixth: ");Serial.println(DisplayData);
EEPROM.get(Address12,DisplayData);
Serial.print("Seventh: ");Serial.println(DisplayData);
}
When I run the code on a Nano, I get these expected results:
-----------------------------------------
Clearing EEPROM...
-----------------------------------------
Stored EEPROM Data:
First: 0
Second: 0
Third: 0
Fourth: 0
Fifth: 0
Sixth: 0
Seventh: 0
-----------------------------------------
Writing single digit test values...
-----------------------------------------
Stored EEPROM Data:
First: 1
Second: 2
Third: 3
Fourth: 4
Fifth: 5
Sixth: 6
Seventh: 7
-----------------------------------------
Writing 3-digit test values:
-----------------------------------------
Stored EEPROM Data:
First: 101
Second: 202
Third: 303
Fourth: 404
Fifth: 505
Sixth: 606
Seventh: 707
But on any Bluepill board, I get this instead:
-----------------------------------------
Clearing EEPROM...
-----------------------------------------
Stored EEPROM Data:
First: 0
Second: 0
Third: 0
Fourth: 0
Fifth: 0
Sixth: 0
Seventh: 0
-----------------------------------------
Writing single digit test values...
-----------------------------------------
Stored EEPROM Data:
First: 131073
Second: 196610
Third: 262147
Fourth: 327684
Fifth: 393221
Sixth: 458758
Seventh: 7
-----------------------------------------
Writing 3-digit test values:
-----------------------------------------
Stored EEPROM Data:
First: 13238373
Second: 19857610
Third: 26476847
Fourth: 33096084
Fifth: 39715321
Sixth: 46334558
Seventh: 707
And the data being reported back are the same values regardless of when I run the code or on which Bluepill board. (I.e. whatever values those are they are not random.) It's also odd that the last values that get written (here the 7 and 707) are as expected. For example, if I shorten the code to store only 4 values, then I get 3 garbage numbers and the last value again is correct (would be a 4 and 404 with this code). Why just the last value?
A side question: Is the emulated EEPROM on these STM32 boards subject to the same 100,000 cycle limitation that applies to real EEPROM as in an Arduino? All my testing is being done on new Bluepill boards whose memory has never been stressed in any regard.
Am I doing something wrong here? I notice that the EEPROM.h library was recently updated, but I don't know in what regard, and I have no idea how to troubleshoot a library.
Any suggestions before I move to some really stupid workaround? Anything more I can tell you?
Thanks in advance,
Peter