}\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
(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
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