Improve code in Sd2Card::readBlock

This commit is contained in:
Scott Lahteine 2016-04-15 20:42:31 -07:00
parent 8429ae6beb
commit 40050db210

View File

@ -383,38 +383,31 @@ fail:
* the value zero, false, is returned for failure. * the value zero, false, is returned for failure.
*/ */
bool Sd2Card::readBlock(uint32_t blockNumber, uint8_t* dst) { bool Sd2Card::readBlock(uint32_t blockNumber, uint8_t* dst) {
// use address if not SDHC card
if (type() != SD_CARD_TYPE_SDHC) blockNumber <<= 9;
#if ENABLED(SD_CHECK_AND_RETRY) #if ENABLED(SD_CHECK_AND_RETRY)
uint8_t retryCnt = 3; uint8_t retryCnt = 3;
// use address if not SDHC card do {
if (type() != SD_CARD_TYPE_SDHC) blockNumber <<= 9; if (!cardCommand(CMD17, blockNumber)) {
retry2: if (readData(dst, 512)) return true;
retryCnt --; }
if (cardCommand(CMD17, blockNumber)) { else
error(SD_CARD_ERROR_CMD17); error(SD_CARD_ERROR_CMD17);
if (retryCnt > 0) goto retry;
goto fail; if (--retryCnt) break;
}
if (!readData(dst, 512)) {
if (retryCnt > 0) goto retry;
goto fail;
}
return true;
retry:
chipSelectHigh(); chipSelectHigh();
cardCommand(CMD12, 0);//Try sending a stop command, but ignore the result. cardCommand(CMD12, 0); // Try sending a stop command, ignore the result.
errorCode_ = 0; errorCode_ = 0;
goto retry2; } while (true);
#else #else
// use address if not SDHC card if (cardCommand(CMD17, blockNumber))
if (type() != SD_CARD_TYPE_SDHC) blockNumber <<= 9;
if (cardCommand(CMD17, blockNumber)) {
error(SD_CARD_ERROR_CMD17); error(SD_CARD_ERROR_CMD17);
goto fail; else
}
return readData(dst, 512); return readData(dst, 512);
#endif #endif
fail:
chipSelectHigh(); chipSelectHigh();
return false; return false;
} }