Go Down

Topic: SOLVED - SdFat Error Code 0XA - multiple devices on SPI bus (Read 3643 times) previous topic - next topic

hydrocontrol

Hi,

it took me a while to track this down, so I thought it would be good to share this, in case other people run into similar problems.

The setup is a SD card reader and a LCD display on the SPI bus of an arduino.
The display refreshes (receives data over SPI) every second or so.
SdFat library by Bill Greiman (not the native SD library)

If you...
- pull the SD chip select CS pin high (deactivate)
- insert a SD card
    (in the meantime, there might be some SPI communicatin to the display)
- pull the display CS pin high (deactivate)
- run the SD.begin function (which will pull the SD chip select pin low by itself)
-> SD card initialisation will fail with the SD errorCode: 0XA,0X1


The problem is that even though the SD CS pin is high, the data on the SPI MOSI bus, which is intended for the display, still somehow confuses the SD card, even before we run SD.begin

The solution is to first stop display activity completely, (no SPI communication),
then insert the SD card
then run the SD.begin function -> Success.

After that, switching between SD and display with the help of the CS pins works flawlessly.

Once the SD card is in the above error state, the only way to get it working again is to eject it and re-insert it.


Other things to watch out for:
- the SD.begin function apparently lowers the SPI bus speed, if the initial attempt to talk to the SD fails. Once the initialization process has finally failed, it does not restore the old SPI settings.


Error Code detail:
uint8_t const SD_CARD_ERROR_ACMD41 = 0XA
ACMD41 initialization process timeout


Used parts:
  • Arduino Mega
  • LC Studio SD card adapter. This one uses resistor voltage dividers, which is not recommended, I know. But I think in this case it is irrelevant, because it was a problem not related to unclean signal edges
  • ILI9341 display driver
  • Kingston 8GB SDHC Class 4


I hope this might be helpful for someone.
Thomas
http://www.hydroelectronics.net

fat16lib

Quote
LC Studio SD card adapter. This one uses resistor voltage dividers, which is not recommended, I know. But I think in this case it is irrelevant, because it was a problem not related to unclean signal edges
The LC studio card does not use resistor voltage dividers.  The resistors are pull-ups.  See This.

The full 5V Arduino output is applied to the signal pins of the SD.  Most SD cards have input protection and sink enough current to pull the voltage down.  Many cards sort of work with the LC Studio module.

Your "fix" for the LC Studio module won't work reliably with all SD cards.  Bests to trash the LC studio module.

Quote
The problem is that even though the SD CS pin is high, the data on the SPI MOSI bus, which is intended for the display, still somehow confuses the SD card, even before we run SD.begin
This happens because of the poor design of the LC Studio module.

aisc

You do realize you are lucky not to have trashed the SD Card.
It is not recommended to insert or eject an SD Card under power - it is likely to crash.
I know this first hand through experience.

hydrocontrol

The LC studio card does not use resistor voltage dividers.  The resistors are pull-ups.  See This.

The full 5V Arduino output is applied to the signal pins of the SD.  Most SD cards have input protection and sink enough current to pull the voltage down.  Many cards sort of work with the LC Studio module.

Your "fix" for the LC Studio module won't work reliably with all SD cards.  Bests to trash the LC studio module.
This happens because of the poor design of the LC Studio module.

You are absolutely right on all counts.
The LC Studio module (and similar looking replicas) resistors are only pull-ups, no dividers. I verified this on 2 replica modules I have here. I traced the connections on the pcb.

Today I received two different SD adapters:

Pololu Breakout Board for microSD Card with 3.3V Regulator and Level Shifters
https://www.pololu.com/product/2587

itead studio SD/Micro-SD Card Breakout Module
http://imall.itead.cc/sd-micro-sd-card-breakout-module.html

I tried both adapters with Sandisk and Kingston and Noname SD cards, and a Kingston microSD Card.
Both adapters did not have the Error Code 0XA with any of the SD cards, even if there is other SPI communication going on before the SD card is initialized.

Conclusion:
This is NOT a software initialization problem, it is bad hardware design on the LC Studio module.
I have 3 of these "pull-up resistor" modules, and they will go into the rubbish bin.

Thank you for your input.
Thomas
http://www.hydroelectronics.net

hydrocontrol

You do realize you are lucky not to have trashed the SD Card.
It is not recommended to insert or eject an SD Card under power - it is likely to crash.
I know this first hand through experience.
I acknowledge your account, however I'm not sure that you conclusion to not insert or eject an SD card under power hits the root cause.
It is clear to me that ejecting an SD card during a write procedure can at least destroy the file system, if not more.* If you have a power down procedure which finishes all writing tasks before shut of, then you can avoid this problem.

On the other hand, don't we insert and eject SD cards into our computers all the time without first powering the PC down? Inserting is no problem anyway, but safe ejecting requires that the PC completes all writing procedures first. That's why we use the "safe eject" command to umount the card.

Also I studied several instructions how to insert an SD card into a smartphone - power off was only required if the SD slot is behind the battery. Ejecting requires unmounting, as above.

And isn't pulling the USB plug on an arduino board an uncontrolled power loss, just like pulling the SD card out? This doesn't harm the SD card either. (Except if you happen to pull the USB cable exactly in the moment where there is a write :-)

Maybe you should make sure in your sketch that the CS pin for the SD card is normally high (chip NOT selected). So while inserting the SD card, it does not get confused by data on the SPI bus. But even if, this may prevent a successful initalization, but it should not destroy the card.

Does anybody have more information, if PCs or smart phones or other electronic devices actually cut the 3.3V power supply, if you use the unmount command?

* I managed to trash a navigation system:
The battery in the nav system was completely flat and defective. I wanted to update the maps by powering it from the laptop USB port. In the middle of the update process, the USB power voltage went too low. A certain portion of the flash memory on the nav was irrevocably damaged.

Cheers
Thomas
http://www.hydroelectronics.net

fat16lib

SD cards are designed for hot insertion.  You should wait a few ms for power to stabilize after you insert the card before initializing the card with a begin() call.

SD cards have large caps on Vcc so I have seen insertion of a card cause a glitch in power and cause problems with the Arduino or other devices connected to the Arduino.

You must close all files that are open for write before removing an SD card.  

Many newer SD cards have a protocol so a host can do a handshake with the card to do a safe removal.  Unfortunately this protocol is not supported in SPI mode.  In SPI mode allow at least one second after the last access to the card before removing the card.

After no access for one second, the card will go into a low power sleep mode and the card will not be harmed.  All buffered data will have been written to flash before the card goes to sleep mode.

I don't see a consistent pattern for removal of power with PC adapters.  The lights go out on some adapters and stay on with with other adapters when you click on eject.

hydrocontrol

Thank you, this information is very helpful for me.
You have a lot of background knowledge!
Thomas
http://www.hydroelectronics.net

Go Up