Fix terrible performance v6.0.6
authorMichael McMaster <michael@codesrc.com>
Mon, 15 Aug 2016 11:05:38 +0000 (21:05 +1000)
committerMichael McMaster <michael@codesrc.com>
Mon, 15 Aug 2016 11:05:38 +0000 (21:05 +1000)
STM32CubeMX/SCSI2SD-V6/Drivers/STM32F2xx_HAL_Driver/Src/stm32f2xx_hal_sd.c
src/firmware/config.c
src/firmware/disk.c
src/firmware/main.c
src/firmware/scsi.h
src/firmware/scsiPhy.c

index 418163e..8511aa7 100755 (executable)
@@ -1048,6 +1048,27 @@ HAL_SD_ErrorTypedef HAL_SD_WriteBlocks_DMA(SD_HandleTypeDef *hsd, uint32_t *pWri
   }
   else
   {
+    /* MM: Prepare for write */
+    /* Set Block Size for Card */ 
+    sdio_cmdinitstructure.Argument         = (uint32_t)(hsd->RCA << 16);
+    sdio_cmdinitstructure.CmdIndex         = SD_CMD_APP_CMD;
+    SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
+    errorstate = SD_CmdResp1Error(hsd, SD_CMD_APP_CMD);
+    if (errorstate != SD_OK)
+    {
+      return errorstate;
+    }
+    sdio_cmdinitstructure.Argument         = (uint32_t)NumberOfBlocks;
+    sdio_cmdinitstructure.CmdIndex         = SD_CMD_SET_BLOCK_COUNT;
+    SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
+    errorstate = SD_CmdResp1Error(hsd, SD_CMD_SET_BLOCK_COUNT);
+    if (errorstate != SD_OK)
+    {
+      return errorstate;
+    }
+  
+    /* /MM */
+
     /* Send CMD25 WRITE_MULT_BLOCK with argument data address */
     sdio_cmdinitstructure.CmdIndex = SD_CMD_WRITE_MULT_BLOCK;
   }
index 2aed74b..eb6b369 100755 (executable)
@@ -37,7 +37,7 @@
 \r
 #include <string.h>\r
 \r
-static const uint16_t FIRMWARE_VERSION = 0x0605;\r
+static const uint16_t FIRMWARE_VERSION = 0x0606;\r
 \r
 // 1 flash row\r
 static const uint8_t DEFAULT_CONFIG[128] =\r
index b8c54a2..2e69d48 100755 (executable)
@@ -553,14 +553,6 @@ void scsiDiskPoll()
                        likely(scsiDev.phase == DATA_IN) &&\r
                        likely(!scsiDev.resetFlag))\r
                {\r
-                       // Wait for the next DMA interrupt. It's beneficial to halt the\r
-                       // processor to give the DMA controller more memory bandwidth to\r
-                       // work with.\r
-                       if (sdActive && scsiActive)\r
-                       {\r
-                               __WFI();\r
-                       }\r
-\r
                        if (sdActive && sdReadDMAPoll())\r
                        {\r
                                prep += sdActive;\r
@@ -574,11 +566,13 @@ void scsiDiskPoll()
                                // Start an SD transfer if we have space.\r
                                uint32_t startBuffer = prep % buffers;\r
                                uint32_t sectors = totalSDSectors - prep;\r
+#if 0\r
                                if (!scsiActive && prep == i)\r
                                {\r
                                        sectors = 1; // We need to get some data to send ASAP !\r
                                }\r
                                else\r
+#endif\r
                                {\r
                                        uint32_t freeBuffers = buffers - (prep - i);\r
                                        uint32_t contiguousBuffers = buffers - startBuffer;\r
@@ -616,7 +610,6 @@ void scsiDiskPoll()
                        likely(scsiDev.phase == DATA_IN) &&\r
                        likely(!scsiDev.resetFlag))\r
                {\r
-                       __WFI();\r
                }\r
 \r
 \r
index f418d12..67f7956 100755 (executable)
@@ -106,6 +106,9 @@ void mainLoop()
                }\r
                else\r
                {\r
+                       // TODO this hurts performance significantly! Work out why __WFI()\r
+                       // doesn't wake up immediately !\r
+#if 0\r
                        // Wait for our 1ms timer to save some power.\r
                        // There's an interrupt on the SEL signal to ensure we respond\r
                        // quickly to any SCSI commands. The selection abort time is\r
@@ -116,12 +119,13 @@ void mainLoop()
 \r
                        if (!*SCSI_STS_SELECTED)\r
                        {\r
-                               __WFI(); // Will wake on interrupt, regardless of mask\r
+                               //__WFI(); // Will wake on interrupt, regardless of mask\r
                        }\r
                        if (!interruptState)\r
                        {\r
                                __enable_irq();\r
                        }\r
+#endif\r
                }\r
        }\r
        else if (scsiDev.phase >= 0)\r
