From 60864e38aaa4f319dad5ecc1a912bc97fd12f518 Mon Sep 17 00:00:00 2001 From: Bill Greiman Date: Fri, 4 Jan 2019 07:09:53 -0800 Subject: [PATCH] Improved card re-initialization --- library.properties | 2 +- src/SdCard/SdInfo.h | 5 ++--- src/SdCard/SdSpiCard.cpp | 14 +++++++++++--- src/SdFat.h | 2 +- 4 files changed, 15 insertions(+), 8 deletions(-) diff --git a/library.properties b/library.properties index de417cb7..07088fa6 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=SdFat -version=1.0.13 +version=1.0.14 author=Bill Greiman maintainer=Bill Greiman sentence=FAT16/FAT32 file system for SD cards. diff --git a/src/SdCard/SdInfo.h b/src/SdCard/SdInfo.h index 7ebb4106..38579414 100644 --- a/src/SdCard/SdInfo.h +++ b/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 */ diff --git a/src/SdCard/SdSpiCard.cpp b/src/SdCard/SdSpiCard.cpp index 602e4c8a..6e20e7db 100644 --- a/src/SdCard/SdSpiCard.cpp +++ b/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) { diff --git a/src/SdFat.h b/src/SdFat.h index b1fba0f9..9387372f 100644 --- a/src/SdFat.h +++ b/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