SD mount problem

Jachin
Posts: 11
Joined: Fri Mar 18, 2022 9:06 pm

SD mount problem

Postby Jachin » Mon May 09, 2022 8:02 am

I use SPI and SD respectively according to some online example guidelines_ Connect my esp32 and SD card in MMC mode, and SD card can work normally in SPI mode_ Test program, but SD_ After MMC connection, "card mount failed" is always prompted. Then I try to pull up gpio2,4,12,13,15 and change it to "sd_mmc.begin("/sdcard", false)", but the result is still "card mount failed". Can someone help me? I want to use SD_ MMC way, because I use SPI for RFID card reading. Thank you very much!
  1. /*
  2.  * Connect the SD card to the following pins:
  3.  *
  4.  * SD Card | ESP32
  5.  *    D2       12
  6.  *    D3       13
  7.  *    CMD      15
  8.  *    VSS      GND
  9.  *    VDD      3.3V
  10.  *    CLK      14
  11.  *    VSS      GND
  12.  *    D0       2  (add 1K pull up after flashing)
  13.  *    D1       4
  14.  */
  15.  
  16. #include "FS.h"
  17. #include "SD_MMC.h"
  18.  
  19. #define ONE_BIT_MODE true
  20.  
  21. void listDir(fs::FS &fs, const char * dirname, uint8_t levels){
  22.     Serial.printf("Listing directory: %s\n", dirname);
  23.  
  24.     File root = fs.open(dirname);
  25.     if(!root){
  26.         Serial.println("Failed to open directory");
  27.         return;
  28.     }
  29.     if(!root.isDirectory()){
  30.         Serial.println("Not a directory");
  31.         return;
  32.     }
  33.  
  34.     File file = root.openNextFile();
  35.     while(file){
  36.         if(file.isDirectory()){
  37.             Serial.print("  DIR : ");
  38.             Serial.println(file.name());
  39.             if(levels){
  40.                 listDir(fs, file.path(), levels -1);
  41.             }
  42.         } else {
  43.             Serial.print("  FILE: ");
  44.             Serial.print(file.name());
  45.             Serial.print("  SIZE: ");
  46.             Serial.println(file.size());
  47.         }
  48.         file = root.openNextFile();
  49.     }
  50. }
  51.  
  52. void createDir(fs::FS &fs, const char * path){
  53.     Serial.printf("Creating Dir: %s\n", path);
  54.     if(fs.mkdir(path)){
  55.         Serial.println("Dir created");
  56.     } else {
  57.         Serial.println("mkdir failed");
  58.     }
  59. }
  60.  
  61. void removeDir(fs::FS &fs, const char * path){
  62.     Serial.printf("Removing Dir: %s\n", path);
  63.     if(fs.rmdir(path)){
  64.         Serial.println("Dir removed");
  65.     } else {
  66.         Serial.println("rmdir failed");
  67.     }
  68. }
  69.  
  70. void readFile(fs::FS &fs, const char * path){
  71.     Serial.printf("Reading file: %s\n", path);
  72.  
  73.     File file = fs.open(path);
  74.     if(!file){
  75.         Serial.println("Failed to open file for reading");
  76.         return;
  77.     }
  78.  
  79.     Serial.print("Read from file: ");
  80.     while(file.available()){
  81.         Serial.write(file.read());
  82.     }
  83. }
  84.  
  85. void writeFile(fs::FS &fs, const char * path, const char * message){
  86.     Serial.printf("Writing file: %s\n", path);
  87.  
  88.     File file = fs.open(path, FILE_WRITE);
  89.     if(!file){
  90.         Serial.println("Failed to open file for writing");
  91.         return;
  92.     }
  93.     if(file.print(message)){
  94.         Serial.println("File written");
  95.     } else {
  96.         Serial.println("Write failed");
  97.     }
  98. }
  99.  
  100. void appendFile(fs::FS &fs, const char * path, const char * message){
  101.     Serial.printf("Appending to file: %s\n", path);
  102.  
  103.     File file = fs.open(path, FILE_APPEND);
  104.     if(!file){
  105.         Serial.println("Failed to open file for appending");
  106.         return;
  107.     }
  108.     if(file.print(message)){
  109.         Serial.println("Message appended");
  110.     } else {
  111.         Serial.println("Append failed");
  112.     }
  113. }
  114.  
  115. void renameFile(fs::FS &fs, const char * path1, const char * path2){
  116.     Serial.printf("Renaming file %s to %s\n", path1, path2);
  117.     if (fs.rename(path1, path2)) {
  118.         Serial.println("File renamed");
  119.     } else {
  120.         Serial.println("Rename failed");
  121.     }
  122. }
  123.  
  124. void deleteFile(fs::FS &fs, const char * path){
  125.     Serial.printf("Deleting file: %s\n", path);
  126.     if(fs.remove(path)){
  127.         Serial.println("File deleted");
  128.     } else {
  129.         Serial.println("Delete failed");
  130.     }
  131. }
  132.  
  133. void testFileIO(fs::FS &fs, const char * path){
  134.     File file = fs.open(path);
  135.     static uint8_t buf[512];
  136.     size_t len = 0;
  137.     uint32_t start = millis();
  138.     uint32_t end = start;
  139.     if(file){
  140.         len = file.size();
  141.         size_t flen = len;
  142.         start = millis();
  143.         while(len){
  144.             size_t toRead = len;
  145.             if(toRead > 512){
  146.                 toRead = 512;
  147.             }
  148.             file.read(buf, toRead);
  149.             len -= toRead;
  150.         }
  151.         end = millis() - start;
  152.         Serial.printf("%u bytes read for %u ms\n", flen, end);
  153.         file.close();
  154.     } else {
  155.         Serial.println("Failed to open file for reading");
  156.     }
  157.  
  158.  
  159.     file = fs.open(path, FILE_WRITE);
  160.     if(!file){
  161.         Serial.println("Failed to open file for writing");
  162.         return;
  163.     }
  164.  
  165.     size_t i;
  166.     start = millis();
  167.     for(i=0; i<2048; i++){
  168.         file.write(buf, 512);
  169.     }
  170.     end = millis() - start;
  171.     Serial.printf("%u bytes written for %u ms\n", 2048 * 512, end);
  172.     file.close();
  173. }
  174.  
  175. void setup(){
  176.     Serial.begin(115200);
  177.     pinMode(2, INPUT_PULLUP);
  178.     pinMode(4, INPUT_PULLUP);
  179.     pinMode(12, INPUT_PULLUP);
  180.     pinMode(13, INPUT_PULLUP);
  181.     pinMode(15, INPUT_PULLUP);
  182.     if(!SD_MMC.begin("/sdcard", ONE_BIT_MODE)){
  183.         Serial.println("Card Mount Failed");
  184.         return;
  185.     }
  186.     uint8_t cardType = SD_MMC.cardType();
  187.  
  188.     if(cardType == CARD_NONE){
  189.         Serial.println("No SD_MMC card attached");
  190.         return;
  191.     }
  192.  
  193.     Serial.print("SD_MMC Card Type: ");
  194.     if(cardType == CARD_MMC){
  195.         Serial.println("MMC");
  196.     } else if(cardType == CARD_SD){
  197.         Serial.println("SDSC");
  198.     } else if(cardType == CARD_SDHC){
  199.         Serial.println("SDHC");
  200.     } else {
  201.         Serial.println("UNKNOWN");
  202.     }
  203.  
  204.     uint64_t cardSize = SD_MMC.cardSize() / (1024 * 1024);
  205.     Serial.printf("SD_MMC Card Size: %lluMB\n", cardSize);
  206.  
  207.     listDir(SD_MMC, "/", 0);
  208.     createDir(SD_MMC, "/mydir");
  209.     listDir(SD_MMC, "/", 0);
  210.     removeDir(SD_MMC, "/mydir");
  211.     listDir(SD_MMC, "/", 2);
  212.     writeFile(SD_MMC, "/hello.txt", "Hello ");
  213.     appendFile(SD_MMC, "/hello.txt", "World!\n");
  214.     readFile(SD_MMC, "/hello.txt");
  215.     deleteFile(SD_MMC, "/foo.txt");
  216.     renameFile(SD_MMC, "/hello.txt", "/foo.txt");
  217.     readFile(SD_MMC, "/foo.txt");
  218.     testFileIO(SD_MMC, "/test.txt");
  219.     Serial.printf("Total space: %lluMB\n", SD_MMC.totalBytes() / (1024 * 1024));
  220.     Serial.printf("Used space: %lluMB\n", SD_MMC.usedBytes() / (1024 * 1024));
  221. }
  222.  
  223. void loop(){
  224.  
  225. }
  1. E (42) sdmmc_common: sdmmc_init_ocr: send_op_cond (1) returned 0x107
  2. E (43) vfs_fat_sdmmc: sdmmc_card_init failed (0x107).
  3. Card Mount Failed

