Browse Source

Fix issue with SCSI timeouts if writes take too long

Michael McMaster 4 years ago
parent
commit
7461f6c4a1

+ 14 - 4
lib/SCSI2SD/src/firmware/bsp_driver_sd.c

@@ -192,6 +192,7 @@ __weak void BSP_SD_DetectCallback(void)
   * @param  NumOfBlocks: Number of SD blocks to read 
   * @retval SD status
   */
+/*
 uint8_t BSP_SD_ReadBlocks(uint8_t *pData, uint64_t BlockAddr, uint32_t NumOfBlocks)
 {
   if(HAL_SD_ReadBlocks_DMA(&hsd, pData, BlockAddr, NumOfBlocks) != HAL_OK)  
@@ -199,7 +200,7 @@ uint8_t BSP_SD_ReadBlocks(uint8_t *pData, uint64_t BlockAddr, uint32_t NumOfBloc
     return MSD_ERROR;
   }
   return MSD_OK;
-}
+}*/
 
 /**
   * @brief  Writes block(s) to a specified address in an SD card, in polling mode. 
@@ -208,14 +209,14 @@ uint8_t BSP_SD_ReadBlocks(uint8_t *pData, uint64_t BlockAddr, uint32_t NumOfBloc
   * @param  NumOfBlocks: Number of SD blocks to write
   * @retval SD status
   */
-uint8_t BSP_SD_WriteBlocks(uint8_t *pData, uint64_t BlockAddr, uint32_t NumOfBlocks)
+/*uint8_t BSP_SD_WriteBlocks(uint8_t *pData, uint64_t BlockAddr, uint32_t NumOfBlocks)
 {
   if(HAL_SD_WriteBlocks_DMA(&hsd, pData, BlockAddr, NumOfBlocks) != HAL_OK)  
   {
     return MSD_ERROR;
   }
   return MSD_OK;
-}
+}*/
 
 /**
   * @brief  Reads block(s) from a specified address in an SD card, in DMA mode. 
@@ -283,7 +284,16 @@ uint8_t BSP_SD_WriteBlocks_DMA(uint8_t *pData, uint64_t BlockAddr, uint32_t NumO
       SD_state = MSD_OK;
     }
 
-    while (HAL_SD_GetCardState(&hsd) == HAL_SD_CARD_PROGRAMMING) {}
+    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
+        // 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);
+    }
+
   }
   
   return SD_state; 

+ 2 - 2
lib/SCSI2SD/src/firmware/bsp_driver_sd.h

@@ -88,8 +88,8 @@ uint8_t BSP_SD_Init(void);
 uint8_t BSP_SD_ITConfig(void);
 void BSP_SD_DetectIT(void);
 __weak void BSP_SD_DetectCallback(void);
-uint8_t BSP_SD_ReadBlocks(uint8_t *pData, uint64_t BlockAddr, uint32_t NumOfBlocks);
-uint8_t BSP_SD_WriteBlocks(uint8_t *pData, uint64_t BlockAddr, uint32_t NumOfBlocks);
+//uint8_t BSP_SD_ReadBlocks(uint8_t *pData, uint64_t BlockAddr, uint32_t NumOfBlocks);
+//uint8_t BSP_SD_WriteBlocks(uint8_t *pData, uint64_t BlockAddr, uint32_t NumOfBlocks);
 uint8_t BSP_SD_ReadBlocks_DMA(uint8_t *pData, uint64_t BlockAddr, uint32_t NumOfBlocks);
 uint8_t BSP_SD_WriteBlocks_DMA(uint8_t *pData, uint64_t BlockAddr, uint32_t NumOfBlocks);
 //uint8_t BSP_SD_Erase(uint64_t StartAddr, uint64_t EndAddr);

+ 16 - 1
lib/SCSI2SD/src/firmware/disk.c

@@ -859,6 +859,16 @@ void scsiDiskPoll()
 					// Wait while keeping BSY.
 				}
 
+                HAL_SD_CardStateTypeDef cardState = HAL_SD_GetCardState(&hsd);
+                while ((cardState == HAL_SD_CARD_PROGRAMMING || cardState == HAL_SD_CARD_RECEIVING) &&
+					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 (i + sectors >= totalSDSectors &&
 					!underrun &&
 					(!parityError || !enableParity))
@@ -878,9 +888,14 @@ void scsiDiskPoll()
 				{
 					// Wait while keeping BSY.
 				}
-                while (HAL_SD_GetCardState(&hsd) == HAL_SD_CARD_PROGRAMMING) 
+
+                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
+                    // 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 (underrun && (!parityError || !enableParity))