Skip to content

SD_MMC sketch begin() function get crash with case SD-card is not present or removed out slot socket SD. #5368

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
TienHuyIoT opened this issue Jul 9, 2021 · 3 comments
Assignees

Comments

@TienHuyIoT
Copy link

esp32 module: WROOM
version: install latest 2.0.0

SDMMC begin and work succeed within case the SD-card have present in slot socket SD.
If the SD-card is removed out slot socket SD. Esp32 shall crash and reset log print as below.

with version: release v1.0.6
ESP32 example SD_MMC work normal with this case.

rst:0xc (SW_CPU_RESET),boot:0x1f (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0030,len:1412
load:0x40078000,len:13416
load:0x40080400,len:3672
entry 0x400805f8
E (79) sdmmc_common: sdmmc_init_ocr: send_op_cond (1) returned 0x107
E (80) vfs_fat_sdmmc: sdmmc_card_init failed (0x107).
assertion "xQueue" failed: file "../esp-idf/components/freertos/queue.c", line 1946, function: uxQueueMessagesWaiting

abort() was called at PC 0x400eafe7 on core 1


Backtrace:0x400d8051:0x3ffb25a00x40088d41:0x3ffb25c0 0x4008e74d:0x3ffb25e0 0x400eafe7:0x3ffb2660 0x40089e89:0x3ffb2690 0x400dd4b7:0x3ffb26b0 0x400dbac1:0x3ffb26e0 0x400dbc2a:0x3ffb2720 0x400e4f95:0x3ffb2740 0x400d30e1:0x3ffb2770 0x400d2278:0x3ffb27f0 0x400d402f:0x3ffb2820 

Originally posted by @TienHuyIoT in #5195 (comment)

@me-no-dev
Copy link
Member

please decode the Backtrace

@TienHuyIoT
Copy link
Author

TienHuyIoT commented Jul 9, 2021

Hi @me-no-dev ,
This is my SDMMC_Test.ino Code.

/*
 * Connect the SD card to the following pins:
 *
 * SD Card | ESP32
 *    D2       12
 *    D3       13
 *    CMD      15
 *    VSS      GND
 *    VDD      3.3V
 *    CLK      14
 *    VSS      GND
 *    D0       2  (add 1K pull up after flashing)
 *    D1       4
 */

#include "FS.h"
#include "SD_MMC.h"

#define SD_POWER_PIN     		        22
#define SD_POWER_PINMODE_INIT()         pinMode(SD_POWER_PIN, OUTPUT_OPEN_DRAIN)
#define SD_POWER_ON()                   digitalWrite(SD_POWER_PIN, LOW)
#define SD_POWER_OFF()                  digitalWrite(SD_POWER_PIN, HIGH)

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 createDir(fs::FS &fs, const char * path){
    Serial.printf("Creating Dir: %s\n", path);
    if(fs.mkdir(path)){
        Serial.println("Dir created");
    } else {
        Serial.println("mkdir failed");
    }
}

void removeDir(fs::FS &fs, const char * path){
    Serial.printf("Removing Dir: %s\n", path);
    if(fs.rmdir(path)){
        Serial.println("Dir removed");
    } else {
        Serial.println("rmdir failed");
    }
}

void readFile(fs::FS &fs, const char * path){
    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()){
        Serial.write(file.read());
    }
}

void writeFile(fs::FS &fs, const char * path, const char * message){
    Serial.printf("Writing file: %s\n", path);

    File file = fs.open(path, FILE_WRITE);
    if(!file){
        Serial.println("Failed to open file for writing");
        return;
    }
    if(file.print(message)){
        Serial.println("File written");
    } else {
        Serial.println("Write failed");
    }
}

void appendFile(fs::FS &fs, const char * path, const char * message){
    Serial.printf("Appending to file: %s\n", path);

    File file = fs.open(path, FILE_APPEND);
    if(!file){
        Serial.println("Failed to open file for appending");
        return;
    }
    if(file.print(message)){
        Serial.println("Message appended");
    } else {
        Serial.println("Append failed");
    }
}

void renameFile(fs::FS &fs, const char * path1, const char * path2){
    Serial.printf("Renaming file %s to %s\n", path1, path2);
    if (fs.rename(path1, path2)) {
        Serial.println("File renamed");
    } else {
        Serial.println("Rename failed");
    }
}

void deleteFile(fs::FS &fs, const char * path){
    Serial.printf("Deleting file: %s\n", path);
    if(fs.remove(path)){
        Serial.println("File deleted");
    } else {
        Serial.println("Delete failed");
    }
}