rpiloverbd
Posts: 101
Joined: Tue Mar 22, 2022 5:23 am

Re: SD mount problem

Postby rpiloverbd » Mon May 09, 2022 3:26 pm

Have you tested the SD card breakout board separately? With an Arduino?

Jachin
Posts: 11
Joined: Fri Mar 18, 2022 9:06 pm

Re: SD mount problem

Postby Jachin » Tue May 10, 2022 12:12 am

rpiloverbd wrote:
Mon May 09, 2022 3:26 pm
Have you tested the SD card breakout board separately? With an Arduino?
Thank you for your advice. I use the SD Adapter and connect it with ESP32 like this, but it can not run SDMMC_Test successfully.
I tried to change the connect way to use the SPI method and it can run SD_Test.ino successfully. Could you help me to find out the problem, thank you very much.
20220510080525.jpg
SD Adapter
20220510080525.jpg (553.04 KiB) Viewed 6911 times
20220510080657.png
Connect Picture
20220510080657.png (610.16 KiB) Viewed 6911 times
spi.png
SPI
spi.png (496.97 KiB) Viewed 6911 times

Jachin
Posts: 11
Joined: Fri Mar 18, 2022 9:06 pm

Re: SD mount problem

Postby Jachin » Tue May 10, 2022 3:00 am