index 0a12373..2e3a5fa 100755 (executable)
@@ -97,7 +97,8 @@ typedef struct
 
 typedef struct
 {
-       uint8_t data[MAX_SECTOR_SIZE * 2]; // Must be aligned for DMA
+       // TODO reduce this buffer size and add a proper cache
+       uint8_t data[MAX_SECTOR_SIZE * 8]; // Must be aligned for DMA
 
        TargetState targets[S2S_MAX_TARGETS];
        TargetState* target;
index fa586c3..2231089 100755 (executable)
@@ -176,30 +176,32 @@ void
 scsiRead(uint8_t* data, uint32_t count)\r
 {\r
        int i = 0;\r
+\r
+\r
+       uint32_t chunk = ((count - i) > SCSI_FIFO_DEPTH)\r
+               ? SCSI_FIFO_DEPTH : (count - i);\r
+       if (chunk >= 16)\r
+       {\r
+               // DMA is doing 32bit transfers.\r
+               chunk = chunk & 0xFFFFFFF8;\r
+       }\r
+       startScsiRx(chunk);\r
+\r
        while (i < count && likely(!scsiDev.resetFlag))\r
        {\r
-               uint32_t chunk = ((count - i) > SCSI_FIFO_DEPTH)\r
-                       ? SCSI_FIFO_DEPTH : (count - i);\r
+               while (!scsiPhyComplete() && likely(!scsiDev.resetFlag)) {}\r
+               scsiPhyFifoFlip();\r
 \r
-               if (chunk >= 16)\r
+               uint32_t nextChunk = ((count - i - chunk) > SCSI_FIFO_DEPTH)\r
+                       ? SCSI_FIFO_DEPTH : (count - i - chunk);\r
+               if (nextChunk >= 16)\r
                {\r
-                       // DMA is doing 32bit transfers.\r
-                       chunk = chunk & 0xFFFFFFF8;\r
+                       nextChunk = nextChunk & 0xFFFFFFF8;\r
                }\r
-\r
-#if FIFODEBUG\r
-               if (!scsiPhyFifoAltEmpty()) {\r
-                       // Force a lock-up.\r
-                       assertFail();\r
+               if (nextChunk > 0)\r
+               {\r
+                       startScsiRx(nextChunk);\r
                }\r
-#endif\r
-\r
-               startScsiRx(chunk);\r
-               // Wait for the next scsi interrupt (or the 1ms systick)\r
-               __WFI();\r
-\r
-               while (!scsiPhyComplete() && likely(!scsiDev.resetFlag)) {}\r
-               scsiPhyFifoFlip();\r
 \r
                if (chunk < 16)\r
                {\r
@@ -209,15 +211,10 @@ scsiRead(uint8_t* data, uint32_t count)
                {\r
                        scsiReadDMA(data + i, chunk);\r
 \r
-                       // Wait for the next DMA interrupt (or the 1ms systick)\r
-                       // It's beneficial to halt the processor to\r
-                       // give the DMA controller more memory bandwidth to work with.\r
-                       __WFI();\r
                        trace(trace_spinReadDMAPoll);\r
 \r
                        while (!scsiReadDMAPoll() && likely(!scsiDev.resetFlag))\r
                        {\r
-                               __WFI();\r
                        };\r
                }\r
 \r
@@ -230,6 +227,7 @@ scsiRead(uint8_t* data, uint32_t count)
                }\r
 #endif\r
                i += chunk;\r
+               chunk = nextChunk;\r
        }\r
 }\r
 \r
@@ -328,21 +326,15 @@ scsiWrite(const uint8_t* data, uint32_t count)
                        chunk = chunk & 0xFFFFFFF8;\r
                        scsiWriteDMA(data + i, chunk);\r
 \r
-                       // Wait for the next DMA interrupt (or the 1ms systick)\r
-                       // It's beneficial to halt the processor to\r
-                       // give the DMA controller more memory bandwidth to work with.\r
-                       __WFI();\r
                        trace(trace_spinReadDMAPoll);\r
 \r
                        while (!scsiWriteDMAPoll() && likely(!scsiDev.resetFlag))\r
                        {\r
-                               __WFI();\r
-                       };\r
+                       }\r
                }\r
 \r
                while (!scsiPhyComplete() && likely(!scsiDev.resetFlag))\r
                {\r
-                       __WFI();\r
                }\r
 \r
 #if FIFODEBUG\r
@@ -357,7 +349,6 @@ scsiWrite(const uint8_t* data, uint32_t count)
        }\r
        while (!scsiPhyComplete() && likely(!scsiDev.resetFlag))\r
        {\r
-               __WFI();\r
        }\r
 \r
 #if FIFODEBUG\r