From 1a02b0528c38ca726d9644cb1eaf4f054fd339e6 Mon Sep 17 00:00:00 2001 From: deepikabhavnani Date: Sat, 12 Aug 2017 00:11:31 -0500 Subject: [PATCH] Fixed SD card intialization failure Few SD cards were failing for CMD41 as device was not ready after CMD55. Added busy delay loop to make sure device is ready and than proceed. --- SDBlockDevice.cpp | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/SDBlockDevice.cpp b/SDBlockDevice.cpp index 3dc70cc128..734227f2fe 100644 --- a/SDBlockDevice.cpp +++ b/SDBlockDevice.cpp @@ -690,7 +690,12 @@ int SDBlockDevice::_cmd(SDBlockDevice::cmdSupported cmd, uint32_t arg, bool isAc for(int i = 0; i < 3; i++) { // Send CMD55 for APP command first if (isAcmd) { - _cmd_spi(CMD55_APP_CMD, 0x0); + response = _cmd_spi(CMD55_APP_CMD, 0x0); + } + + // Wait for card to be ready after CMD55 + if (false == _wait_ready(SD_COMMAND_TIMEOUT)) { + debug_if(SD_DBG, "Card not ready yet \n"); } // Send command over SPI interface @@ -710,20 +715,20 @@ int SDBlockDevice::_cmd(SDBlockDevice::cmdSupported cmd, uint32_t arg, bool isAc // Process the response R1 : Exit on CRC/Illegal command error/No response if (R1_NO_RESPONSE == response) { _deselect(); - debug_if(SD_DBG, "No response CMD:%d \n", cmd); + debug_if(SD_DBG, "No response CMD:%d response: 0x%x\n",cmd, response); return SD_BLOCK_DEVICE_ERROR_NO_DEVICE; // No device } if (response & R1_COM_CRC_ERROR) { _deselect(); - debug_if(SD_DBG, "CRC error CMD:%d \n", cmd); + debug_if(SD_DBG, "CRC error CMD:%d response 0x%x \n",cmd, response); return SD_BLOCK_DEVICE_ERROR_CRC; // CRC error } if (response & R1_ILLEGAL_COMMAND) { - debug_if(SD_DBG, "Illegal command CMD:%d\n", cmd); + _deselect(); + debug_if(SD_DBG, "Illegal command CMD:%d response 0x%x\n",cmd, response); if (CMD8_SEND_IF_COND == cmd) { // Illegal command is for Ver1 or not SD Card _card_type = CARD_UNKNOWN; } - _deselect(); return SD_BLOCK_DEVICE_ERROR_UNSUPPORTED; // Command not supported } @@ -1024,4 +1029,5 @@ void SDBlockDevice::_deselect() { _cs = 1; _spi.unlock(); } + #endif /* DEVICE_SPI */