A re-written SDFileSystem library with improved compatibility, CRC support, and card removal/replacement support.

Dependencies:   FATFileSystem

Dependents:   xadow_m0_SD_Hello roam_v1 roam_v2 Polytech_tours ... more

Revision:
20:2c1e8d442f68
Parent:
18:2286a4e7fa31
Child:
21:d10a519c0910
--- a/SDFileSystem.cpp	Thu Nov 26 16:19:53 2015 +0000
+++ b/SDFileSystem.cpp	Tue Dec 08 16:02:44 2015 +0000
@@ -56,19 +56,19 @@
     }
 }
 
-SDFileSystem::CardType SDFileSystem::card_type()
+bool SDFileSystem::card_present()
 {
-    //Check if there's a card in the socket
+    //Check the card socket
     checkSocket();
 
-    //Check if a card is present, but not initialized
-    if (!(m_Status & STA_NODISK) && (m_Status & STA_NOINIT)) {
-        //Initialize the card in order to determine the card type
-        disk_initialize();
+    //Return whether or not a card is present
+    return !(m_Status & STA_NODISK);
+}
 
-        //Change the status back to uninitialized so that FatFs will handle the card change properly
-        m_Status |= STA_NOINIT;
-    }
+SDFileSystem::CardType SDFileSystem::card_type()
+{
+    //Check the card socket
+    checkSocket();
 
     //Return the card type
     return m_CardType;
@@ -82,7 +82,7 @@
 
 void SDFileSystem::crc(bool enabled)
 {
-    //Check if there's a card in the socket
+    //Check the card socket
     checkSocket();
 
     //Just update the member variable if the card isn't initialized
@@ -132,9 +132,9 @@
     //Unmount the filesystem
     FATFileSystem::unmount();
 
-    //Change the status to not initialized, and the card type to none
+    //Change the status to not initialized, and the card type to unknown
     m_Status |= STA_NOINIT;
-    m_CardType = CARD_NONE;
+    m_CardType = CARD_UNKNOWN;
 
     //Always succeeds
     return 0;
@@ -308,7 +308,7 @@
 
 int SDFileSystem::disk_status()
 {
-    //Check if there's a card in the socket
+    //Check the card socket
     checkSocket();
 
     //Return the disk status
@@ -403,20 +403,27 @@
 
 void SDFileSystem::onCardRemoval()
 {
-    //Check if there's a card in the socket
+    //Check the card socket
     checkSocket();
 }
 
 inline void SDFileSystem::checkSocket()
 {
     //Use the card detect switch (if available) to determine if the socket is occupied
-    if (m_CdAssert == -1 || m_Cd == m_CdAssert) {
-        //The socket is occupied
-        m_Status &= ~STA_NODISK;
-    } else {
-        //The socket is empty
-        m_Status |= (STA_NODISK | STA_NOINIT);
-        m_CardType = CARD_NONE;
+    if (m_CdAssert != -1) {
+        if (m_Status & STA_NODISK) {
+            if (m_Cd == m_CdAssert) {
+                //The socket is now occupied
+                m_Status &= ~STA_NODISK;
+                m_CardType = CARD_UNKNOWN;
+            }
+        } else {
+            if (m_Cd != m_CdAssert) {
+                //The socket is now empty
+                m_Status |= (STA_NODISK | STA_NOINIT);
+                m_CardType = CARD_NONE;
+            }
+        }
     }
 }