Page 1 of 5

Pico Mounting SD Card

Posted: Sun Jan 31, 2021 10:13 pm
by kbradley89
Hey, I'm attempting to mount an SD card to a Pico using a Adalogger Featherwing board.

My circuit is: Image

I am attempting to use https://github.com/micropython/micropyt ... /sdcard.py this driver with the code:
import sdcard
import os
import utime

# Set the SPI controls for the ADC
spi_sck=machine.Pin(18)
spi_tx=machine.Pin(19)
spi_rx=machine.Pin(16)
spi=machine.SPI(0,baudrate=100000,sck=spi_sck, mosi=spi_tx, miso=spi_rx)

spi_SDCS = machine.Pin(13)

# Set the SPI values for the SD card
sd = sdcard.SDCard(spi, spi_SDCS)
os.mount(sd, "/SD")
print('Done')
I have two SD cards, one gives the error:
Traceback (most recent call last):
File "<stdin>", line 17, in <module>
OSError: 19

the second gives:
Traceback (most recent call last):
File "<stdin>", line 14, in <module>
File "sdcard.py", line 48, in __init__
File "sdcard.py", line 81, in init_card
File "sdcard.py", line 129, in init_card_v2
OSError: timeout waiting for v2 card

I have attempted umount('/') before mounting (to '/' or '/SD') but that doesn't appear to fix anything.

I assume the second one at 32 GB is too big to read or something similar. Both are FAT32 formatted empty microSD cards (8GB and 32GB). The board appears to be talking to the SPI of the SD card reader correctly but the OS mounting seems to be the issue. Has anyone managed to add an external SD card reader and have any suggestions on what I might be doing wrong?

Re: Pico Mounting SD Card

Posted: Mon Feb 01, 2021 6:09 am
by Roberthh
I experienced the same and assume, that FAT support is missing.

Re: Pico Mounting SD Card

Posted: Mon Feb 01, 2021 11:22 pm
by MCHobby
Hi there, from various Pico documentation (the VGA board) I did noticed a SDIO as describe in the following picture:

https://raw.githubusercontent.com/mchob ... c/Pico.png

Please note the SD_CLK on GP5

Re: Pico Mounting SD Card

Posted: Tue Feb 02, 2021 9:12 am
by kbradley89
That is interesting, I'm not sure how to use the pins though as I don't seem to be having any luck with any built in SD class modules and it doesn't look like they translate to valid spi pins (e.g. GP5 is designated SPI0 CSn according to the getting started guides rather than a clk).

The two built in versions of SD card on the micropython docs are machine.SDCard and machine.SD (which appears to be a board specific implementation) whichever of these I try to import it suggests they don't exist: ImportError: can't import name SDCard

Do you think there is a way of wiring to the suggested pins for use with the sdcard.py library I had before or is there supposed to be an SD implementation on the board that I need to find?

Re: Pico Mounting SD Card

Posted: Tue Feb 02, 2021 9:37 am
by Roberthh
The sdcard.py module uses any SPI interface to drive the SD card. It works and I have used that for in instance with the W600 port. Technically it works with the RTP2040 too. In you first attempt, the SD card is detected and the firmware can read form it. The step that fails is mounting, because the driver for the FAT filesystem is not present. That's what OSError 19 tells.
The connection @MCHobby mentions seems to be for instance by the PIO based SD card support liek mentioned here: https://github.com/raspberrypi/pico-ext ... co_sd_card. But that one is not needed here. Even then, the FAT support would have to be included in the firmware.

Re: Pico Mounting SD Card

Posted: Tue Feb 02, 2021 9:50 am
by pythoncoder
Roberthh wrote:
Tue Feb 02, 2021 9:37 am
...Even then, the FAT support would have to be included in the firmware.
Perhaps it should be requested. I think a good many people will want removable mass storage.

Re: Pico Mounting SD Card

Posted: Tue Feb 02, 2021 11:29 am
by kbradley89
Thanks for the help. For now I think I can cope with the in built storage, I had hoped for the possibility of larger storage from an SD card and the ability for others to view the files on the SD card without extra software which seems necessary if you want to get the files from the in built storage.

Is there anything I should do to request SD card functionality or at least highlight that I'd like it?

Re: Pico Mounting SD Card

Posted: Tue Feb 02, 2021 4:45 pm
by Roberthh
I made a sample build with support for FAT. That one can use the SD card. But the firmware size increases by 14k. The changes to the build files are minor and could maybe be even fewer. The only thing that is missing and has to be created is a fatfs_port.c file providing the function get_fattime(void). For the moment I use a dummy. It should not be too difficult, but one has to sort in the data from the rtc.

Note: I tried also attaching a spi flash chip as external drive, using @pythoncoder's flash_spi.py and bdevice.py files. Works well too, but is obviously not removable like a SD card. That SPI flash chip was formatted obviously with LFS.

Re: Pico Mounting SD Card

Posted: Wed Feb 03, 2021 10:26 am
by Roberthh
A branch of the Repo with the changes for SD card support is here: https://github.com/robert-hh/micropython/tree/rp2_fat
It includes a firmware binary too in build/firmware.uf2

Re: Pico Mounting SD Card

Posted: Wed Feb 03, 2021 11:42 am
by kbradley89
Thanks Roberthh, I'll try out the new firmware at some point this week. I'm impressed with how quickly you were able to implement it; I hope it wasn't too much of an inconvenience for you.