SD.Open() causes crashing with esp32

qheinal
Posts: 25
Joined: Tue Feb 20, 2024 4:38 pm

SD.Open() causes crashing with esp32

Postby qheinal » Thu Mar 07, 2024 2:35 pm

The default library for reading / writing to SD seems to not work correctly with esp32. All the functions work well in Setup() but if you try to read or write to a file in loop() then it causes a crash with this error


10:22:03.495 -> Listing directory: /
10:22:03.495 -> DIR : System Volume Information
10:22:03.495 -> FILE: test.txt SIZE: 1048576
10:22:03.495 -> FILE: foo.txt SIZE: 13
10:22:03.495 -> FILE: blue-flower.bmp SIZE: 153666
10:22:03.538 -> FILE: Slime01_32x32.bmp SIZE: 2114
10:22:03.538 -> FILE: Slime02.bmp SIZE: 2114
10:22:03.538 -> FILE: Slime03_32x34.bmp SIZE: 2242
10:22:03.538 -> FILE: Spaceship.bmp SIZE: 2650
10:22:03.538 -> FILE: bruh.txt SIZE: 12
10:22:03.538 -> FILE: gameastroid.bmp SIZE: 4674
10:22:04.549 -> Reading File
10:22:04.549 -> Writing file: /hello.txt
10:22:04.549 ->
10:22:04.549 -> assert failed: xQueueSemaphoreTake queue.c:1545 (( pxQueue ))
10:22:04.549 ->
10:22:04.549 ->
10:22:04.549 -> Backtrace: 0x400835dd:0x3ffb1ae0 0x4008984d:0x3ffb1b00 0x4008e749:0x3ffb1b20 0x4008a5ad:0x3ffb1c50 0x400d4d6e:0x3ffb1c90 0x400d2541:0x3ffb1cc0 0x400e351a:0x3ffb1cf0 0x400e3819:0x3ffb1d10 0x400e57b3:0x3ffb1d50 0x400e6fd6:0x3ffb1fc0 0x400e092a:0x3ffb2130 0x400f0e28:0x3ffb2150 0x400d4876:0x3ffb2170 0x400d37be:0x3ffb2200 0x400d188b:0x3ffb2230 0x400d19c9:0x3ffb2270 0x400d80a9:0x3ffb2290
10:22:04.596 ->
10:22:04.596 ->
10:22:04.596 ->
10:22:04.596 ->
10:22:04.596 -> ELF file SHA256: d311bda49d21a55e

My guess is the default library might be using while loops which don't have delays for the watchdog task

lbernstone
Posts: 826
Joined: Mon Jul 22, 2019 3:20 pm

Re: SD.Open() causes crashing with esp32

Postby lbernstone » Thu Mar 07, 2024 4:37 pm

The examples in the file system libraries are primarily there for unit testing, to verify that changes in the code have not impacted the behavior. Some pieces are quite generally useful (file listing). Some will need to be modified to use in live code.
Is there a question here? If you are conducting operations on multiple files at high priority, you should have a delay in between them to allow the system time to operate.

qheinal
Posts: 25
Joined: Tue Feb 20, 2024 4:38 pm

Re: SD.Open() causes crashing with esp32

Postby qheinal » Sat Mar 09, 2024 6:03 pm

It doesnt work with delay or without. Just immediately causes crash Whenever its used in Loop(). Im only trying to read a file. But it crashes on all Reading or writing.

lbernstone
Posts: 826
Joined: Mon Jul 22, 2019 3:20 pm

Re: SD.Open() causes crashing with esp32

Postby lbernstone » Sat Mar 09, 2024 7:51 pm

If you were to post a minimal example, we could help you.

qheinal
Posts: 25
Joined: Tue Feb 20, 2024 4:38 pm

Re: SD.Open() causes crashing with esp32

Postby qheinal » Sat Mar 09, 2024 10:08 pm

This also produces same error. Also this doesnt happen with SdFat library. Just the ones from esp32 default libraries

Code: Select all

#include "SD.h"
#include "SPI.h"

#define HSPI_MISO   12
#define HSPI_MOSI   13
#define HSPI_SCLK   14
#define HSPI_SS     27

SPIClass * hspi = NULL;
static const uint32_t hspiClk = 16000000; // 16 MHz  The sd card im using only supports 8 or 16 Mhz spi.

