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

Files at this revision

API Documentation at this revision

Comitter:
neilt6
Date:
Tue Dec 08 16:02:44 2015 +0000
Parent:
19:84b2958bbcae
Child:
21:d10a519c0910
Commit message:
Improved card detection logic

Changed in this revision

SDFileSystem.cpp Show annotated file Show diff for this revision Revisions of this file
SDFileSystem.h Show annotated file Show diff for this revision Revisions of this file
--- 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;
+            }
+        }
     }
 }
 
--- a/SDFileSystem.h	Thu Nov 26 16:19:53 2015 +0000
+++ b/SDFileSystem.h	Tue Dec 08 16:02:44 2015 +0000
@@ -102,9 +102,19 @@
      */
     SDFileSystem(PinName mosi, PinName miso, PinName sclk, PinName cs, const char* name, PinName cd = NC, SwitchType cdtype = SWITCH_NONE, int hz = 1000000);
 
+    /** Determine whether or not a card is present
+     *
+     * @returns
+     *   'true' if a card is present,
+     *   'false' if no card is present.
+     */
+    bool card_present();
+
     /** Get the detected SD/MMC card type
      *
      * @returns The detected card type as a CardType enum.
+     *
+     * @note Valid after the filesystem has been mounted.
      */
     SDFileSystem::CardType card_type();