|
|
|
|
|
|
|
|
type_ = 0;
|
|
|
type_ = 0;
|
|
|
chipSelectPin_ = chipSelectPin;
|
|
|
chipSelectPin_ = chipSelectPin;
|
|
|
// 16-bit init start time allows over a minute
|
|
|
// 16-bit init start time allows over a minute
|
|
|
uint16_t t0 = (uint16_t)millis();
|
|
|
|
|
|
|
|
|
unsigned int t0 = millis();
|
|
|
uint32_t arg;
|
|
|
uint32_t arg;
|
|
|
|
|
|
|
|
|
digitalWrite(chipSelectPin_, HIGH);
|
|
|
digitalWrite(chipSelectPin_, HIGH);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// command to go idle in SPI mode
|
|
|
// command to go idle in SPI mode
|
|
|
while ((status_ = cardCommand(CMD0, 0)) != R1_IDLE_STATE) {
|
|
|
while ((status_ = cardCommand(CMD0, 0)) != R1_IDLE_STATE) {
|
|
|
if (((uint16_t)(millis() - t0)) > SD_INIT_TIMEOUT) {
|
|
|
|
|
|
|
|
|
unsigned int d = millis() - t0;
|
|
|
|
|
|
if (d > SD_INIT_TIMEOUT) {
|
|
|
goto fail; // SD_CARD_ERROR_CMD0
|
|
|
goto fail; // SD_CARD_ERROR_CMD0
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
while ((status_ = cardAcmd(ACMD41, arg)) != R1_READY_STATE) {
|
|
|
while ((status_ = cardAcmd(ACMD41, arg)) != R1_READY_STATE) {
|
|
|
// check for timeout
|
|
|
// check for timeout
|
|
|
if (((uint16_t)(millis() - t0)) > SD_INIT_TIMEOUT) {
|
|
|
|
|
|
|
|
|
unsigned int d = millis() - t0;
|
|
|
|
|
|
if (d > SD_INIT_TIMEOUT) {
|
|
|
goto fail; // SD_CARD_ERROR_ACMD41
|
|
|
goto fail; // SD_CARD_ERROR_ACMD41
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
}
|
|
|
}
|
|
|
//------------------------------------------------------------------------------
|
|
|
//------------------------------------------------------------------------------
|
|
|
// wait for card to go not busy
|
|
|
// wait for card to go not busy
|
|
|
uint8_t Sd2Card::waitNotBusy(uint16_t timeoutMillis) {
|
|
|
|
|
|
uint16_t t0 = millis();
|
|
|
|
|
|
|
|
|
uint8_t Sd2Card::waitNotBusy(unsigned int timeoutMillis) {
|
|
|
|
|
|
unsigned int t0 = millis();
|
|
|
|
|
|
unsigned int d;
|
|
|
do {
|
|
|
do {
|
|
|
if (spiRec() == 0XFF) return true;
|
|
|
if (spiRec() == 0XFF) return true;
|
|
|
|
|
|
d = millis() - t0;
|
|
|
}
|
|
|
}
|
|
|
while (((uint16_t)millis() - t0) < timeoutMillis);
|
|
|
|
|
|
|
|
|
while (d < timeoutMillis);
|
|
|
return false;
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
//------------------------------------------------------------------------------
|
|
|
//------------------------------------------------------------------------------
|
|
|
/** Wait for start block token */
|
|
|
/** Wait for start block token */
|
|
|
uint8_t Sd2Card::waitStartBlock(void) {
|
|
|
uint8_t Sd2Card::waitStartBlock(void) {
|
|
|
uint16_t t0 = millis();
|
|
|
|
|
|
|
|
|
unsigned int t0 = millis();
|
|
|
while ((status_ = spiRec()) == 0XFF) {
|
|
|
while ((status_ = spiRec()) == 0XFF) {
|
|
|
if (((uint16_t)millis() - t0) > SD_READ_TIMEOUT) {
|
|
|
|
|
|
|
|
|
unsigned int d = millis() - t0;
|
|
|
|
|
|
if (d > SD_READ_TIMEOUT) {
|
|
|
return false; // SD_CARD_ERROR_READ_TIMEOUT
|
|
|
return false; // SD_CARD_ERROR_READ_TIMEOUT
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|