Improve IRQ handler responsiveness
[SCSI2SD-V6.git] / src / firmware / disk.c
index a0bcf491600c3ed3965c6e18a8695325010c8a9d..26962d939536a2f7aefdc101c6c17dfc4ce44a2c 100755 (executable)
@@ -657,7 +657,8 @@ void scsiDiskPoll()
                                }\r
                        }\r
 \r
-                       if ((prep - i) > 0)\r
+                       if (((prep - i) > 0) &&\r
+                scsiFifoReady())\r
                        {\r
                                int dmaBytes = SD_SECTOR_SIZE;\r
                                if ((i % sdPerScsi) == (sdPerScsi - 1))\r
@@ -679,20 +680,26 @@ void scsiDiskPoll()
                        phaseChangeDelayUs = 0;\r
                }\r
 \r
+        // Wait for the SD transfer to complete before we disable IRQs.\r
+        // (Otherwise some cards will cause an error if we don't sent the\r
+        // stop transfer command via the DMA complete handler in time)\r
+               while (HAL_SD_GetState(&hsd) == HAL_SD_STATE_BUSY)\r
+               {\r
+                       // Wait while keeping BSY.\r
+               }\r
+\r
                // We've finished transferring the data to the FPGA, now wait until it's\r
                // written to he SCSI bus.\r
-               __disable_irq();\r
                while (!scsiPhyComplete() &&\r
                        likely(scsiDev.phase == DATA_IN) &&\r
                        likely(!scsiDev.resetFlag))\r
                {\r
-                       __WFI();\r
-               }\r
-               __enable_irq();\r
-\r
-               while (HAL_SD_GetState(&hsd) == HAL_SD_STATE_BUSY)\r
-               {\r
-                       // Wait while keeping BSY.\r
+               __disable_irq();\r
+                   if (!scsiPhyComplete() && likely(!scsiDev.resetFlag))\r
+            {\r
+                           __WFI();\r
+            }\r
+                   __enable_irq();\r
                }\r
 \r
                if (scsiDev.phase == DATA_IN)\r