bool  StorageMounted = false;
void setup(){ 
 
   Serial.begin(115200);

   hspi = new SPIClass(HSPI);
    SPIClass mSPI = *hspi;
    pinMode(HSPI_SS, OUTPUT);    
    //digitalWrite(HSPI_SS, HIGH);
    hspi->begin(HSPI_SCLK,HSPI_MISO,HSPI_MOSI,HSPI_SS);
   // hspi->beginTransaction(SPISettings(spiClk, MSBFIRST, SPI_MODE0));
    bool SD_OK =  SD.begin(HSPI_SS,mSPI,hspiClk,"/sd",10,false);
   
    if(!SD_OK){
      StorageMounted = false;
        Serial.println("Card Mount Failed");
        return;
    }
    else {  StorageMounted = true ; Serial.println("Card Mounted. "); }
    
     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, "/", 0);
}


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

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

    File file = root.openNextFile();
    while(file){
        if(file.isDirectory()){
            Serial.print("  DIR : ");
            Serial.println(file.name());
            if(levels){
                listDir(fs, file.path(), levels -1);
            }
        } else {
            Serial.print("  FILE: ");
            Serial.print(file.name());
            Serial.print("  SIZE: ");
            Serial.println(file.size());
        }
        file = root.openNextFile();
    }
}

void readFile(fs::FS &fs, const char * path, char* FileBuffer, uint32_t FileSize){
    Serial.printf("Reading file: %s\n", path);

    File file = fs.open(path);
    if(!file){
        Serial.println("Failed to open file for reading");
        return;
    }

    Serial.print("Read from file: ");

    while(file.available()){
        file.readBytes(FileBuffer, FileSize);
    }
    file.close();
}


bool disableREAD =false;
char* DBuffer = (char*)malloc(1024);

void loop(){
 
   if(StorageMounted && !disableREAD)
  {
     readFile(SD, "/test.txt",DBuffer,8);
     Serial.println("Done Reading");
     disableREAD =true;
  }
}

lbernstone
Posts: 826
Joined: Mon Jul 22, 2019 3:20 pm

Re: SD.Open() causes crashing with esp32

Postby lbernstone » Sun Mar 10, 2024 3:50 am

Code: Select all

    hspi = new SPIClass(HSPI);
    hspi->begin(HSPI_SCLK,HSPI_MISO,HSPI_MOSI,HSPI_SS);
    bool SD_OK =  SD.begin(HSPI_SS,*(hspi));
https://wokwi.com/projects/391940646407088129

Emile_esp
Posts: 5
Joined: Wed Jan 04, 2023 10:02 am

Re: SD.Open() causes crashing with esp32

Postby Emile_esp » Sun Mar 10, 2024 8:38 am

I have did a number of project using the Arduino Sd and greiman/SdFat
That supports >32GB sd cards and test on all ESP ( exspet the new ones H2/C6 )
You can have a look at the total projects ( Wifi manager, OTA updates, NTP time , and SD card , FTP Server)
https://github.com/EmileSpecialProducts ... sk-driveEx
https://github.com/EmileSpecialProducts ... disk-drive
https://github.com/EmileSpecialProducts/FTP-WebServer
The FTP-Webserver is not yet supportin >32GB

qheinal
Posts: 25
Joined: Tue Feb 20, 2024 4:38 pm

Re: SD.Open() causes crashing with esp32

Postby qheinal » Sun Mar 10, 2024 1:03 pm

Btw this is written in arduino ide

@lbernstone the problem wasn't the spi code. It is the default esp32 SD library. Also the simulation isn't perfect so it will pass this code that doesn't work on real hardware.

@Emile_esp your code doesn't exactly read files from loop. the SD can read /write to files but only in the Setup() function. Put it on Loop and it fails and causes crashing.

lbernstone
Posts: 826
Joined: Mon Jul 22, 2019 3:20 pm

Re: SD.Open() causes crashing with esp32

Postby lbernstone » Sun Mar 10, 2024 5:44 pm

You are welcome to open an issue in the repo. They will want example code and a decoded backtrace from the error.

sailingfree
Posts: 1
Joined: Thu Jun 13, 2024 4:26 pm

Re: SD.Open() causes crashing with esp32

Postby sailingfree » Thu Jun 13, 2024 4:32 pm

I've just run into this problem too. The solution is to move the definition of the SPI object into global scope rather than have it as a local object which goes out of scope after the setup function has finished. The example in the SD library is also wrong and has been copied to other SD examples as well.

Who is online

Users browsing this forum: Google [Bot] and 90 guests