@rpiloverbd

Jachin
Posts: 11
Joined: Fri Mar 18, 2022 9:06 pm

Re: SD mount problem

Postby Jachin » Tue May 10, 2022 3:25 am

E (15527) sdmmc_cmd: sdmmc_read_sectors_dma: sdmmc_send_cmd returned 0x109
E (15528) diskio_sdmmc: sdmmc_read_blocks failed (265)
E (15528) vfs_fat_sdmmc: mount_to_vfs failed (0xffffffff).
Card Mount Failed



Sometimes, it report error log like this. I don't even know why show a new kind error

rpiloverbd
Posts: 101
Joined: Tue Mar 22, 2022 5:23 am

Re: SD mount problem

Postby rpiloverbd » Sat May 14, 2022 2:03 pm

Oh, I see. Sorry, I never connected an SD card to an Arduino this way. I only used SD card breakout boards like this one: https://www.waveshare.com/micro-sd-storage-board.htm

So, I really can't say what are the possible scopes of error in your arrangement. Let's see if anyone else can suggest anything worthwhile.

Jachin
Posts: 11
Joined: Fri Mar 18, 2022 9:06 pm

Re: SD mount problem

Postby Jachin » Sun May 15, 2022 7:52 am

rpiloverbd wrote:
Sat May 14, 2022 2:03 pm
Oh, I see. Sorry, I never connected an SD card to an Arduino this way. I only used SD card breakout boards like this one: https://www.waveshare.com/micro-sd-storage-board.htm

So, I really can't say what are the possible scopes of error in your arrangement. Let's see if anyone else can suggest anything worthwhile.
Thank you anyway! You are so kind!

Who is online

Users browsing this forum: No registered users and 57 guests