Simplify SD card busy waiting
authorMichael McMaster <michael@codesrc.com>
Tue, 18 May 2021 11:13:21 +0000 (21:13 +1000)
committerMichael McMaster <michael@codesrc.com>
Tue, 18 May 2021 11:13:21 +0000 (21:13 +1000)
src/firmware/bsp_driver_sd.c
src/firmware/disk.c
src/firmware/sd.c
src/firmware/sd.h

index e972c98ff818aa38b724cd201774f82fac5ec4c4..e69c7183844bc18c842d873173506c09f9536ebd 100755 (executable)
@@ -34,6 +34,7 @@
 /* USER CODE BEGIN 0 */
 /* Includes ------------------------------------------------------------------*/
 #include "bsp_driver_sd.h"
+#include "sd.h"
 
 /* Extern variables ---------------------------------------------------------*/ 
   
@@ -283,15 +284,8 @@ uint8_t BSP_SD_WriteBlocks_DMA(uint8_t *pData, uint64_t BlockAddr, uint32_t NumO
     {
         while (HAL_SD_GetState(&hsd) == HAL_SD_STATE_BUSY) {}
 
-        HAL_SD_CardStateTypeDef cardState = HAL_SD_GetCardState(&hsd);
-        while (cardState == HAL_SD_CARD_PROGRAMMING) 
-        {   
-            // Wait while the SD card is writing buffer to flash
-            // The card may remain in the RECEIVING state (even though it's programming) if
-            // it has buffer space to receive more data available.
-
-            cardState = HAL_SD_GetCardState(&hsd);
-        }
+        // Wait while the SD card has no buffer space
+        while (sdIsBusy()) {}
 
         HAL_SD_WriteBlocks_Data(&hsd, pData + (i * 512));
     }
@@ -312,6 +306,9 @@ uint8_t BSP_SD_WriteBlocks_DMA(uint8_t *pData, uint64_t BlockAddr, uint32_t NumO
       SD_state = MSD_OK;
     }
 
+    // Wait while the SD card is in the PROGRAMMING state.
+    while (sdIsBusy()) {}
+
     HAL_SD_CardStateTypeDef cardState = HAL_SD_GetCardState(&hsd);
     while (cardState == HAL_SD_CARD_PROGRAMMING || cardState == HAL_SD_CARD_RECEIVING) 
     {
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
index 1f2a27bc2fed8d916701970ec552f1d364ed505d..0eff83d373431eb0baf5bfbf693f1187b3de44d8 100755 (executable)
@@ -214,3 +214,11 @@ int sdInit()
        return result;\r
 }\r
 \r
+// Return 1 if the SD card has no buffer space left for writes and/or is\r
+// in the programming state.\r
+int sdIsBusy()\r
+{\r
+    // Busy while DAT0 is held low.\r
+    return HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_8) == 0;\r
+}\r
+\r
index b82f11920835d690a20b0fc408587cc742bc18f0..5a1d79a1508f75f293fcbf0291aea01c7bc2ecb0 100755 (executable)
@@ -38,5 +38,6 @@ void sdReadDMA(uint32_t lba, uint32_t sectors, uint8_t* outputBuffer);
 int sdReadDMAPoll(uint32_t remainingSectors);
 void sdCompleteTransfer();
 
+int sdIsBusy();
 
 #endif