Search code examples
flashcompiler-errorsesp32

How to store data in flash memory of ESP32?


I am trying to use this library to store data into the ESP32 flash memory. I am particularly using this example to write and read float values. I have just added a Serial.println("error") to this code as follows -

for (uint8_t i = 0; i < arrayLen(floatAddr); i++) 
  {
    if (flash.writeFloat(0x00, testFloat[i])) 
    {
      Serial.print(testFloat[i]);
      Serial.print(" written to 0x");
      Serial.println(floatAddr[i], HEX);
    }
    else
    {
      Serial.println("error");
    }
    
    _float = flash.readFloat(floatAddr[i]);
    Serial.print(_float);
    Serial.print(" read from 0x");
    Serial.println(floatAddr[i], HEX);
  }

If I run this code, the error statement is printed out.i.e.flash.write is not working. I just saw some instructions on the page that for ESP32, some modifications are required. *

"An alternate version SPIFlash flash (SPIPinsArray) of the constructor can be used (only with ESP32 board as of now) to enable the use of custom SPI pins. SPIPinsArray has to be a 4 element array containing the custom SPI pin numbers (as signed integers - int8_t) in the following order - sck, miso, mosi, ss. Also make sure to include flash.begin(CHIPSIZE*) in void setup(). This enables the library to detect the type of flash chip installed and load the right parameters. * Optional"

So I modified my code as follows -

#include<SPIMemory.h>

#if defined(ARDUINO_SAMD_ZERO) && defined(SERIAL_PORT_USBVIRTUAL)
// Required for Serial on Zero based boards
#define Serial SERIAL_PORT_USBVIRTUAL
#endif

#if defined (SIMBLEE)
#define BAUD_RATE 250000
#else
#define BAUD_RATE 115200
#endif

#define arrayLen(x) sizeof(x)/sizeof(x[0])
uint32_t strAddr[3], floatAddr[2], byteAddr[4];
String testStr[] = {
  "Test String 0",
  "Test String 1",
  "Test String 2"
};
float testFloat[] = {
  3.1415, 6.283
};
byte testByte[] = {
  3, 245, 84, 100
};
int8_t SPIPinsArray[] = {18,21,19,5}; //sclk,miso,mosi,ss
//SPIFlash flash(SS1, &SPI1);       //Use this constructor if using an SPI bus other than the default SPI. Only works with chips with more than one hardware SPI bus
//SPIFlash flash;
SPIFlash flash(int8_t *SPIPinsArray);
void getAddresses();
void writeData();
void setup() {
  Serial.begin(BAUD_RATE);
#if defined (ARDUINO_ARCH_SAMD) || (__AVR_ATmega32U4__) || defined(ARCH_STM32)
  while (!Serial) ; // Wait for Serial monitor to open
#endif
  delay(50); //Time to terminal get connected
  Serial.print(F("Initialising Flash memory"));
  for (int i = 0; i < 10; ++i)
  {
    Serial.print(F("."));
  }
  Serial.println();
  flash.begin();
  
  Serial.println();
  Serial.println();

 // getAddresses();
  dataIO();
  //flash.eraseChip();      // Uncomment this if you would like to erase chip
}

void loop() 
{

}



// Function to write data
void dataIO() 
{
  uint8_t _byte;
  float _float;
  String _string;
 

  for (uint8_t i = 0; i < arrayLen(floatAddr); i++) 
  {
    if (flash.writeFloat(0x00, testFloat[i])) 
    {
      Serial.print(testFloat[i]);
      Serial.print(" written to 0x");
      Serial.println(floatAddr[i], HEX);
    }
    else
    {
      Serial.println("error");
    }
    
    _float = flash.readFloat(floatAddr[i]);
    Serial.print(_float);
    Serial.print(" read from 0x");
    Serial.println(floatAddr[i], HEX);
  }

  
}

But I get the following error when I do this -

request for member 'begin' in 'flash', which is of non-class type 'SPIFlash(int8_t*) {aka SPIFlash(signed char*)}' flash.begin;

What does this error mean? I am not able to debug it. Can anyone help me in resolving this issue?


Solution

  • Some ESP32's have integrated flash memory. Some use an external flash chip. This flash holds the application firmware, may have a filesystem on it (usually SPIFFS) and may have a key value store (NVS). This flash is the primary, and usually only, flash memory on an ESP32.

    The library you're trying to use cannot work properly with the primary flash memory I described above. The library you're trying to use would require a secondary flash chip to be connected to the ESP32. If it did use the primary flash memory it would take control of it and interfere with the ESP32's ability to run its firmware. Only use this library if you're connected secondary flash to the ESP32.

    To use the flash storage that comes with the ESP32, either use SPIFFS for a filesystem or use Preferences (NVS) for a key value store. Both are part of the Arduino Core for the ESP32, are easy to use and don't require a second SPI flash chip.