Made the initialize function try up to three times to get the card in IDLE state before giving up. This fixes the issue with f_open failing the first time after a power cycle.

Dependencies:   FATFileSystem

Dependents:   dm_sdcard_with_adapter dm_sdcard

Fork of SDFileSystem by mbed official

Files at this revision

API Documentation at this revision

Comitter:
displaymodule
Date:
Mon Jul 07 06:29:05 2014 +0000
Parent:
3:7b35d1709458
Commit message:
Made initialize function try again if the card could not be set to IDLE state. This prevents the issue where the first f_open() fails but all other succeed.

Changed in this revision

SDFileSystem.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 7b35d1709458 -r 4e415ec868b5 SDFileSystem.cpp
--- a/SDFileSystem.cpp	Mon Mar 17 14:34:01 2014 +0000
+++ b/SDFileSystem.cpp	Mon Jul 07 06:29:05 2014 +0000
@@ -147,14 +147,24 @@
         _spi.write(0xFF);
     }
     
-    // send CMD0, should return with all zeros except IDLE STATE set (bit 0)
-    if (_cmd(0, 0) != R1_IDLE_STATE) {
+    // Make three attempts at getting the card in IDLE STATE
+    int r;
+    for (int attempt = 0; attempt < 3; attempt++) {
+        // send CMD0, should return with all zeros except IDLE STATE set (bit 0)
+        r = _cmd(0, 0);
+        if (r == R1_IDLE_STATE) {
+            break;
+        } else {
+            wait(0.1);
+        }
+    }
+    if (r != R1_IDLE_STATE) {
         debug("No disk, or could not put SD card in to SPI idle state\n");
         return SDCARD_FAIL;
     }
     
     // send CMD8 to determine whther it is ver 2.x
-    int r = _cmd8();
+    r = _cmd8();
     if (r == R1_IDLE_STATE) {
         return initialise_card_v2();
     } else if (r == (R1_IDLE_STATE | R1_ILLEGAL_COMMAND)) {