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

Revision:
4:4e415ec868b5
Parent:
3:7b35d1709458
--- 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)) {