Browse Source

Improved card re-initialization

Bill Greiman 6 years ago
parent
commit
60864e38aa
4 changed files with 15 additions and 8 deletions
  1. 1 1
      library.properties
  2. 2 3
      src/SdCard/SdInfo.h
  3. 11 3
      src/SdCard/SdSpiCard.cpp
  4. 1 1
      src/SdFat.h

+ 1 - 1
library.properties

@@ -1,5 +1,5 @@
 name=SdFat
-version=1.0.13
+version=1.0.14
 author=Bill Greiman <fat16lib@sbcglobal.net>
 maintainer=Bill Greiman <fat16lib@sbcglobal.net>
 sentence=FAT16/FAT32 file system for SD cards.

+ 2 - 3
src/SdCard/SdInfo.h

@@ -120,9 +120,8 @@ const uint8_t SD_CARD_TYPE_SDHC = 3;
 #define SPI_SIXTEENTH_SPEED SD_SCK_HZ(F_CPU/32)
 //------------------------------------------------------------------------------
 // SD operation timeouts
-/** CMD0 loop delay ms */
-const uint16_t SD_CMD0_DELAY = 100;
-/** init timeout ms */
+/** CMD0 retry count */
+const uint8_t SD_CMD0_RETRY = 10;
 /** command timeout ms */
 const uint16_t SD_CMD_TIMEOUT = 300;
 /** init timeout ms */

+ 11 - 3
src/SdCard/SdSpiCard.cpp

@@ -243,9 +243,17 @@ bool SdSpiCard::begin(SdSpiDriver* spi, uint8_t csPin, SPISettings settings) {
   spiSelect();
 
   // command to go idle in SPI mode
-  if (cardCommand(CMD0, 0) != R1_IDLE_STATE) {
-    error(SD_CARD_ERROR_CMD0);
-    goto fail;
+  for (uint8_t i = 0; cardCommand(CMD0, 0) != R1_IDLE_STATE; i++) {
+    if (i == SD_CMD0_RETRY) {
+      error(SD_CARD_ERROR_CMD0);
+      goto fail;
+    }
+    // stop multi-block write
+    spiSend(STOP_TRAN_TOKEN);
+    // finish block transfer
+    for (int i = 0; i < 520; i++) {
+      spiReceive();
+    }
   }
 #if USE_SD_CRC
   if (cardCommand(CMD59, 1) != R1_IDLE_STATE) {

+ 1 - 1
src/SdFat.h

@@ -37,7 +37,7 @@
 #endif  // INCLUDE_SDIOS
 //------------------------------------------------------------------------------
 /** SdFat version */
-#define SD_FAT_VERSION "1.0.13"
+#define SD_FAT_VERSION "1.0.14"
 //==============================================================================
 /**
  * \class SdBaseFile