ESP32-S3 SD NAND openNextFile returning on results

frankcohen
Posts: 25
Joined: Mon Apr 05, 2021 4:03 am

ESP32-S3 SD NAND openNextFile returning on results

Postby frankcohen » Tue Oct 31, 2023 7:29 am

I'm running the SD example code on an ESP32-S3 dev module and I am getting no return content when using openNextFile(). I am building with Arduino IDE 2.2.1 and ESP32 v 2.0.14. When I call openNextFile() I get no results.

Here is the code:

Code: Select all

#include <Arduino.h>
#include "FS.h"
#include <LittleFS.h>
#include "SPI.h"

#define SPI_SCK       36
#define SPI_MISO      37
#define SPI_MOSI      35
#define SD_CS         15

// Display
#define Display_SPI_DC    5
#define Display_SPI_CS    12
#define Display_SPI_RST   0
#define Display_SPI_BK    6

boolean listDir(fs::FS &fs, const char * dirname, uint8_t levels, bool monitor){
    Serial.printf("Listing directory: %s\n", dirname);

    File root = fs.open(dirname);
    if(!root){
        Serial.print( F( "Failed to open directory w: " ) );
        Serial.println( dirname );
        return false;
    }
    if(!root.isDirectory()){
        Serial.println( F( "Not a directory" ) );
        return false;
    }

    Serial.print( "root = " );
    Serial.println( root.path() );

    File file = root.openNextFile();

    Serial.print( "file = " );
    Serial.println( file );
    Serial.print( "file.path = " );
    Serial.println( file.path() );

    while ( file )
    {
        if ( file.isDirectory() )
        {
          if ( monitor )
          {
            Serial.print( F( "  DIR : " ) );
            Serial.println(file.name());
          }
          if(levels)
          {
            String fname2 = file.name();
            String fname3 = "/" + fname2;
            listDir(fs, fname3.c_str(), levels -1, monitor);
          }
        } 
        else
        {
          if ( monitor )
          {
            Serial.print("  FILE: ");
            Serial.print(file.name());
            Serial.print("  SIZE: ");
            Serial.println(file.size());
          }
        }
        file = root.openNextFile();
    }

    root.close();
    return true;
}

void setup(){
  Serial.begin(115200);
  delay(1000);
  Serial.println("Starting");

  SPI.begin(SPI_SCK, SPI_MISO, SPI_MOSI, -1);  
  delay(1000);

  pinMode(SD_CS, OUTPUT );      // Turn power on to NAND
  digitalWrite(SD_CS, LOW);


  pinMode(Display_SPI_CS, OUTPUT);
  digitalWrite(Display_SPI_CS, LOW);

  pinMode(Display_SPI_DC, OUTPUT);
  digitalWrite(Display_SPI_DC, HIGH);

  pinMode(Display_SPI_RST, OUTPUT);
  digitalWrite(Display_SPI_RST, HIGH);

  pinMode(Display_SPI_BK, OUTPUT);
  digitalWrite(Display_SPI_BK, LOW);

  delay(1000);

  if ( !SD.begin( SD_CS ) )
  {
      Serial.println("Card Mount Failed");
      return;
  }

  uint8_t cardType = SD.cardType();

  if(cardType == CARD_NONE){
      Serial.println("No SD card attached");
      return;
  }

  Serial.print("SD Card Type: ");
  if(cardType == CARD_MMC){
      Serial.println("MMC");
  } else if(cardType == CARD_SD){
      Serial.println("SDSC");
  } else if(cardType == CARD_SDHC){
      Serial.println("SDHC");
  } else {
      Serial.println("UNKNOWN");
  }

  uint64_t cardSize = SD.cardSize() / (1024 * 1024);
  Serial.printf("SD Card Size: %lluMB\n", cardSize);

  listDir(SD, "/", 100, true);
  Serial.println("-end-");
}

void loop()
{
}
ideas why?

-Frank

frankcohen
Posts: 25
Joined: Mon Apr 05, 2021 4:03 am

Re: ESP32-S3 SD NAND openNextFile returning on results

Postby frankcohen » Tue Oct 31, 2023 2:42 pm

Here's the output:

Code: Select all

Starting
SD Card Type: SDSC
SD Card Size: 122MB
Listing directory: /
root = /
file = 0
file.path =
-end-
There are 3 files, no directories. The typical functions of create file for write, read file, delete file, create directory, delete directory all work. It's only openNextFile that isn't behaving as expected.

-Frank

frankcohen
Posts: 25
Joined: Mon Apr 05, 2021 4:03 am

Re: ESP32-S3 SD NAND openNextFile returning on results

Postby frankcohen » Tue Oct 31, 2023 6:59 pm

This may be an SD formatting problem, or a hardware problem. I tried the same code on a new board with a new NAND/SD component. It works as expected:

Code: Select all

Starting
SD Card Type: SDSC
SD Card Size: 122MB
Writing file: /hello.txt
File written
Writing file: /hello2.txt
File written
Writing file: /hello3.txt
File written
Listing directory: /
root = /
file = 1
file.path = /hello.txt
FILE: hello.txt SIZE: 6
FILE: hello2.txt SIZE: 6
FILE: hello3.txt SIZE: 6
-end-
Now I'm hunting for SD Fat formatting/initialization code. This is something that clears the NAND and starts with a fresh directory structure. So far I've only found one in the SDFat library and I'm not sure if that will work with the Espressif ESP32 version of the SD library? Any recommendations?

-Frank

frankcohen
Posts: 25
Joined: Mon Apr 05, 2021 4:03 am

Re: ESP32-S3 SD NAND openNextFile returning on results

Postby frankcohen » Sat Nov 11, 2023 2:01 am

It's not the SD formatting to blame. Further investigation shows openNextFile not returning the file and directory details at the top level - '/' - of the directory. openNextFile works fine in subdirectories. I opened a trouble ticket here: https://www.arduino.cc/reference/en/lib ... nnextfile/

-Frank

Who is online

Users browsing this forum: No registered users and 397 guests