Page 1 of 1

Mounting sd card

Posted: Thu May 26, 2016 10:49 pm
by Almoturg
I would like to read and write files from an sd card. Currently I'm using a microsd connector ripped from an old device (connected to a custom esp-12 breakout board) but the goal is to mount a connector on a new custom pcb eventually.

I have gotten sdcard.py from micropython/drivers/sdcard to work by changing the spi syntax to the current one and adding some more waiting/retry loops to work with my specific card.

I'm not sure how to mount the card to the filesystem though. I thought I could just look at the code for the pyboard but the moduos.c files seem to be organized quite differently. Simply doing

Code: Select all

card=SDCard(spi,cs)
sdfs=os.Vfsfat(card, "/sd")
doesn't seem to work, writing to the sd directory writes to the flash and calling e.g. "sdfs.listdir()" just gives the files on the flash. I couldn't figure out why though, I got a bit lost in the code (there is a hardcoded "MP_STATE_PORT(fs_user_mount)[0]" in moduos.py but that shouldn't affect the methods on sdfs).
I would appreciate any tips.

The goal would be to load the python code (main.py) from the flash on the esp-12 and read/write data files on the sd card.

Re: Mounting sd card

Posted: Fri May 27, 2016 8:04 am
by pythoncoder
The file sdtest.py shows how to mount the filesystem and perform read/writes. Have you run it against your card? Note, you may be the first person to try this on the ESP8266...

Re: Mounting sd card

Posted: Fri May 27, 2016 10:08 am
by Almoturg
The esp8266 port doesn't have the pyb.mount function which is used in sdtest.py (I think it should be in os now, but its not there either). os.Vfsfat seem to be imported from extmod/vfsfat while the other file system functions (e.g. os.mkdir,...) just call the methods of the first file system object.

Re: Mounting sd card

Posted: Sat May 28, 2016 9:55 am
by pythoncoder
This would be worth raising as an issue on Github. It would be useful to be able to mount devices which use the block protocol.

Re: Mounting sd card

Posted: Sat May 28, 2016 11:41 am
by Roberthh
Hi all, I'm digging through that part of code at the moment, adding useful stuff and maybe also some golden handles. Since I planned to attach am SD card too, the option for adding more file systems looks like a worthy task. At least I could try.

Re: Mounting sd card

Posted: Sat May 28, 2016 9:18 pm
by Almoturg
That would be awesome!

It seems that one issue is the sector size: The onboard flash has 4 kiB sectors while the sd card uses 512 byte ones. Unfortunately creating the FATFS filesystem struct always allocates the maximum sector size, so it runs out of memory...

Edit: Actually there seems to be enough memory, just not in a contiguous block.