Initializing SD Card always fails on first attempt - why?
ags
Posts: 386
I've developed a highly-specialized SD card driver, and had to make it very lean to fit in cog RAM. It works, but I see that every time I first insert an SD card, the initialization process fails. If I run it again, it will succeed each subsequent time.
I reviewed the fsrw code by lonesock et al, and notice that implementation tries up to 10 times to reset ("GO_IDLE") the SD card. Why is this? If a card is removed (from power) and re-inserted, isn't it then in a known state?
The failure I see is always at the first step, CMD0. The return error code varies, but after trying again it will succeed. I suppose I can just loop through this command several times until it fails, but this type of "it works but I don't know why" solution seems to always come back with a vengeance.
Any insight into this? Thanks.
I reviewed the fsrw code by lonesock et al, and notice that implementation tries up to 10 times to reset ("GO_IDLE") the SD card. Why is this? If a card is removed (from power) and re-inserted, isn't it then in a known state?
The failure I see is always at the first step, CMD0. The return error code varies, but after trying again it will succeed. I suppose I can just loop through this command several times until it fails, but this type of "it works but I don't know why" solution seems to always come back with a vengeance.
Any insight into this? Thanks.
Comments
I'm just taking a wild guess here. Do you have a line of code that checks for the presence of an SD card in the slot and did this presence-detection get slipped down in your code someplace so it's not getting satisfied before you start all your other initializations on the first try, etc.?
2 - that was a good idea. I do detect presence of the card (don't recall which pin has the pullup) but it's the first thing I do.
3 - I'm not using or supporting SDIO cards, just SD (SDHC, to be precise). But this is more along the line of what I was wondering. I have read and re-read the SDC spec many times, yet I haven't noticed any discussion of a card being "stuck" in a particular mode. I can see that if in the middle of a block operation the card was removed that might leave it in an unusual state, but would expect it to always return to a known starting state when power is applied.
It's OK
I was just curious. I know for example that the Spinneret board has all pins hooked up for standard SD mode, and it would be interesting to see how that would work.
Our drivers should set SDIO (not SD mode) on first use to avoid trouble ... whether the all propeller SDIO SPI drivers do or not is unclear.
Though, today, I did take the time to look at a large number of existing Open source drivers for SD Cards on many different MCUs and boards and they all seem to loop on CMD8 until they succeed, so I would say that it is probably an issue with the SDCard manufactures implementations. My drivers do this just to be sure to operate correctly, though before today I had never took the time to look at so many other sources.
Many of the SD Card HowTos for many different devices say that this is what you do though fail to say why.
Though thank you for raising the question.
Would you be so kind as to post links to the SD Card HowTos you reference? Thanks.
Maybe this is not related. As I was working on the RAISD driver I had a prototype build by Kenichi (@MacTuxLin).
The funny thing was that rebooting (switch power on/off) failed to Init the SD cards - when USB (Prop Plug) was connected.
It turned out that the Prop Plug was powering the board thru rx/tx (just minimal ... VERY dim led showed still power there)
My solution (during development) was to either unplug the Prop Plug or remove the SDs and reinsert them.
The available SD documentation states that ALL SD cards will startup in SD mode (not SPI) and have to be initialized to SPI direct after power up or reset.
Reading your description I wonder if you insert the SD into a powered up board?
FSWR (and lonesocks super driver) do not check for sd-change (KYEs Fat_Engine does - maybe you can look at his code at the beginning of the PASM part)
So - just a guess - Does your initialization is maybe TO LATE to switch the SD card into SPI mode and thus failing?
Since the SD card association published the 4 bit mode description I am pondering over a non SPI driver.
But as far as I understand the documentation switching OFF the checksum just works in SPI mode.
while receiving we can just ignore the checksum. But when sending CMDs or Data that checksum needs to be calculated for EVERY DATALINE (BIT) SEPERATLY.
I still think it is worth a try - even if writing may be slower - reading will be faster for sure ...
Enjoy!
Mike
I just took a look at the Spinneret schematic and noticed that all four SPI lines (MISO, MOSI, SCLK and SCS) have pullup resistors - I have none. I wonder if that is a factor. I wouldn't expect so, unlike an I2C interface.
Well here is a good one (targets the AVR though still quite generic).
http://elm-chan.org/docs/mmc/mmc_e.html
and another(this one showing a scope of the init):
http://www.esawdust.com/blog/serial/files/SPI_SD_MMC_ATMega128_AVR.html
There are many more, though I no longer have them bookmarked.
Aonther problem I found is that the card has to be forced to release the DO pin by sending a number of clocks. This doesn't sound like the problem but could try it anyway.
IMHO you really need a propplug or equivalent to debug. Do you have any USB-TTL boards? There are lots of cheapies using the CP2102??? chip. You just eed to add the DTR (or RTS line with the transistor reset circuit which is simple and basic - almost any generic npn transistor, 10K, 10nF.
The code is sending the correct signals (AFAIK, given my understanding of the SDC specification) and works except for the first time the card is powered up. It's just that the card does not respond with the expected $01 response. I have not looked at the VDD pin with an oscope to see if there is noise/ringing/glitches that may point to the decoupling/bypass capacitor problem.
Thanks for the suggestions.
It is for at people not fold SD recomendations on poling a unused SD pins High
I see someone has already mentionde 10uH Capacitor