void testFileIO(fs::FS &fs, const char * path){
    File file = fs.open(path);
    static uint8_t buf[512];
    size_t len = 0;
    uint32_t start = millis();
    uint32_t end = start;
    if(file){
        len = file.size();
        size_t flen = len;
        start = millis();
        while(len){
            size_t toRead = len;
            if(toRead > 512){
                toRead = 512;
            }
            file.read(buf, toRead);
            len -= toRead;
        }
        end = millis() - start;
        Serial.printf("%u bytes read for %u ms\n", flen, end);
        file.close();
    } else {
        Serial.println("Failed to open file for reading");
    }


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

    size_t i;
    start = millis();
    for(i=0; i<2048; i++){
        file.write(buf, 512);
    }
    end = millis() - start;
    Serial.printf("%u bytes written for %u ms\n", 2048 * 512, end);
    file.close();
}

void setup(){
    Serial.begin(115200);

    SD_POWER_PINMODE_INIT();
    SD_POWER_OFF();
    delay(10);
    SD_POWER_ON();
    delay(10);

    if(!SD_MMC.begin()){
        Serial.println("Card Mount Failed");
        return;
    }
    uint8_t cardType = SD_MMC.cardType();

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

    Serial.print("SD_MMC 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_MMC.cardSize() / (1024 * 1024);
    Serial.printf("SD_MMC Card Size: %lluMB\n", cardSize);

    listDir(SD_MMC, "/", 0);
    createDir(SD_MMC, "/mydir");
    listDir(SD_MMC, "/", 0);
    removeDir(SD_MMC, "/mydir");
    listDir(SD_MMC, "/", 2);
    writeFile(SD_MMC, "/hello.txt", "Hello ");
    appendFile(SD_MMC, "/hello.txt", "World!\n");
    readFile(SD_MMC, "/hello.txt");
    deleteFile(SD_MMC, "/foo.txt");
    renameFile(SD_MMC, "/hello.txt", "/foo.txt");
    readFile(SD_MMC, "/foo.txt");
    testFileIO(SD_MMC, "/test.txt");
    Serial.printf("Total space: %lluMB\n", SD_MMC.totalBytes() / (1024 * 1024));
    Serial.printf("Used space: %lluMB\n", SD_MMC.usedBytes() / (1024 * 1024));
}

void loop(){

}

And my Exception Deocder

Decoding stack results
0x400d8051: panic_abort at ../esp-idf/components/esp_system/panic.c line 390
0x40088d41: esp_system_abort at ../esp-idf/components/esp_system/esp_system.c line 126
0x4008e74d: abort at ../esp-idf/components/newlib/abort.c line 46
0x400eafe7: __assert_func at /builds/idf/crosstool-NG/.build/HOST-i686-w64-mingw32/xtensa-esp32-elf/src/newlib/newlib/libc/stdlib/assert.c line 62
0x40089e89: uxQueueMessagesWaiting at ../esp-idf/components/freertos/queue.c line 1946
0x400dd4b7: spi_bus_remove_device at ../esp-idf/components/driver/spi_master.c line 434
0x400dbac1: deinit_slot at ../esp-idf/components/driver/sdspi_host.c line 223
0x400dbc2a: sdspi_host_remove_device at ../esp-idf/components/driver/sdspi_host.c line 267
0x400e4f95: esp_vfs_fat_sdmmc_mount at ../esp-idf/components/fatfs/vfs/vfs_fat_sdmmc.c line 361
0x400d30e1: fs::SDMMCFS::begin(char const*, bool, bool) at C:\Users\HUY-GAMING\Documents\Arduino\hardware\espressif\esp32\libraries\SD_MMC\src\SD_MMC.cpp line 76
0x400d2278: setup() at C:\Users\HUY-GAMING\Documents\Arduino\generated_examples\SDMMC_Test/SDMMC_Test.ino line 187
0x400d402f: loopTask(void*) at C:\Users\HUY-GAMING\Documents\Arduino\hardware\espressif\esp32\cores\esp32\main.cpp line 35

@VojtechBartoska VojtechBartoska added the Status: Test needed Issue needs testing label Jul 14, 2021
@diegonunesbr
Copy link

diegonunesbr commented Aug 16, 2021

I have the same problem. With the card present, the code works very well.

Without the card, the driver try to deinit SPI bus, but SDMMC was initied in SDMMC 4 bits mode, not SPI.
My board all 6 data pins connected, exactly as source code provied by TienHuyIoT

My error is little different:
E (906) sdmmc_common: sdmmc_init_ocr: send_op_cond (1) returned 0x107 E (907) vfs_fat_sdmmc: sdmmc_card_init failed (0x107). E (907) gpio: gpio_isr_handler_remove(483): GPIO isr service is not installed, call gpio_install_isr_service() first assertion "heap != NULL && "free() target pointer is outside heap areas"" failed: file "../esp-idf/components/heap/heap_caps.c", line 304, function: heap_caps_free

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants