User avatar
jbeale
Posts: 3858
Joined: Tue Nov 22, 2011 11:51 pm
Contact: Website

anyone know about SD cards?

Mon Apr 19, 2021 6:28 pm

I wasn't aware I could use a microSD card by just soldering standard header pins to the common SD card holder that most microSD cards come packaged with, and connecting that direct to the Pico. But it does work (after a tweek, see below) which is convenient for standalone data logging operation onto removable media.

After discovering that my Pico running MicroPython was not able to read my SD card using the micropython sdcard.py library ("timeout waiting for card" error) I increased a timeout in their code from 100 to 1000. Then it did work. Another person with same problem on similar SanDisk SD cards, found this fix worked for them too. I then filed a github issue against that code. https://github.com/micropython/micropython/issues/7129

I see there is now a response asking me if I can tweak my arbitrary timeout value to see what the minimum necessary timeout is for my specific SD card to work. That may well be the expedient approach, but I feel there is something unsatisfying here.

I'm not sure what makes the difference to the SD card controller, eg some number of SPI bus clocks, or just a certain amount of delay time. But does anyone know if there is some standard procedure for how SD cards are supposed to work, rather than just replacing one possibly arbitrary number, with another definitely arbitrary number? Is it even possible to get a reliably-working SD card interface using simple SPI, instead of the 4-bit SD interface that larger systems use? If so are there code examples to follow?
20210419_SDcard.jpg
pico writing data to SD card
20210419_SDcard.jpg (154.04 KiB) Viewed 347 times

pica200
Posts: 272
Joined: Tue Aug 06, 2019 10:27 am

Re: anyone know about SD cards?

Tue Apr 20, 2021 12:58 am

The timeouts for reads and writes are specified in "Physical Layer Simplified Specification" here:
https://www.sdcard.org/downloads/pls/

Don't know the pages out of my head but it does give you the maximum times for read/write.

User avatar
Gavinmc42
Posts: 5490
Joined: Wed Aug 28, 2013 3:31 am

Re: anyone know about SD cards?

Tue Apr 20, 2021 2:07 am

I think the older MMC interface is closer to pure SPI.
Not sure if the eMMC used on CM4 is an extension, but the source code should be around.
Will that still work on uSDs?

There should be plenty of Arduino code around too.
If you find out let us know, got my second Pico yesterday, now I have a programmer ;)

Pico data loggers would be useful for sensor things.
I'm dancing on Rainbows.
Raspberries are not Apples or Oranges

cjketle
Posts: 19
Joined: Mon Jan 14, 2019 6:29 pm

Re: anyone know about SD cards?

Tue Apr 20, 2021 9:01 am

I think you need pull up resistors on the unused data pins as well (see att) to make SPI access reliable. No problems using SPI mode though with ESP32 here , Pico should not be any different electrically.

Chris K
Attachments
sd.jpg
sd.jpg (36.88 KiB) Viewed 250 times

pica200
Posts: 272
Joined: Tue Aug 06, 2019 10:27 am

Re: anyone know about SD cards?

Tue Apr 20, 2021 10:29 am

I don't think this has anything to do with missing pull ups. None of that is required to my knowledge. It's simply the card takes longer than expected to respond. If i remember right the max timeout for read/write was about 500 ms.

User avatar
jbeale
Posts: 3858
Joined: Tue Nov 22, 2011 11:51 pm
Contact: Website

Re: anyone know about SD cards?

Wed Apr 21, 2021 12:36 am

pica200 wrote:
Tue Apr 20, 2021 12:58 am
The timeouts for reads and writes are specified in "Physical Layer Simplified Specification" here:
https://www.sdcard.org/downloads/pls/
Thanks for the link. That is a 305 page document and I haven't read the whole thing but I did notice that it mentions the host must use a timeout value of one (1) second or greater for the ACMD41 command. That is what advances the SD card state machine from poweron (idle) state to "ready" state, but that may apply only to SD interface mode? In SPI mode I don't see any timeout mentioned, although the ACMD41 still shows up in the SPI operational flowchart on p.7.1 (p. 263).

UPDATE: In my SD card experiments with Pico in MicroPython writing to a SanDisk 8GB card, I found that the sdcard.py _CMD_TIMEOUT value of 150 worked, but 125 failed. The existing sdcard.py library used a timeout of 100, which of course also fails.
When it fails, my user-level code that fails is os.mount(sd, "/SD")
When it works (eg. _CMD_TIMEOUT = 150 or more), that command returns after taking only 20 msec. So it's clear the default timeout value of 100 is a long way from waiting a full second before declaring failure.

In this case, it appears that timeout value is a count of software loop cycles, not any fixed time units, and I wonder if maybe it works better on other boards that are slower than the Pico in running MicroPython code, if "100 loops" ends up taking longer to complete.

pica200
Posts: 272
Joined: Tue Aug 06, 2019 10:27 am

Re: anyone know about SD cards?

Wed Apr 21, 2021 8:36 am

If nothing is mentioned for SPI mode i would assume it's the same as SD mode. And yeah, a timer is better than loop counts.

I don't know Python so i assumed the timeout you mentioned was about read and write but yeah, the op cond loop needs a timeout of 1 second. I have a very old 2 GB SD that takes pretty long until it responds with the "busy" (should have been named ready) bit set. Most newer cards are much faster.

If in doubt you can also look into the Linux driver(s) for SD/MMC:
https://github.com/torvalds/linux/tree/ ... s/mmc/core

Return to “General”