Esp32 與 Winbond W25Q128JV 串列快閃記憶體

https://forum.arduino.cc/t/esp32-and-winbond-w25q128jv-serial-flash-memory/861315


透過範例程式碼(在訊息末尾新增),它可以在 Arduino Nano IoT 33 上正常工作,但在 esp32 上我看不到檔案或寫入任何內容到晶片。
當傳回能夠寫入的 maxfiles 為 0 時,SerialFlash.create() 函數似乎失敗了。
有什麼想法嗎?
謝謝
這是範例程式碼:


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

const int FlashChipSelect = 4;

void setup(){

  Serial.begin(115200);
  delay(3000);
  Serial.println(“SPI Flash”);
 
  if (!SerialFlash.begin(FlashChipSelect)) {
    Serial.println(“Unable to access SPI Flash chip”);
    while(1){}
  }
 
}

void loop(){

  // MAIN MENU
  Serial.println(“”);
  Serial.println(“”);
  Serial.println(“SerialFlash Read and Write”);
  Serial.println(“(switch your terminal to no line endings)”);
  Serial.println(“————————–“);
  Serial.println(“1) Create a new file”);
  Serial.println(“2) Open a file”);
  Serial.println(“3) Delete a file”);
  Serial.println(“————————–“);
  Serial.println(“Select a number”);
  while(!Serial.available()){}
  char choice = Serial.read();
  while(Serial.available()){Serial.read();}

  switch(choice){

    case ‘1’:
      newFile();
      break;

    case ‘2’:
      openFile();
      break;

    case ‘3’:
      deleteFile();
      break;

    default:
      Serial.println(“Invalid Selection”);
    
  }
 
}

/* Create a new file
 *
 * Request filename up to 20 chars
 * Request a size up to 256 bytes
 * Request some contents
 * Create a file
 */
void newFile(){

  Serial.println(“Enter a filename”); // Request filename from user
  while(!Serial.available()){} // Wait for user

  char filename[20] = {}; // buffer to store user filename
  Serial.readBytesUntil(‘ ‘, filename, 20);
  while(Serial.available()){Serial.read();}

  Serial.println(“Enter a filesize in bytes”); // Request file size from user
  while(!Serial.available()){} // Wait for user

  char sizeArray[3] = {}; // buffer to store requested file size
  Serial.readBytesUntil(‘ ‘, sizeArray, 3);
  while(Serial.available()){Serial.read();}
  int filesize = atoi(sizeArray); // Convert char array to int (i.e. “40” to 40)

  if(SerialFlash.create(filename, filesize)){ // Returns false if file already exists

    SerialFlashFile file; // Open the file we just created for writing
    file = SerialFlash.open(filename);
    Serial.println(“Write some file contents:”); // Request file contents from user
    while(!Serial.available()){} // Wait for user

    char contents[256] = {}; // buffer to store file contents
    Serial.readBytesUntil(255, contents, 256);
    while(Serial.available()){Serial.read();} // Empty read buffer
    file.write(contents, filesize); // Write the contents buffer
    Serial.println(“”);
    Serial.print(“New file “);
    Serial.print(filename);
    Serial.print(” created with size “);
    Serial.print(filesize);
    Serial.println(” bytes!”);
  }else{
    Serial.println(“”);
    Serial.println(“There was an error creating this file (does it already exist?)”);
  }

  return;
 
}

/* Open a file
 *
 * Print the directory listing
 * Request filename up to 20 chars
 * Open file and display contents
 */
void openFile(){

  printDir(); // Function to print the directory listing

  Serial.println(“Enter a filename to OPEN”); // Request file name from user
  Serial.println();
  while(!Serial.available()){} // Wait for user

  char filename[20] = {}; // buffer to store the file name
  Serial.readBytesUntil(‘ ‘, filename, 20);
  while(Serial.available()){Serial.read();}

  Serial.println(filename);

  SerialFlashFile file;
  file = SerialFlash.open(filename); // open the file
  if (file) {
    Serial.print(“File Name: “);
    Serial.println(filename);
    Serial.println();
    Serial.print(“File Size: “);
    Serial.print(file.size());
    Serial.println(” bytes”);
    Serial.println();
    Serial.println(“File Contents:”);
    char buffer[256] = {}; // create a buffer for the file contents
    file.read(buffer, 256); // read file to buffer
    Serial.print(buffer);
  }else{
    Serial.println(“File not found!”);
  }

  return;
 
}

/* Delete a file
 *
 * Print the directory listing
 * Request filename up to 20 chars
 * Delete File
 */
void deleteFile(){

  printDir(); // Function to print the directory listing

  Serial.println(“Enter a filename to DELETE”); // Request file name from user
  while(!Serial.available()){} // Wait for user

  char filename[20] = {}; // buffer to store file name
  Serial.readBytesUntil(‘ ‘, filename, 20);
  while(Serial.available()){Serial.read();}

  SerialFlash.remove(filename); // Delete the file

  return;
 
}

/* Print Directory
 *
 * Print a list of all files on the chip
 * Stolen from SerialFlash library example “ListFiles”
 */
void printDir(){

  Serial.println(“Directory Listing”);
  Serial.println(“—————–“);

  SerialFlash.opendir();
  while (1) {
    char filename[64];
    uint32_t filesize;

    if (SerialFlash.readdir(filename, sizeof(filename), filesize)) {
      Serial.print(”  “);
      Serial.print(filename);
      spaces(20 – strlen(filename));
      Serial.print(”  “);
      Serial.print(filesize);
      Serial.print(” bytes”);
      Serial.println();
    } else {
      break; // no more files
    }
  }
}

void spaces(int num) {
  for (int i=0; i < num; i++) {
    Serial.print(” “);
  }
}

—————————————————————————————————————-

不,如果該程式庫只是使用 Arduino IDE 支援的所有平台的通用功能編寫的,那麼這應該不是問題。我猜你檢查了顯而易見的事情:沒有使用與 Nano IoT 33 相同的文件名,沒有在之前的寫入中填充完整的晶片,等等。如果你……

感謝您的回复,接線是:
ESP32 W25Q128JV
3V3 ——–> 3.3V
PIN4 ——–> CS
PIN18 ——–> CLK
PIN23 — ——> DI
PIN19 ——–> DO
3V3 ——–> WP
3V3 ——–> 保持
接地 ——– > GND
該函式庫是為 arduino 編寫的,這不會造成問題嗎?
謝謝!

—————————————————————————————————————-

SPI 匯流排設計用於連接較短的 PCB。根據我的經驗,只要沒有更大的電噪聲源(pe 馬達)污染環境,SPI 頻率高達 4MHz 左右的麵包板設定就可以非常可靠地工作。您可能會以更高的頻率取得成功,但這可能只是運氣。
ESP(通常,您沒有告訴我們您使用的特定型號)運行在 240MHz 上。 SPI頻率高達50MHz,超過Nano ioT 33的處理器時脈(48MHz)。

—————————————————————————————————————-

我建議在麵包板設定上保持在 4MHz 或以下(假設您使用高品質的麵包板)。
在 PCB 上,您可能會走得更高,如果您正確進行走線佈線,50 MHz 應該不是問題。

多謝!我改成 20Mhz,在麵包板上運作得很好。

You may also like...

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *