SDFileSystem

Dependencies:   FATFileSystem

Dependents:   EMIRv2

Fork of SDFileSystem by mbed official

Revision:
4:079bf96a6aad
Parent:
3:7b35d1709458
--- a/SDFileSystem.cpp	Mon Mar 17 14:34:01 2014 +0000
+++ b/SDFileSystem.cpp	Sat Mar 29 10:45:18 2014 +0000
@@ -199,6 +199,8 @@
 int SDFileSystem::disk_initialize() {
     int i = initialise_card();
     debug_if(SD_DBG, "init card = %d\n", i);
+    if (i == SDCARD_FAIL) return 1;
+    
     _sectors = _sd_sectors();
     
     // Set block length to 512 (CMD16)
@@ -350,7 +352,13 @@
     _cs = 0;
     
     // read until start byte (0xFF)
-    while (_spi.write(0xFF) != 0xFE);
+    int timeout = SD_COMMAND_TIMEOUT;
+    while (_spi.write(0xFF) != 0xFE && --timeout);
+    if (timeout == 0) {
+        _cs = 1;
+        _spi.write(0xFF);
+        return 1;
+    }
     
     // read data
     for (int i = 0; i < length; i++) {
@@ -387,7 +395,13 @@
     }
     
     // wait for write to finish
-    while (_spi.write(0xFF) == 0);
+    int timeout = SD_COMMAND_TIMEOUT;
+    while (_spi.write(0xFF) == 0 && --timeout);
+    if (timeout == 0) {
+        _cs = 1;
+        _spi.write(0xFF);
+        return 1;
+    }
     
     _cs = 1;
     _spi.write(0xFF);