Simplify SD card busy waiting
[SCSI2SD-V6.git] / src / firmware / disk.c
index c9c7ae4596c5c3e7f0b336b453618477ff05d919..45b18aa0cf86bbd65bf4468972a971961983cdb0 100755 (executable)
@@ -693,13 +693,13 @@ void scsiDiskPoll()
             {\r
                 // Wait while keeping BSY.\r
             }\r
-       }\r
+        }\r
 \r
         HAL_SD_CardStateTypeDef cardState = HAL_SD_GetCardState(&hsd);\r
         while (cardState == HAL_SD_CARD_PROGRAMMING || cardState == HAL_SD_CARD_SENDING) \r
         {\r
             cardState = HAL_SD_GetCardState(&hsd);\r
-        }\r
+         }\r
 \r
         // We've finished transferring the data to the FPGA, now wait until it's\r
         // written to he SCSI bus.\r
@@ -787,14 +787,11 @@ void scsiDiskPoll()
                 while (j < sectors && !scsiDev.resetFlag)\r
                 {\r
                     if (sdActive &&\r
-                        HAL_SD_GetState(&hsd) != HAL_SD_STATE_BUSY)\r
+                        HAL_SD_GetState(&hsd) != HAL_SD_STATE_BUSY &&\r
+                        !sdIsBusy())\r
                     {\r
-                        HAL_SD_CardStateTypeDef tmpCardState = HAL_SD_GetCardState(&hsd);\r
-                        if (tmpCardState != HAL_SD_CARD_PROGRAMMING)\r
-                        {\r
-                            j += sdActive;\r
-                            sdActive = 0;\r
-                        }\r
+                        j += sdActive;\r
+                        sdActive = 0;\r
                     }\r
                     if (!sdActive && ((prep - j) > 0))\r
                     {\r
@@ -850,14 +847,12 @@ void scsiDiskPoll()
                     }\r
                 }\r
 \r
-                HAL_SD_CardStateTypeDef cardState = HAL_SD_GetCardState(&hsd);\r
-                while ((cardState == HAL_SD_CARD_PROGRAMMING || cardState == HAL_SD_CARD_RECEIVING) &&\r
+                while (sdIsBusy() &&\r
                     s2s_elapsedTime_ms(dmaFinishTime) < 180)\r
                 {\r
                     // Wait while the SD card is writing buffer to flash\r
                     // The card may remain in the RECEIVING state (even though it's programming) if\r
                     // it has buffer space to receive more data available.\r
-                    cardState = HAL_SD_GetCardState(&hsd);\r
                 }\r
 \r
                 if (!disconnected && \r
@@ -875,7 +870,11 @@ void scsiDiskPoll()
                     clearBSY = process_MessageIn(0); // Will go to BUS_FREE state but keep BSY asserted.\r
                 }\r
 \r
-                cardState = HAL_SD_GetCardState(&hsd);\r
+                // Wait while the SD card is writing buffer to flash\r
+                // The card may remain in the RECEIVING state (even though it's programming) if\r
+                // it has buffer space to receive more data available.\r
+                while (sdIsBusy()) {}\r
+                HAL_SD_CardStateTypeDef cardState = HAL_SD_GetCardState(&hsd);\r
                 while (cardState == HAL_SD_CARD_PROGRAMMING || cardState == HAL_SD_CARD_RECEIVING) \r
                 {\r
                     // Wait while the SD card is writing buffer to flash\r