STM32 - Read Write Flash Memory
The Flash is one type of Non-Volatile Memories(NVM), which means the data stored in it doesn’t get lost even after the board is turned off.
Working with flash memory on STM32 micro-controllers using the Arduino framework involves utilizing STM32 specific libraries and functions tailored to STM32's architecture. Unlike ESP32, which has built-in EEPROM-like flash storage, STM32 typically utilizes the built-in Flash memory for program storage, not for general data storage like EEPROM. However, STM32 micro-controllers do offer options to write and read data to/from specific Flash memory sectors.
Prerequisites:
Arduino IDE Setup for STM32 - Follow the steps to set up the STM32 boards in Arduino IDE. Instructions may vary depending on the specific STM32 board.
STM32Cube Library - it includes the necessary libraries and examples to work with STM32 micro-controllers.
Step 1: Connect Board to Laptop
Step 2: ESP32 EEPROM size Example Code
Select boar ESP32 DEV Module from Tools >> Boards menu, then select appropriate com port. Upload this program to ESP32.
#include <Arduino.h>
#include <STM32Flash.h>
// Define the flash sector to use - adjust according to your STM32 board
#define FLASH_SECTOR FLASH_SECTOR_7
// Data structure to store in flash
struct StoredData {
uint32_t value1;
float value2;
};
// Address in flash memory where the data will be stored
#define DATA_ADDRESS 0x08060000 // Example address in Sector 7
void setup() {
Serial.begin(115200);
while (!Serial);
// Initialize STM32Flash library
STM32Flash.begin();
// Example data to store in flash memory
StoredData dataToStore = {
.value1 = 123,
.value2 = 456.789
};
// Write data to flash memory
STM32Flash.write(DATA_ADDRESS, (uint8_t*)&dataToStore, sizeof(dataToStore));
Serial.println("Data written to flash memory.");
// Read data from flash memory
StoredData readData;
STM32Flash.read(DATA_ADDRESS, (uint8_t*)&readData, sizeof(readData));
// Print read data
Serial.print("Read value1: ");
Serial.println(readData.value1);
Serial.print("Read value2: ");
Serial.println(readData.value2);
}
void loop() {
// Your code here
}