Improvements to SD media detection, esp. when using nor flash on 5.2
authorMichael McMaster <michael@codesrc.com>
Thu, 11 Mar 2021 10:33:55 +0000 (20:33 +1000)
committerMichael McMaster <michael@codesrc.com>
Thu, 11 Mar 2021 10:33:55 +0000 (20:33 +1000)
software/SCSI2SD/src/sd.c

index e1056abd54826412e6a2ef30287d8ba88f8c17bc..e54b5060da4b72d1c7d0f6cb7143c2f990f8b48a 100755 (executable)
@@ -977,6 +977,24 @@ void sdPoll()
        }\r
 }\r
 \r
+static int\r
+sdIsCardPresent()\r
+{\r
+       // The CS line is pulled high by the SD card.\r
+       // De-assert the line, and check if it's high.\r
+       // This isn't foolproof as it'll be left floating without\r
+       // an SD card. We can't use the built-in pull-down resistor as it will\r
+       // overpower the SD pullup resistor.\r
+       SD_CS_Write(0);\r
+       SD_CS_SetDriveMode(SD_CS_DM_DIG_HIZ);\r
+\r
+       // Delay extended to work with 60cm cables running cards at 2.85V\r
+       CyDelayCycles(128);\r
+       int cs = SD_CS_Read();\r
+       SD_CS_SetDriveMode(SD_CS_DM_STRONG);\r
+       return cs;\r
+}\r
+\r
 void sdCheckPresent()\r
 {\r
        static int firstCheck = 1;\r
@@ -985,31 +1003,21 @@ void sdCheckPresent()
                (sdIOState == SD_IDLE) &&\r
                (sdCmdState == CMD_STATE_IDLE))\r
        {\r
-               // The CS line is pulled high by the SD card.\r
-               // De-assert the line, and check if it's high.\r
-               // This isn't foolproof as it'll be left floating without\r
-               // an SD card. We can't use the built-in pull-down resistor as it will\r
-               // overpower the SD pullup resistor.\r
-               SD_CS_Write(0);\r
-               SD_CS_SetDriveMode(SD_CS_DM_DIG_HIZ);\r
-\r
-               // Delay reduced for v5.2 board, no support for extension cables.\r
-               CyDelayCycles(32);\r
-               uint8_t cs = SD_CS_Read();\r
-               SD_CS_SetDriveMode(SD_CS_DM_STRONG);\r
+               int cs = sdIsCardPresent();\r
 \r
                if (cs && !(sdCard.dev.mediaState & MEDIA_PRESENT))\r
                {\r
                        static int firstInit = 1;\r
 \r
-                       // Debounce, except on startup if the card is present at\r
+                       // Debounce. Quicker if the card is present at\r
                        // power on\r
-                       if (!firstCheck)\r
+                       for (int i = 0; cs && (i < firstCheck ? 2 : 50); ++i)\r
                        {\r
-                               CyDelay(250);\r
+                               cs = sdIsCardPresent();\r
+                               CyDelay(5);\r
                        }\r
 \r
-                       if (sdInit())\r
+                       if (cs && sdInit())\r
                        {\r
                                sdCard.dev.mediaState |= MEDIA_PRESENT | MEDIA_INITIALISED;\r
 \r
@@ -1093,8 +1101,8 @@ static int sd_pollMediaChange(S2S_Device* dev)
        SdCard* sdCardDevice = (SdCard*)dev;\r
        if (elapsedTime_ms(sdCardDevice->lastPollMediaTime) > 200)\r
        {\r
-               sdCardDevice->lastPollMediaTime = getTime_ms();\r
                sdCheckPresent();\r
+               sdCardDevice->lastPollMediaTime = getTime_ms();\r
                return 0;\r
        }\r
        else\r