FlashStorage STM32 - How to save, update and read data for beginner

Post here first, or if you can't find a relevant section!
Post Reply
nawasaqi
Posts: 20
Joined: Wed Dec 22, 2021 7:06 pm

FlashStorage STM32 - How to save, update and read data for beginner

Post by nawasaqi »

Hello again everyone
I have my age and I have a hard time absorbing knowledge. And quite a lot of things I don't understand yet. Forgive my questions, maybe simple for some people, but I never studied programming or electronics in school;)
But to the point. I found a library
https://github.com/khoih-prog/FlashStorage_STM32

And I would like to use it, I would like to read the position from gps and save it to the emulated eeprom memory.
For this example, I hard-coded the coordinates of three capital cities.

My question is. I have a Stm32F411 and I would like it to write data to my memory. What value should I enter for my STM32 in this field?
const int WRITTEN_SIGNATURE = 0xBEEFDEED; Suppose I would like 50% for the code and the other 50% for "eeprom".

The second question is how to correctly save these three state capitals to my eeprom virtual memory.

I used the example from put () but maybe just write () is enough ??
https://github.com/khoih-prog/FlashStor ... _write.ino

Ultimately, I would like to have 10 GPS points to be able to save and read them.

Thank you in advance for your help

My anti-code ;)

Code: Select all

// Demonstrate how to use FlashStorage_STM32 with an API that is similar to the EEPROM library to Store and retrieve structured data.

#include <FlashStorage_STM32.h>

const int WRITTEN_SIGNATURE = 0xBEEFDEED;

        float lat1 = 52,211233;
        float lon1 = 4,532122;
        float lat2 = 38,024321; 
        float lon2 = 23,443456;
        float lat3 = 38,024321; 
        float lon3 = 23,443456;

// Create a structure that is big enough to contain a lat
// and a long. The "valid" variable is set to "true" once
// the structure is filled with actual data for the first time.
typedef struct
{
  char lat[10];
  char lon[10];
} Capitals;

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

  delay(200);

  // Check signature at address 0
  int signature;

  // Create a "Capitals" variable and call it "owner"
  uint16_t storedAddress = 0;
  Capitals owner;

  EEPROM.get(storedAddress, signature);

  // If the EEPROM is empty then no WRITTEN_SIGNATURE
  if (signature != WRITTEN_SIGNATURE)
  {
    Serial.println("EEPROM is empty, writing WRITTEN_SIGNATURE and some example data:");

    EEPROM.put(storedAddress, WRITTEN_SIGNATURE);

    // ...in this case we ask for user data.
    Serial.setTimeout(30000);
    Serial.print("Insert your lat : ");
    String lat = Serial.readStringUntil('\n');
    Serial.println(lat);
    Serial.print("Insert your long : ");
    String long = Serial.readStringUntil('\n');
    Serial.println(long);

    // Fill the "owner" structure with the data entered by the user...
    lat.toCharArray(owner.lat, 100);
    long.toCharArray(owner.long, 100);

    // ...and finally save everything into emulated-EEPROM
    EEPROM.put(storedAddress + sizeof(signature), owner);

    // Print a confirmation of the data inserted.
    Serial.print("<< Your lat: "); Serial.print(owner.lat);
    Serial.print(". Your long: "); Serial.print(owner.long);
    Serial.println(" >> have been saved. Thank you!");
  }
}

void loop()
{
  // Do nothing...
}
GonzoG
Posts: 403
Joined: Wed Jan 15, 2020 11:30 am
Answers: 27
Location: Prudnik, Poland

Re: FlashStorage STM32 - How to save, update and read data for beginner

Post by GonzoG »

You don't need any additional library. Virtual EEPROM is in STM32 core and it's written to work like standard Arduino EEPROM library: https://www.arduino.cc/en/Reference/EEPROM

For variables/objects bigger than 1B (8b) you need to use put()/get() methods.
write() / read() work only for 1B of data.
User avatar
Greirat
Posts: 1
Joined: Thu May 27, 2021 2:54 pm

Re: FlashStorage STM32 - How to save, update and read data for beginner

Post by Greirat »

The WRITTEN_SIGNATURE is just a value you can use to check if the EEPROM has been initialized or not. You can set it to any 32-bit integer value you like. It's used as a marker to determine whether CRM data enrichment has been written to the emulated EEPROM before or not. The value 0xBEEFDEED is just a standard hexadecimal marker value, but you can use any other value you prefer.
In your code, you've defined GPS coordinates for three capital cities. If you want to save these coordinates using the FlashStorage_STM32 library, you can follow these steps:

a. Include the FlashStorage_STM32.h library at the top of your sketch.

b. Define a structure to hold your GPS coordinates, similar to what you've done with the Capitals structure.
typedef struct {
float latitude;
float longitude;
} GPSData;

c. Create instances of the FlashStorage class for each set of GPS coordinates you want to store. For example:
FlashStorage(GPSData, GPS1);
FlashStorage(GPSData, GPS2);
FlashStorage(GPSData, GPS3);

d. In your setup function, you can save the GPS coordinates as follows:
void setup() {
// Initialize Serial and other setup code...

// Define GPS data for the three cities
GPSData city1 = {52.211233, 4.532122};
GPSData city2 = {38.024321, 23.443456};
GPSData city3 = {38.024321, 23.443456};

// Save the GPS data to emulated EEPROM
GPS1.write(city1);
GPS2.write(city2);
GPS3.write(city3);
}

e. You can later read these GPS coordinates from emulated EEPROM using the read method and use them in your application.
GPSData retrievedCity1 = GPS1.read();
GPSData retrievedCity2 = GPS2.read();
GPSData retrievedCity3 = GPS3.read();

These steps allow you to efficiently store and retrieve GPS data using the FlashStorage_STM32 library for your STM32-based application.
User avatar
ManX84
Posts: 20
Joined: Tue Oct 17, 2023 3:30 pm

Re: FlashStorage STM32 - How to save, update and read data for beginner

Post by ManX84 »

@Greirat : Sorry but the FlashStorage_STM32 not working like you describe ! you describe the use of the "original" FlashStorage from cmaglie ! BUT the FlashStorage_STM32 is not a clone! it's just implement the eeprom like API

So please test the code you gave before answered, especially for beginner question
Exposing your opinion is good, exposing your code is better! :mrgreen:
Post Reply

Return to “General discussion”