Read String from EEPROM on STM32F411

Post here first, or if you can't find a relevant section!
Post Reply
InXoconochtli
Posts: 6
Joined: Tue Feb 23, 2021 12:54 am

Read String from EEPROM on STM32F411

Post by InXoconochtli »

I'm using this code to burn the initial configuration for my peristaltic pump which my main program uses when loading up. It includes Khoi Hoang's library.

Code: Select all

#include <FlashStorage_STM32.h>

  int i;
  int currentConfig = 1;
  bool saveCheck = false; 
  bool flagDir = false;
  float calibration = 1.8251;
  float flowLow = 20;
  float flowHigh = 150;
  String volString = "mL";
  String timeString = "min";
  float doseLow = 2;
  float doseHigh = 100;
  String doseString = "mL";
  int baudRate = 115200;

  int currentConfigRead;
  bool saveCheckRead;
  bool flagDirRead;
  float calibrationRead;
  float flowLowRead;
  float flowHighRead;
  String volStringRead;
  String timeStringRead;
  float doseLowRead;
  float doseHighRead;
  String doseStringRead;
  int baudRateRead;

void setup()
{
  Serial.begin(115200);
  while (!Serial);

  delay(200);

  Serial.print(F("\nStart EEPROM_put on ")); Serial.println(BOARD_NAME);
  Serial.println(FLASH_STORAGE_STM32_VERSION);

  Serial.print("EEPROM length: ");
  Serial.println(EEPROM.length());
  
  //One simple call, with the address first and the object second.
  EEPROM.put(0, currentConfig);
  Serial.println("Config number established");
  EEPROM.put(4, true);
  Serial.println("saveCheck 1 written as true.");
  for (i = 1; i < 10; i++){
    EEPROM.put(4 + i * 62, saveCheck);
    Serial.print("saveCheck ");
    Serial.print(i + 1);
    Serial.println(" written as false.");
  }
  EEPROM.put(5, flagDir);
  Serial.println("flagDir written.");
  EEPROM.put(6, calibration);
  Serial.println("calibration written.");
  EEPROM.put(10, flowLow);
  Serial.println("flowLow written.");
  EEPROM.put(14, flowHigh);
  Serial.println("flowHigh written.");
  EEPROM.put(18, volString);
  Serial.println("volString written.");
  EEPROM.put(30, timeString);
  Serial.println("timeString written.");
  EEPROM.put(42, doseLow);
  Serial.println("doseLow written.");
  EEPROM.put(46, doseHigh);
  Serial.println("doseHigh written.");
  EEPROM.put(50, doseString);
  Serial.println("doseString written.");
  EEPROM.put(62, baudRate);
  Serial.println("baudRate written.");

  Serial.println("EEPROM initialized");
  Serial.print("Current config is: ");
  Serial.println(EEPROM.get(0, currentConfigRead));
  for (i = 0; i < 10; i++){
    EEPROM.get(4 + i * 62, saveCheckRead);
    Serial.print("saveCheck ");
    Serial.print(i + 1);
    Serial.print(" = ");
    Serial.println(saveCheckRead);
  }
  Serial.println("The predefined values burned were:");
  Serial.print("Roration: ");
  if (EEPROM.get(5, flagDirRead) == false) {
    Serial.println("clockwise");
  }
  else if (EEPROM.get(5, flagDirRead) == true) {
    Serial.println("counterclockwise");
  }
  Serial.print("Calibration constant: ");
  Serial.print(EEPROM.get(6, calibrationRead));
  Serial.println(" mL/revolution.");
  Serial.print("Pumping limits: ");
  Serial.print(EEPROM.get(10, flowLowRead));
  Serial.print(" - ");
  Serial.print(EEPROM.get(14, flowHighRead));
  Serial.print(" ");
  Serial.print(EEPROM.get(18, volStringRead));
  Serial.print("/");
  Serial.println(EEPROM.get(30, timeStringRead));
  Serial.print("Dosing range: ");
  Serial.print(EEPROM.get(42, doseLowRead));
  Serial.print(" - ");
  Serial.print(EEPROM.get(46, doseHighRead));
  Serial.print(" ");
  Serial.println(EEPROM.get(50, doseStringRead));
  Serial.print("Baud rate: ");
  Serial.println(EEPROM.get(62, baudRateRead));
  /** Put is designed for use with custom structures also. **/

}

void loop()
{
  /* Empty loop */
}
I can succesfully read all the configuration in my main program, except for the Strings written. When running the above code, all the Strings are read fine, but running essentially the same code, but without the .put section, such as the one below, I cannot longer access the Strings. Any idea why?

