Browse Source

Fix timeouts

main
PaulStoffregen 7 years ago
parent
commit
a6da621578
2 changed files with 18 additions and 13 deletions
  1. +13
    -8
      utility/Sd2Card.cpp
  2. +5
    -5
      utility/Sd2Card.h

+ 13
- 8
utility/Sd2Card.cpp View File

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

+ 5
- 5
utility/Sd2Card.h View File

/** Protect block zero from write if nonzero */ /** Protect block zero from write if nonzero */
#define SD_PROTECT_BLOCK_ZERO 1 #define SD_PROTECT_BLOCK_ZERO 1
/** init timeout ms */ /** init timeout ms */
uint16_t const SD_INIT_TIMEOUT = 2000;
const unsigned int SD_INIT_TIMEOUT = 2000;
/** erase timeout ms */ /** erase timeout ms */
uint16_t const SD_ERASE_TIMEOUT = 10000;
const unsigned int SD_ERASE_TIMEOUT = 10000;
/** read timeout ms */ /** read timeout ms */
uint16_t const SD_READ_TIMEOUT = 300;
const unsigned int SD_READ_TIMEOUT = 300;
/** write time out ms */ /** write time out ms */
uint16_t const SD_WRITE_TIMEOUT = 600;
const unsigned int SD_WRITE_TIMEOUT = 600;
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// card types // card types
/** Standard capacity V1 SD card */ /** Standard capacity V1 SD card */
uint8_t sendWriteCommand(uint32_t blockNumber, uint32_t eraseCount); uint8_t sendWriteCommand(uint32_t blockNumber, uint32_t eraseCount);
void chipSelectHigh(void); void chipSelectHigh(void);
void chipSelectLow(void); void chipSelectLow(void);
uint8_t waitNotBusy(uint16_t timeoutMillis);
uint8_t waitNotBusy(unsigned int timeoutMillis);
uint8_t writeData(uint8_t token, const uint8_t* src); uint8_t writeData(uint8_t token, const uint8_t* src);
uint8_t waitStartBlock(void); uint8_t waitStartBlock(void);
uint8_t setSckRate(uint8_t sckRateID); uint8_t setSckRate(uint8_t sckRateID);

Loading…
Cancel
Save