Skip to content

Use SerialFlash library and mutiple SPI #7866

@savagerex

Description

@savagerex

Board

ESP32 S3

Device Description

ADXL355 Sensor - SPI
external Flash -SPI

Hardware Configuration

NONE

Version

v2.0.5

IDE Name

Arduino IDE

Operating System

Windows 10

Flash frequency

80Mhz

PSRAM enabled

no

Upload speed

921600

Description

ESP32 S3 need two SPI interface.
but i use Serial Flash library.(https://github.com/PaulStoffregen/SerialFlash)
it only support one spi (SPI).

I try to modified the library code.
but ESP32 + External flash, cannot work.

please help me to modified the code from lib(https://github.com/PaulStoffregen/SerialFlash).

Sketch

#include <SerialFlash.h>
#include <SPI.h>

//Buffer sizes
#define USB_BUFFER_SIZE      128
#define FLASH_BUFFER_SIZE    4096

//Max filename length (8.3 plus a null char terminator)
#define FILENAME_STRING_SIZE      13

//State machine
#define STATE_START      0
#define STATE_SIZE      1
#define STATE_CONTENT    2

//Special bytes in the communication protocol
#define BYTE_START      0x7e
#define BYTE_ESCAPE      0x7d
#define BYTE_SEPARATOR    0x7c

#define SensorPowerPin 17
#define led1GPin  41

//SPI Pins (these are the values on the Audio board; change them if you have different ones)
#define MOSI              35
#define MISO              37
#define SCK               36
#define CSPIN             34
//#define CSPIN           21  // Arduino 101 built-in SPI Flash


#define SPI_FREQUENCY           10 * 1000000

//#if CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3
//#define VSPI FSPI
//#endif

//SPIClass SPIPORT(VSPI);

//static SPIClass * SPIPORT = new SPIClass(VSPI);
//SPIClass * vspi = NULL;
//SPIClass * hspi = NULL;

void setup(){
//  vspi = new SPIClass(VSPI);
//  hspi = new SPIClass(HSPI);
  
  Serial.begin(115200);  //Teensy serial is always at full USB speed and buffered... the baud rate here is required but ignored
  pinMode(led1GPin, OUTPUT);
  pinMode(SensorPowerPin, OUTPUT);
  digitalWrite(SensorPowerPin, HIGH);
  delay(15);

  SPI.begin(SCK, MISO, MOSI, CSPIN);
  SPI.setDataMode(SPI_MODE0);
  SPI.setBitOrder(MSBFIRST);
  SPI.setFrequency(SPI_FREQUENCY);

//  SPIPORT->begin(SCK, MISO, MOSI, CSPIN);
//  SPIPORT->setDataMode(SPI_MODE0);
//  SPIPORT->setBitOrder(MSBFIRST);
//  SPIPORT->setFrequency(SPI_FREQUENCY);
  
  
  //Set up SPI
  //SPI.setMOSI(MOSI);  // uncomment these if using the alternate pins
  //SPI.setMISO(MISO);
  //SPI.setSCK(SCK);
  if (!SerialFlash.begin(CSPIN)) {
    while (1) {
      Serial.println(F("Unable to access SPI Flash chip"));
      delay(1000);
    }
  }


  //We start by formatting the flash...
  uint8_t id[5];
  SerialFlash.readID(id);
  SerialFlash.eraseAll();

  delay(1000);

  while (!SerialFlash.ready()) {
    delay(500);
    digitalWrite(led1GPin, HIGH);
    delay(500);
    digitalWrite(led1GPin, LOW);
  }

  SerialFlashFile flashFile;

  //SerialFlash.create(filename, size);
  SerialFlash.create("123.txt", 2560000);
  flashFile = SerialFlash.open("123.txt");

  if (!flashFile) {
    Serial.println("There was an error opening the file for writing");
    return;
  }

  flashFile = SerialFlash.open("123.txt");

  if (!flashFile) {
    Serial.println("There was an error opening the file for writing");
    return;
  }

  uint8_t str[256];

  for(int i = 0; i < 256; i++)
  {
            str[i] = i;   
  }

  for(int j = 0; j < 10000; j++)
  {  
     if(j == 9999)
     {
         str[255] = 44;
     }
     flashFile.write(str, 256);
     
  }
  flashFile.close();

  delay(1000);

  flashFile = SerialFlash.open("123.txt");

      if (!flashFile) {
        Serial.println("There was an error opening the file for writing");
        return;
      }

  uint8_t Readbuffer[256];
  for(int j = 0; j < 10000; j++)
  {  
    flashFile.read(Readbuffer, 256);
    if (j == 9999)
    {
      for(int k = 0; k < 256; k++)
      {
         Serial.println(Readbuffer[k]);
      }
    }
  }    
  flashFile.close();

  
  
}

void loop() {
  // put your main code here, to run repeatedly:

}

Debug Message

NONE

Other Steps to Reproduce

No response

I have checked existing issues, online documentation and the Troubleshooting Guide

  • I confirm I have checked existing issues, online documentation and Troubleshooting guide.

Metadata

Metadata

Assignees

Labels

Area: LibrariesIssue is related to Library support.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions