Browse Source

Merge pull request #3527 from thinkyhead/rc_sd_readblock_cleanup

Improve code in Sd2Card::readBlock
Scott Lahteine 8 years ago
parent
commit
5847738bf9
1 changed files with 22 additions and 29 deletions
  1. 22
    29
      Marlin/Sd2Card.cpp

+ 22
- 29
Marlin/Sd2Card.cpp View File

@@ -383,38 +383,31 @@ fail:
383 383
  * the value zero, false, is returned for failure.
384 384
  */
385 385
 bool Sd2Card::readBlock(uint32_t blockNumber, uint8_t* dst) {
386
-#if ENABLED(SD_CHECK_AND_RETRY)
387
-  uint8_t retryCnt = 3;
388 386
   // use address if not SDHC card
389 387
   if (type() != SD_CARD_TYPE_SDHC) blockNumber <<= 9;
390
-retry2:
391
-  retryCnt --;
392
-  if (cardCommand(CMD17, blockNumber)) {
393
-    error(SD_CARD_ERROR_CMD17);
394
-    if (retryCnt > 0) goto retry;
395
-    goto fail;
396
-  }
397
-  if (!readData(dst, 512)) {
398
-    if (retryCnt > 0) goto retry;
399
-    goto fail;
400
-  }
401
-  return true;
402
-retry:
403
-  chipSelectHigh();
404
-  cardCommand(CMD12, 0);//Try sending a stop command, but ignore the result.
405
-  errorCode_ = 0;
406
-  goto retry2;
407
-#else
408
-  // use address if not SDHC card
409
-  if (type() != SD_CARD_TYPE_SDHC) blockNumber <<= 9;
410
-  if (cardCommand(CMD17, blockNumber)) {
411
-    error(SD_CARD_ERROR_CMD17);
412
-    goto fail;
413
-  }
414
-  return readData(dst, 512);
415
-#endif
416 388
 
417
-fail:
389
+  #if ENABLED(SD_CHECK_AND_RETRY)
390
+    uint8_t retryCnt = 3;
391
+    do {
392
+      if (!cardCommand(CMD17, blockNumber)) {
393
+        if (readData(dst, 512)) return true;
394
+      }
395
+      else
396
+        error(SD_CARD_ERROR_CMD17);
397
+
398
+      if (--retryCnt) break;
399
+
400
+      chipSelectHigh();
401
+      cardCommand(CMD12, 0); // Try sending a stop command, ignore the result.
402
+      errorCode_ = 0;
403
+    } while (true);
404
+  #else
405
+    if (cardCommand(CMD17, blockNumber))
406
+      error(SD_CARD_ERROR_CMD17);
407
+    else
408
+      return readData(dst, 512);
409
+  #endif
410
+
418 411
   chipSelectHigh();
419 412
   return false;
420 413
 }

Loading…
Cancel
Save