Code: Select all

#include <FlashStorage_STM32.h>

  int i;

  int currentConfigRead;
  bool saveCheckRead;
  bool flagDirRead;
  float calibrationRead;
  float flowLowRead;
  float flowHighRead;
  String volStringRead;
  String timeStringRead;
  float doseLowRead;
  float doseHighRead;
  String doseStringRead;
  int baudRateRead;

void setup()
{
  Serial.begin(115200);
  while (!Serial);

  delay(200);

  Serial.print(F("\nStart EEPROM_put on ")); Serial.println(BOARD_NAME);
  Serial.println(FLASH_STORAGE_STM32_VERSION);

  Serial.print("EEPROM length: ");
  Serial.println(EEPROM.length());
  
   Serial.println("EEPROM initialized");
  Serial.print("Current config is: ");
  Serial.println(EEPROM.get(0, currentConfigRead));
  for (i = 0; i < 10; i++){
    EEPROM.get(4 + i * 62, saveCheckRead);
    Serial.print("saveCheck ");
    Serial.print(i + 1);
    Serial.print(" = ");
    Serial.println(saveCheckRead);
  }
  Serial.println("The predefined values burned were:");
  Serial.print("Roration: ");
  if (EEPROM.get(5, flagDirRead) == false) {
    Serial.println("clockwise");
  }
  else if (EEPROM.get(5, flagDirRead) == true) {
    Serial.println("counterclockwise");
  }
  Serial.print("Calibration constant: ");
  Serial.print(EEPROM.get(6, calibrationRead));
  Serial.println(" mL/revolution.");
  Serial.print("Pumping limits: ");
  Serial.print(EEPROM.get(10, flowLowRead));
  Serial.print(" - ");
  Serial.print(EEPROM.get(14, flowHighRead));
  Serial.print(" ");
  Serial.print(EEPROM.get(18, volStringRead));
  Serial.print("/");
  Serial.println(EEPROM.get(30, timeStringRead));
  Serial.print("Dosing range: ");
  Serial.print(EEPROM.get(42, doseLowRead));
  Serial.print(" - ");
  Serial.print(EEPROM.get(46, doseHighRead));
  Serial.print(" ");
  Serial.println(EEPROM.get(50, doseStringRead));
  Serial.print("Baud rate: ");
  Serial.println(EEPROM.get(62, baudRateRead));
  /** Put is designed for use with custom structures also. **/

}

void loop()
{
  /* Empty loop */
}
by mlundin » Fri Mar 12, 2021 3:36 pm
Storing a String object with EEPROM.put will store the String object, but it will not store the character array.
If you try it with put and get in the same code, you save and restore the String object and the string data is already in memory so it seems to work.

So basically you must save and restore the character array that your String objects contains, one example of how this is done can be found in
https://roboticsbackend.com/arduino-wri ... in-eeprom/.

Otherwise use char[12] instead of string, sine you allocate 12 bytes in EEPROM for your strings.
Go to full post
mlundin
Posts: 94
Joined: Wed Nov 04, 2020 1:20 pm
Answers: 6
Location: Sweden

Re: Read String from EEPROM on STM32F411

Post by mlundin »

Storing a String object with EEPROM.put will store the String object, but it will not store the character array.
If you try it with put and get in the same code, you save and restore the String object and the string data is already in memory so it seems to work.

So basically you must save and restore the character array that your String objects contains, one example of how this is done can be found in
https://roboticsbackend.com/arduino-wri ... in-eeprom/.

Otherwise use char[12] instead of string, sine you allocate 12 bytes in EEPROM for your strings.
InXoconochtli
Posts: 6
Joined: Tue Feb 23, 2021 12:54 am

Re: Read String from EEPROM on STM32F411

Post by InXoconochtli »

Thanks mlundin! Just what I was looking for.
mlundin
Posts: 94
Joined: Wed Nov 04, 2020 1:20 pm
Answers: 6
Location: Sweden

Re: Read String from EEPROM on STM32F411

Post by mlundin »

There are some deep issues with saving and restoring objects, for info search for "object serialization". For instance the data in a String object ( from WString.h ) is

Code: Select all

    char *buffer;         // the actual char array
    unsigned int capacity;  // the array length minus one (for the '\0')
    unsigned int len;       // the String length (not counting the '\0')
as you can see the actual text data is stored somewhere else i a memalloced buffer, and the String object only has the reference, the memory address, of this particular instance of string character data. Saving the String object only saves the data structure above, and restoring it restores the identical memory position for char * buffer, that now most surely is not a memalloced buffer of the same string data as the object contained before.
Post Reply

Return to “General discussion”