Pico Mounting SD Card

RP2040 based microcontroller boards running MicroPython.
Target audience: MicroPython users with an RP2040 boards.
This does not include conventional Linux-based Raspberry Pi boards.
User avatar
jcf
Posts: 60
Joined: Wed Mar 03, 2021 11:14 am

Re: Pico Mounting SD Card

Post by jcf » Mon Sep 13, 2021 9:37 am

I have put a zip file containing my recently used libs on my homepage:
http://staff.ltam.lu/feljc/electronics/ ... pythonLibs

These are mostly libs I found on the net and that I have slightly modified, but also some I have written.

User avatar
SchroedersKater
Posts: 11
Joined: Sun Dec 19, 2021 4:09 pm
Location: Near Bonn & Cologne, Germany

Re: Pico Mounting SD Card

Post by SchroedersKater » Sun Dec 19, 2021 4:14 pm

Hello All,

I basically got it all running, but there is some help I could need:

Modern SD cards starting at 4 GB use FAT32, but I need smaller cards (128 & 256 MB) and they are formatted with FAT16. Did someone modify the driver according to this? Any help appreciated.

Greetings from Germany
Thomas

User avatar
scruss
Posts: 360
Joined: Sat Aug 12, 2017 2:27 pm
Location: Toronto, Canada
Contact:

Re: Pico Mounting SD Card

Post by scruss » Sun Dec 19, 2021 5:49 pm

I have used small FAT16 cards successfully without changes.

User avatar
SchroedersKater
Posts: 11
Joined: Sun Dec 19, 2021 4:09 pm
Location: Near Bonn & Cologne, Germany

Re: Pico Mounting SD Card

Post by SchroedersKater » Sun Dec 19, 2021 10:44 pm

Thank you for this information!

After reading this I formatted 2 cards with FAT32 and got the same result. I’ll post my source code here tomorrow. Maybe some one has an idea what’s going on.

FeK9
Posts: 33
Joined: Sun Jan 20, 2019 8:19 am

Re: Pico Mounting SD Card

Post by FeK9 » Mon Dec 20, 2021 7:32 am

@SchroedersKater

Morning... :)

Like scruss above I've had no problem with small FAT16 SD cards, I used this driver found on the MicroPython GitHub
page... https://github.com/micropython/micropyt ... ers/sdcard

Below is my test code and REPL output from a 64MB card...

Code: Select all

from machine import SPI, Pin
import uos
import sdcard
import time

spi=machine.SPI(1,sck=Pin(10), mosi=Pin(11), miso=Pin(12))
sd = sdcard.SDCard(spi, Pin(15))

vfs = uos.VfsFat(sd)
uos.mount(vfs, "/")

print(uos.listdir("/"))
uos.umount("/")

======================================================
MicroPython v1.17 on 2021-09-02; Raspberry Pi Pico with RP2040
Type "help()" for more information.
>>> %Run -c $EDITOR_CONTENT

['rats1.txt', 'rats.txt', 'TomsDiner8.wav', 'fox.txt', 'tone.BAS', 'toggle1819.BAS']

>>> 

User avatar
SchroedersKater
Posts: 11
Joined: Sun Dec 19, 2021 4:09 pm
Location: Near Bonn & Cologne, Germany

Re: Pico Mounting SD Card

Post by SchroedersKater » Mon Dec 20, 2021 10:57 am

Thank you all!

I tried several configurations and this is what I found out until now:

- FAT16 or FAT32, doesn't matter.
- Only the 128 MB cards make problems.
- But I can read these cards fine on my Macs with different adapters, on a Windows PC and even with a DFPlayer Mini connected to the Pico.
- It can read one block of data, but stops with error at the second like in the following image (the baudrate param makes no difference).

Code: Select all

from machine import Pin, SPI
import time, sdcard

CS = Pin(17, Pin.OUT, value = 1)

def sdrawtest():
    sd = sdcard.SDCard(SPI(0, baudrate=40000000, sck=Pin(18), mosi=Pin(19), miso=Pin(16)), CS)
    buf = bytearray(512)
    
    for j in range(0, 2):
        sd.readblocks(j, buf)
        print()
        print("buffer ", j)
        
        for i in range(0, 512):
            if i % 16 == 8:
                print('    ',end='')
                
            if i % 16 == 0:
                print()
                print('{:04X}'.format(i), end=': ')
        
            print('{:02X}'.format(buf[i]), end=' ')
               
sdrawtest()

Code: Select all

>>> %Run -c $EDITOR_CONTENT

buffer  0

0000: 00 00 00 00 00 00 00 00     00 00 00 00 00 00 00 00 
...
01C0: 01 00 0B FE FF FF 3F 00     00 00 85 BF 03 00 00 00 
01D0: 00 00 00 00 00 00 00 00     00 00 00 00 00 00 00 00 
01E0: 00 00 00 00 00 00 00 00     00 00 00 00 00 00 00 00 
01F0: 00 00 00 00 00 00 00 00     00 00 00 00 00 00 55 AA 
Traceback (most recent call last):
  File "<stdin>", line 26, in <module>
  File "<stdin>", line 12, in sdrawtest
  File "sdcard.py", line 238, in readblocks
OSError: [Errno 5] EIO
Running this stuff a second time results in this:

Code: Select all

>>> %Run -c $EDITOR_CONTENT
Traceback (most recent call last):
  File "<stdin>", line 26, in <module>
  File "<stdin>", line 8, in sdrawtest
  File "sdcard.py", line 54, in __init__
  File "sdcard.py", line 82, in init_card
OSError: no SD card
Any idea? Almost desperate ;-) Could it be a timing problem?

FeK9
Posts: 33
Joined: Sun Jan 20, 2019 8:19 am

Re: Pico Mounting SD Card

Post by FeK9 » Mon Dec 20, 2021 12:42 pm

SchroedersKater wrote:
Mon Dec 20, 2021 10:57 am
Thank you all!

I tried several configurations and this is what I found out until now:
...............................................................
I've plug my SPI settings in and your code works... Curios :|

Are you sure your CS pin is correct, that second error "OSError: no SD card" I think is linked to the CS pin....
Sdcard.py driver has it's own baud defaults... A slow init then it ramp's up the baud afterwards...

Code: Select all

from machine import Pin, SPI
import time, sdcard

CS = Pin(15, Pin.OUT, value = 1)


def sdrawtest():
    sd = sdcard.SDCard(SPI(1, baudrate=40000000, sck=Pin(10), mosi=Pin(11), miso=Pin(12)), CS)
    buf = bytearray(512)
    
    for j in range(0, 2):
        sd.readblocks(j, buf)
        print()
        print("buffer ", j)
        
        for i in range(0, 512):
            if i % 16 == 8:
                print('    ',end='')
                
            if i % 16 == 0:
                print()
                print('{:04X}'.format(i), end=': ')
        
            print('{:02X}'.format(buf[i]), end=' ')
               
sdrawtest()

Code: Select all

>>> %Run -c $EDITOR_CONTENT

buffer  0

0000: 00 00 00 00 00 00 00 00     00 00 00 00 00 00 00 00 
0010: 00 00 00 00 00 00 00 00     00 00 00 00 00 00 00 00 
0020: 00 00 00 00 00 00 00 00     00 00 00 00 00 00 00 00 
0030: 00 00 00 00 00 00 00 00     00 00 00 00 00 00 00 00 
0040: 00 00 00 00 00 00 00 00     00 00 00 00 00 00 00 00 
0050: 00 00 00 00 00 00 00 00     00 00 00 00 00 00 00 00 
0060: 00 00 00 00 00 00 00 00     00 00 00 00 00 00 00 00 
0070: 00 00 00 00 00 00 00 00     00 00 00 00 00 00 00 00 
0080: 00 00 00 00 00 00 00 00     00 00 00 00 00 00 00 00 
0090: 00 00 00 00 00 00 00 00     00 00 00 00 00 00 00 00 
00A0: 00 00 00 00 00 00 00 00     00 00 00 00 00 00 00 00 
00B0: 00 00 00 00 00 00 00 00     00 00 00 00 00 00 00 00 
00C0: 00 00 00 00 00 00 00 00     00 00 00 00 00 00 00 00 
00D0: 00 00 00 00 00 00 00 00     00 00 00 00 00 00 00 00 
00E0: 00 00 00 00 00 00 00 00     00 00 00 00 00 00 00 00 
00F0: 00 00 00 00 00 00 00 00     00 00 00 00 00 00 00 00 
0100: 00 00 00 00 00 00 00 00     00 00 00 00 00 00 00 00 
0110: 00 00 00 00 00 00 00 00     00 00 00 00 00 00 00 00 
0120: 00 00 00 00 00 00 00 00     00 00 00 00 00 00 00 00 
0130: 00 00 00 00 00 00 00 00     00 00 00 00 00 00 00 00 
0140: 00 00 00 00 00 00 00 00     00 00 00 00 00 00 00 00 
0150: 00 00 00 00 00 00 00 00     00 00 00 00 00 00 00 00 
0160: 00 00 00 00 00 00 00 00     00 00 00 00 00 00 00 00 
0170: 00 00 00 00 00 00 00 00     00 00 00 00 00 00 00 00 
0180: 00 00 00 00 00 00 00 00     00 00 00 00 00 00 00 00 
0190: 00 00 00 00 00 00 00 00     00 00 00 00 00 00 00 00 
01A0: 00 00 00 00 00 00 00 00     00 00 00 00 00 00 00 00 
01B0: 00 00 00 00 00 00 00 00     64 70 6D 9D 00 00 00 00 
01C0: 28 00 0E FE 3F 06 27 00     00 00 D9 DB 01 00 00 00 
01D0: 00 00 00 00 00 00 00 00     00 00 00 00 00 00 00 00 
01E0: 00 00 00 00 00 00 00 00     00 00 00 00 00 00 00 00 
01F0: 00 00 00 00 00 00 00 00     00 00 00 00 00 00 55 AA 
buffer  1

