Browse Source

Simplify SD card busy waiting

Michael McMaster 4 years ago
parent
commit
a299c591e6

+ 6 - 9
lib/SCSI2SD/src/firmware/bsp_driver_sd.c

@@ -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) 
     {

+ 12 - 13
lib/SCSI2SD/src/firmware/disk.c

@@ -693,13 +693,13 @@ void scsiDiskPoll()
             {
                 // Wait while keeping BSY.
             }
-	}
+        }
 
         HAL_SD_CardStateTypeDef cardState = HAL_SD_GetCardState(&hsd);
         while (cardState == HAL_SD_CARD_PROGRAMMING || cardState == HAL_SD_CARD_SENDING) 
         {
             cardState = HAL_SD_GetCardState(&hsd);
-        }
+         }
 
         // We've finished transferring the data to the FPGA, now wait until it's
         // written to he SCSI bus.
@@ -787,14 +787,11 @@ void scsiDiskPoll()
                 while (j < sectors && !scsiDev.resetFlag)
                 {
                     if (sdActive &&
-                        HAL_SD_GetState(&hsd) != HAL_SD_STATE_BUSY)
+                        HAL_SD_GetState(&hsd) != HAL_SD_STATE_BUSY &&
+                        !sdIsBusy())
                     {
-                        HAL_SD_CardStateTypeDef tmpCardState = HAL_SD_GetCardState(&hsd);
-                        if (tmpCardState != HAL_SD_CARD_PROGRAMMING)
-                        {
-                            j += sdActive;
-                            sdActive = 0;
-                        }
+                        j += sdActive;
+                        sdActive = 0;
                     }
                     if (!sdActive && ((prep - j) > 0))
                     {
@@ -850,14 +847,12 @@ void scsiDiskPoll()
                     }
                 }
 
-                HAL_SD_CardStateTypeDef cardState = HAL_SD_GetCardState(&hsd);
-                while ((cardState == HAL_SD_CARD_PROGRAMMING || cardState == HAL_SD_CARD_RECEIVING) &&
+                while (sdIsBusy() &&
                     s2s_elapsedTime_ms(dmaFinishTime) < 180)
                 {
                     // 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);
                 }
 
                 if (!disconnected && 
@@ -875,7 +870,11 @@ void scsiDiskPoll()
                     clearBSY = process_MessageIn(0); // Will go to BUS_FREE state but keep BSY asserted.
                 }
 
-                cardState = HAL_SD_GetCardState(&hsd);
+                // 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.
+                while (sdIsBusy()) {}
+                HAL_SD_CardStateTypeDef cardState = HAL_SD_GetCardState(&hsd);
                 while (cardState == HAL_SD_CARD_PROGRAMMING || cardState == HAL_SD_CARD_RECEIVING) 
                 {
                     // Wait while the SD card is writing buffer to flash

+ 8 - 0
lib/SCSI2SD/src/firmware/sd.c

@@ -214,3 +214,11 @@ int sdInit()
 	return result;
 }
 
+// Return 1 if the SD card has no buffer space left for writes and/or is
+// in the programming state.
+int sdIsBusy()
+{
+    // Busy while DAT0 is held low.
+    return HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_8) == 0;
+}
+

+ 1 - 0
lib/SCSI2SD/src/firmware/sd.h

@@ -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