ESP32的闪存如何存储数据?

How to store data in flash memory of ESP32?

我正在尝试使用这个 library to store data into the ESP32 flash memory. I am particularly using this example 来写入和读取浮点值。我刚刚在这段代码中添加了一个 Serial.println("error") 如下 -

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);
  }

如果我运行这段代码,打印的错误语句out.i.e.flash.write不工作。我刚刚在页面上看到一些说明,对于 ESP32,需要进行一些修改。 *

"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"

所以我修改了我的代码如下-

#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);
  }

  
}

但是当我这样做时出现以下错误 -

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

这个错误是什么意思?我无法调试它。谁能帮我解决这个问题?

部分 ESP32 集成了闪存。有些使用外部闪存芯片。该闪存包含应用程序固件,上面可能有一个文件系统(通常是 SPIFFS)并且可能有一个键值存储(NVS)。此闪存是 ESP32 上的主要闪存,通常也是唯一的闪存。

您尝试使用的库无法与我上面描述的主闪存一起正常工作。您尝试使用的库需要将辅助闪存芯片连接到 ESP32。如果它确实使用主闪存,它将控制它并干扰 ESP32 运行 其固件的能力。仅当您将辅助闪存连接到 ESP32 时才使用此库。

要使用 ESP32 附带的闪存,请使用 SPIFFS for a filesystem or use Preferences (NVS) 作为键值存储。两者都是 ESP32 的 Arduino 核心的一部分,易于使用且不需要第二个 SPI 闪存芯片。