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:
Thu Jul 31 17:40:50 2014 +0000
Parent:
4:49b29888eca7
Child:
6:55a26a56046a
Commit message:
Added ACMD42 to disconnect the internal pull-up resistor on /CS

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 Jul 30 17:51:33 2014 +0000
+++ b/SDFileSystem.cpp	Thu Jul 31 17:40:50 2014 +0000
@@ -71,8 +71,8 @@
     for (int i = 0; i < 10; i++)
         m_SPI.write(0xFF);
 
-    //Write CMD0(0), and check for a valid response
-    resp = writeCommand(CMD0, 0);
+    //Write CMD0(0x00000000) to reset the card
+    resp = writeCommand(CMD0, 0x00000000);
     if (resp != 0x01) {
         //Initialization failed
         m_CardType = CARD_UNKNOWN;
@@ -89,8 +89,8 @@
             return m_Status;
         }
 
-        //Send CMD58(0) to read the OCR, and verify that the card supports 3.2-3.3V
-        resp = writeCommand(CMD58, 0);
+        //Send CMD58(0x00000000) to read the OCR, and verify that the card supports 3.2-3.3V
+        resp = writeCommand(CMD58, 0x00000000);
         if (resp != 0x01 || !(readReturn() & (1 << 20))) {
             //Initialization failed
             m_CardType = CARD_UNKNOWN;
@@ -112,11 +112,11 @@
             return m_Status;
         }
 
-        //Send CMD58(0) to read the OCR
-        resp = writeCommand(CMD58, 0);
+        //Send CMD58(0x00000000) to read the OCR
+        resp = writeCommand(CMD58, 0x00000000);
         if (resp == 0x00) {
             //Check the CCS bit to determine if this is a high capacity card
-            if (readReturn() & 0x40000000)
+            if (readReturn() & (1 << 30))
                 m_CardType = CARD_SDHC;
             else
                 m_CardType = CARD_SD;
@@ -127,8 +127,8 @@
         }
     } else {
         //Didn't respond or illegal command, this is either an SDCv1 or MMC card
-        //Send CMD58(0) to read the OCR, and verify that the card supports 3.2-3.3V
-        resp = writeCommand(CMD58, 0);
+        //Send CMD58(0x00000000) to read the OCR, and verify that the card supports 3.2-3.3V
+        resp = writeCommand(CMD58, 0x00000000);
         if (resp != 0x01 || !(readReturn() & (1 << 20))) {
             //Initialization failed
             m_CardType = CARD_UNKNOWN;
@@ -186,6 +186,16 @@
         }
     }
 
+    //Send ACMD42(0x00000000) to disconnect the internal pull-up resistor on /CS if necessary
+    if (m_CardType != CARD_MMC) {
+        resp = writeCommand(ACMD42, 0x00000000);
+        if (resp != 0x00) {
+            //Initialization failed
+            m_CardType = CARD_UNKNOWN;
+            return m_Status;
+        }
+    }
+
     //The card is now initialized
     m_Status &= ~STA_NOINIT;
 
@@ -312,8 +322,8 @@
 
     //Try to read the CSD register up to 3 times
     for (int i = 0; i < 3; i++) {
-        //Send CMD9(0) to read the CSD register
-        if (writeCommand(CMD9, 0) == 0x00) {
+        //Send CMD9(0x00000000) to read the CSD register
+        if (writeCommand(CMD9, 0x00000000) == 0x00) {
             //Receive the 16B CSD data
             char csd[16];
             if (readData(csd, 16)) {
@@ -398,9 +408,9 @@
 
     //Try to send the command up to 3 times
     for (int i = 0; i < 3; i++) {
-        //Send CMD55 prior to an ACMD
-        if (cmd == ACMD41) {
-            resp = writeCommand(CMD55, 0);
+        //Send CMD55(0x00000000) prior to an application specific command
+        if (cmd == ACMD41 || cmd == ACMD42) {
+            resp = writeCommand(CMD55, 0x00000000);
             if (resp > 0x01)
                 return resp;
         }
--- a/SDFileSystem.h	Wed Jul 30 17:51:33 2014 +0000
+++ b/SDFileSystem.h	Thu Jul 31 17:40:50 2014 +0000
@@ -112,12 +112,13 @@
     enum Command {
         CMD0 = (0x40 | 0),      /**< GO_IDLE_STATE */
         CMD1 = (0x40 | 1),      /**< SEND_OP_COND */
-        ACMD41 = (0x40 | 41),   /**< APP_SEND_OP_COND */
         CMD8 = (0x40 | 8),      /**< SEND_IF_COND */
         CMD9 = (0x40 | 9),      /**< SEND_CSD */
         CMD16 = (0x40 | 16),    /**< SET_BLOCKLEN */
         CMD17 = (0x40 | 17),    /**< READ_SINGLE_BLOCK */
         CMD24 = (0x40 | 24),    /**< WRITE_BLOCK */
+        ACMD41 = (0x40 | 41),   /**< SD_SEND_OP_COND */
+        ACMD42 = (0x40 | 42),   /**< SET_CLR_CARD_DETECT */
         CMD55 = (0x40 | 55),    /**< APP_CMD */
         CMD58 = (0x40 | 58),    /**< READ_OCR */
         CMD59 = (0x40 | 59)     /**< CRC_ON_OFF */