-
Notifications
You must be signed in to change notification settings - Fork 7.6k
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
Comments
please decode the |
Hi @me-no-dev , /*
* 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 |
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 error is little different: |
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.
Originally posted by @TienHuyIoT in #5195 (comment)
The text was updated successfully, but these errors were encountered: