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:
Wed Apr 13 16:51:25 2016 +0000
Parent:
22:3fa5eaf48e81
Child:
24:a52e682a1ce4
Commit message:
Added high-speed mode (50MHz) support for v2.00 or later SD cards

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	Wed Feb 24 17:46:31 2016 +0000
+++ b/SDFileSystem.cpp	Wed Apr 13 16:51:25 2016 +0000
@@ -222,11 +222,20 @@
             else
                 m_CardType = CARD_SD;
 
-            //Increase the SPI frequency to full speed (up to 25MHz for SDCv2)
-            if (m_FREQ > 25000000)
-                m_Spi.frequency(25000000);
-            else
+            //Increase the SPI frequency to full speed (up to 50MHz for SDCv2)
+            if (m_FREQ > 25000000) {
+                if (enableHighSpeedMode()) {
+                    if (m_FREQ > 50000000) {
+                        m_Spi.frequency(50000000);
+                    } else {
+                        m_Spi.frequency(m_FREQ);
+                    }
+                } else {
+                    m_Spi.frequency(25000000);
+                }
+            } else {
                 m_Spi.frequency(m_FREQ);
+            }
         } else {
             //Initialization failed
             m_CardType = CARD_UNKNOWN;
@@ -883,3 +892,32 @@
     deselect();
     return false;
 }
+
+bool SDFileSystem::enableHighSpeedMode()
+{
+    //Try to issue CMD6 up to 3 times
+    for (int f = 0; f < 3; f++) {
+        //Select the card, and wait for ready
+        if(!select())
+            break;
+
+        //Send CMD6(0x80FFFFF1) to change the access mode to high speed
+        if (writeCommand(CMD6, 0x80FFFFF1) == 0x00) {
+            //Read the 64B status data block
+            char status[64];
+            bool success = readData(status, 64);
+            deselect();
+            if (success) {
+                //Return whether or not the operation was successful
+                return ((status[16] & 0x0F) == 0x1);
+            }
+        } else {
+            //The command failed, get out
+            break;
+        }
+    }
+
+    //The operation failed 3 times
+    deselect();
+    return false;
+}
--- a/SDFileSystem.h	Wed Feb 24 17:46:31 2016 +0000
+++ b/SDFileSystem.h	Wed Apr 13 16:51:25 2016 +0000
@@ -173,6 +173,7 @@
     enum Command {
         CMD0 = (0x40 | 0),      /**< GO_IDLE_STATE */
         CMD1 = (0x40 | 1),      /**< SEND_OP_COND */
+        CMD6 = (0x40 | 6),      /**< SWITCH_FUNC */
         CMD8 = (0x40 | 8),      /**< SEND_IF_COND */
         CMD9 = (0x40 | 9),      /**< SEND_CSD */
         CMD12 = (0x40 | 12),    /**< STOP_TRANSMISSION */
@@ -218,6 +219,7 @@
     bool readBlocks(char* buffer, unsigned int lba, unsigned int count);
     bool writeBlock(const char* buffer, unsigned int lba);
     bool writeBlocks(const char* buffer, unsigned int lba, unsigned int count);
+    bool enableHighSpeedMode();
 };
 
 #endif