TheMecaProject
Posts: 10
Joined: Fri Apr 16, 2021 12:02 am

Re: sdcard.py, no sd card error

Sun Apr 18, 2021 10:59 pm

jbeale wrote: I changed that _CMD_TIMEOUT value in sdcard.py from 100 to 1000, and then my card worked!
HURRAY! You are goddamn right.
That indeed made my SHDC cards work - the SD still didn't, but that's all right. Perhaps a larger delay might solve that problem aswell. I'm far happier now anyway, so huge thanks!!!

I might move on my projects now. Mind if I ask one last question? Where can I find good examples on how to use this library? My future project intentions include opening files (obviously), writing to and reading from them, and if possible creating and moving files between folders.

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

Re: sdcard.py, no sd card error

Sun Apr 18, 2021 11:22 pm

I was wondering the same thing, but it looks like reading and writing to SD card in Micropython is mostly the same as any filesystem operation in normal Python. So regular Python code examples may just work as-is. For example the code below works for me (assuming some text file already exists):

Code: Select all

from machine import Pin, SPI
import sdcard # github.com/micropython/micropython/blob/master/drivers/sdcard/sdcard.py
import os

sd_spi = SPI(1, sck = Pin(10, Pin.OUT), mosi = Pin(11, Pin.OUT),miso = Pin(12, Pin.OUT))
sd = sdcard.SDCard(sd_spi, Pin(13, Pin.OUT))

os.mount(sd, "/SD")
print("Mounted")
print("Size: {} MB".format(sd.sectors/2048))

flist = os.listdir("/SD")
pathname = "/SD/" + flist[0]
print(pathname)

fp = open(pathname,'a')   # open first file in directory list, and append data to it
fp.write("This line was added.\n")
fp.close()

fp = open(pathname,'r')   # open the same file again for reading
lcnt = 0
while True:
    line = fp.readline()  # read one line of text from the file
    if not line:
        break  # quit when we've reached the end of the file
    lcnt += 1  # count how many lines we've read
    print("%d: %s" % (lcnt,line.strip()) )
fp.close()    
        
os.umount("/SD")  # unmount card from filesystem

danjperron
Posts: 3792
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: sdcard.py, no sd card error

Wed Apr 21, 2021 2:45 pm

Something you should consider also.

The default SPI clock, once the SDcard is initialized, is 1.32MHz.

To play wave file from the SDCard I need to pump it up at least to 3MHz. By default I change it to 10MHz.

I replaced "self.init_spi(1320000)" to "self.init_spi(10_000_000).


None of my cards had issue with the SPI pump clock. I even went to 50MHz without problem.

P.S. It is also possible to change the SPI clock without changing the SDCard.py file

Code: Select all

import uos
import SDCard
from machine import SPI,Pin
sd = SDCard.SDCard(SPI(1),Pin(13))
sd.init_spi(10_000_000)
uos.mount(sd,"/sd")
** UPDATE ** the SPI clock is the system clock / factor. When I set 50MHz, in reality it is 125MHz / 4 => 31.25MHz
Last edited by danjperron on Fri Apr 23, 2021 8:18 pm, edited 3 times in total.

TheMecaProject
Posts: 10
Joined: Fri Apr 16, 2021 12:02 am

Re: sdcard.py, no sd card error

Wed Apr 21, 2021 10:48 pm

jbeale wrote: I was wondering the same thing, but it looks like reading and writing to SD card in Micropython is mostly the same as any filesystem operation in normal Python. So regular Python code examples may just work as-is.
Very nice, sir! I am quite new on the Raspberry Pi world (and Python), so even if the ways of managing files are the same as on regular Python, that's still a new journey for me (most of my maker life has been around Arduinos). Fortunately Python seems way better for newbies than most languages. I might get used to it in no time.
danjperron wrote: Something you should consider also. The default SPI clock, once the SDcard is initialized, is 1.32MHz.
To play wave file from the SDCard I need to pump it up at least to 3MHz. By default I change it to 10MHz.
I replaced "self.init_spi(1320000)" to "self.init_spi(10_000_000).
Great to know that! I might fool around with the values sometime soon. My goal here is to make a robotic arm with position recording, though, so maybe such a high rate might be unnecessary. But if it works nicely, why not, right?

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

Re: sdcard.py, no sd card error

Thu Apr 22, 2021 5:17 pm

If you are using wires plugged into solderless breadboard setups, signal quality may be marginal from stray capacitance, wire inductance and ground bounce at higher rates like 50 MHz SD clock. That may cause difficult-to-find problems due to rare intermittent failures even if it does work some or most of the time. It's less trouble if you're using dedicated circuit board layouts with good grounds, circuit routing, etc.

horuable
Posts: 123
Joined: Sat Mar 06, 2021 12:35 am

Re: sdcard.py, no sd card error

Thu Apr 22, 2021 8:58 pm

I'm kinda late, but maybe someone will be interested.

Setting SPI to 50 MHz actually results in SCK running at 31.25 MHz, which can be checked by printing out the spi object. The next possible frequency is 62.5 MHz, but that's higher than SD cards are specified for.
In general SPI clock frequency can be set to the peripheral clock (125 MHz by default) divided by an even number, resulting in the highest possible clock being 62.5 MHz, but signal quality is rather questionable and some devices don't like it, even if in theory they can run with faster clocks.

danjperron
Posts: 3792
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: sdcard.py, no sd card error

Thu Apr 22, 2021 11:25 pm

@horuable,
good catch I'm to use with the PIO clock using the fractional divider!

Return to “MicroPython”