embedded-sdmmc-rs icon indicating copy to clipboard operation
embedded-sdmmc-rs copied to clipboard

Add the option to skip waiting in the init sequence when issuing CMD0.

Open jbeaurivage opened this issue 2 years ago • 5 comments

Looking at #32 and #33, at least some cards get stuck in TimeoutWaitNotBusy when trying to initialize them. #32 provides a fix, however it seems to break other cards' init sequence.

I suggest adding an option in AcquireOpts to use this modified init sequence, with the default being to use the "regular" sequence.

jbeaurivage avatar Sep 26 '22 17:09 jbeaurivage

It would awkward if you needed two versions of your code to work with the widest range of SD cards. My general approach has been "Do what Arduino does" - so I checked, and they don't do this.

Their init code is: https://github.com/greiman/SdFat/blob/master/src/SdCard/SdSpiCard.cpp#L170

Maybe we should add this loop of sending 255 lots of 0xFF to clear the card instead?

  // command to go idle in SPI mode
  for (uint8_t i = 1;; i++) {
    if (cardCommand(CMD0, 0) == R1_IDLE_STATE) {
      break;
    }
    if (i == SD_CMD0_RETRY) {
      error(SD_CARD_ERROR_CMD0);
      goto fail;
    }
    // Force any active transfer to end for an already initialized card.
    for (uint8_t j = 0; j < 0XFF; j++) {
      spiSend(0XFF);
    }
  }

thejpster avatar Oct 06 '22 21:10 thejpster

That would be even better. Just to clarify, that sequence would always run, without an option to skip it? I'll try to test it out with a few cards next week.

jbeaurivage avatar Oct 08 '22 01:10 jbeaurivage

Well, it doesn't happen if your card returns IDLE on the first attempt. But every time it doesn't return IDLE, it gets flushed.

thejpster avatar Oct 16 '22 14:10 thejpster

Does this work? I guess skip_wait_not_busy is no longer required if so?

thejpster avatar Oct 17 '22 19:10 thejpster

I'm not sure if it works yet. Getting my cards to fail to initialize is somewhat hard; it happens intermittently and is difficult to reproduce reliably. I'll have to do some more testing, but what I can say is I haven't had an initialization fail using this new method. But you're right that skip_wait_not_busy won't be needed anymore if it works.

jbeaurivage avatar Oct 18 '22 00:10 jbeaurivage