3

I have a SD card reader that is working fine exept that sometimes SD.begin fails for no apparent reason. To work around this issue I have put the SD.begin in a while loop and then I display the error on my LCD. When the error happens I then remove and reinsert the card witch works usualy on the first or second try. I have never seen any problem ones the card is initilized. Does anyone has any idea what might be the problem?

  • Which reader? Link? Possibly you are running too fast a clock for it. There are means of slowing down the SD library. – Nick Gammon Mar 3 '16 at 22:14
  • arduino.cc/en/Main/GTFT I do not use the TFT but only the SD card reader. If SPI where to fast should I not see random errors after a successful init when reading and writing to it to? – Aoaoaoappa Mar 4 '16 at 1:33
  • I don't know. There have been reports that some SD cards work better than others. During initialization there might be negotiation about which protocol to use. Possibly try adding a small delay in setup to give the card time to power up. – Nick Gammon Mar 4 '16 at 1:39
2

I have seen SD cards that do not like this section of code:

  // command to go idle in SPI mode
  while (cardCommand(CMD0, 0) != R1_IDLE_STATE) {
    if (((uint16_t)millis() - t0) > SD_INIT_TIMEOUT) {
      error(SD_CARD_ERROR_CMD0);
      goto fail;
    }
  }

Some SD cards (mine, of course) seem to need a little quiet time to finish their INIT process. Adding a delay of <something> made the INIT process complete successfully, first time through the loop.

I'm guessing that the SD card spends so much time handling the repeated CMD0 commands (continuously sent by above loop) that they don't get to spend time on the INIT process. IIRC, the standard says the card is allowed to take up to 2 seconds to INIT (!). I can't recall, but I think I tried 150ms or so:

  // command to go idle in SPI mode
  while (cardCommand(CMD0, 0) != R1_IDLE_STATE) {
    delay( 150 ); // let SD get some time to work
    if (((uint16_t)millis() - t0) > SD_INIT_TIMEOUT) {
      error(SD_CARD_ERROR_CMD0);
      goto fail;
    }
  }

I do recall that reinserting the card would sometimes make it work. I have suggested this to at least one SD card library maintainer, but I think we're all reluctant to impose a delay on all cards. OTOH, it is just during ::begin.

It might work for you, though. The above snippet is from SdSpiCard.cpp, but you may have to go looking for it in other places, depending on which library and version you're using.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.