0000: 00 00 00 00 00 00 00 00     00 00 00 00 00 00 00 00 
0010: 00 00 00 00 00 00 00 00     00 00 00 00 00 00 00 00 
0020: 00 00 00 00 00 00 00 00     00 00 00 00 00 00 00 00 
0030: 00 00 00 00 00 00 00 00     00 00 00 00 00 00 00 00 
0040: 00 00 00 00 00 00 00 00     00 00 00 00 00 00 00 00 
0050: 00 00 00 00 00 00 00 00     00 00 00 00 00 00 00 00 
0060: 00 00 00 00 00 00 00 00     00 00 00 00 00 00 00 00 
0070: 00 00 00 00 00 00 00 00     00 00 00 00 00 00 00 00 
0080: 00 00 00 00 00 00 00 00     00 00 00 00 00 00 00 00 
0090: 00 00 00 00 00 00 00 00     00 00 00 00 00 00 00 00 
00A0: 00 00 00 00 00 00 00 00     00 00 00 00 00 00 00 00 
00B0: 00 00 00 00 00 00 00 00     00 00 00 00 00 00 00 00 
00C0: 00 00 00 00 00 00 00 00     00 00 00 00 00 00 00 00 
00D0: 00 00 00 00 00 00 00 00     00 00 00 00 00 00 00 00 
00E0: 00 00 00 00 00 00 00 00     00 00 00 00 00 00 00 00 
00F0: 00 00 00 00 00 00 00 00     00 00 00 00 00 00 00 00 
0100: 00 00 00 00 00 00 00 00     00 00 00 00 00 00 00 00 
0110: 00 00 00 00 00 00 00 00     00 00 00 00 00 00 00 00 
0120: 00 00 00 00 00 00 00 00     00 00 00 00 00 00 00 00 
0130: 00 00 00 00 00 00 00 00     00 00 00 00 00 00 00 00 
0140: 00 00 00 00 00 00 00 00     00 00 00 00 00 00 00 00 
0150: 00 00 00 00 00 00 00 00     00 00 00 00 00 00 00 00 
0160: 00 00 00 00 00 00 00 00     00 00 00 00 00 00 00 00 
0170: 00 00 00 00 00 00 00 00     00 00 00 00 00 00 00 00 
0180: 00 00 00 00 00 00 00 00     00 00 00 00 00 00 00 00 
0190: 00 00 00 00 00 00 00 00     00 00 00 00 00 00 00 00 
01A0: 00 00 00 00 00 00 00 00     00 00 00 00 00 00 00 00 
01B0: 00 00 00 00 00 00 00 00     00 00 00 00 00 00 00 00 
01C0: 00 00 00 00 00 00 00 00     00 00 00 00 00 00 00 00 
01D0: 00 00 00 00 00 00 00 00     00 00 00 00 00 00 00 00 
01E0: 00 00 00 00 00 00 00 00     00 00 00 00 00 00 00 00 
01F0: 00 00 00 00 00 00 00 00     00 00 00 00 00 00 00 00 
>>> 

User avatar
SchroedersKater
Posts: 11
Joined: Sun Dec 19, 2021 4:09 pm
Location: Near Bonn & Cologne, Germany

Re: Pico Mounting SD Card

Post by SchroedersKater » Mon Dec 20, 2021 12:50 pm

Yes, the CS pin is definitely correct. All my other cards work fine (8 GB) only the 10 128 MB cards I have refuse. I tried another port on the Pico as well, same result. How can I influence the timing?

One more strange thing I noticed:

On an 8 GB card it says, it has 15601664 sectors which is correct with a sector length of 512.

On my 128 MB cards it tells, it has 1694208 sectors. With this value I get a sector size of about 75 which is definitely wrong. Very strange.

FeK9
Posts: 33
Joined: Sun Jan 20, 2019 8:19 am

Re: Pico Mounting SD Card

Post by FeK9 » Mon Dec 20, 2021 3:20 pm

SchroedersKater wrote:
Mon Dec 20, 2021 12:50 pm
Yes, the CS pin is definitely correct. All my other cards work fine (8 GB) only the 10 128 MB cards I have refuse. I tried another port on the Pico as well, same result. How can I influence the timing?
==================================================
On the baud thing, I'm not sure if you have seen micropython file called 'sdtest.py' found in same place as the driver..?

They have dial down the baud to a 100k for there testing, that file also work for me with my 64MB sdcard. I have
come across fake sized mem-sticks before with 8GB printed but max out at far less GB...

Wondering if you've tried "SD Association" SD Memory Card Formatter https://www.sdcard.org/downloads/formatter/

User avatar
SchroedersKater
Posts: 11
Joined: Sun Dec 19, 2021 4:09 pm
Location: Near Bonn & Cologne, Germany

Re: Pico Mounting SD Card

Post by SchroedersKater » Mon Dec 20, 2021 3:30 pm

Thank you again!

Yes, I tried all you mentioned.

Maybe the CSD from init_card means something to you:

Code: Select all

CSD: 00 7F 00 32 53 5A 80 3B EE BB FF 9F 16 80 00 53
BTW, the driver determines the 128 MB card as a version 2 card. Is this possible? I thought, all cards < 4 Gb are version 1.

Post Reply