Mounting sd card

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
Almoturg
Posts: 10
Joined: Sat May 07, 2016 6:06 pm

Mounting sd card

Post by Almoturg » Thu May 26, 2016 10:49 pm

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.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Mounting sd card

Post by pythoncoder » Fri May 27, 2016 8:04 am

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...
Peter Hinch
Index to my micropython libraries.

Almoturg
Posts: 10
Joined: Sat May 07, 2016 6:06 pm

Re: Mounting sd card

Post by Almoturg » Fri May 27, 2016 10:08 am

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.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Mounting sd card

Post by pythoncoder » Sat May 28, 2016 9:55 am

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.
Peter Hinch
Index to my micropython libraries.

User avatar
Roberthh
Posts: 3668
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Mounting sd card

Post by Roberthh » Sat May 28, 2016 11:41 am

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.

Almoturg
Posts: 10
Joined: Sat May 07, 2016 6:06 pm

Re: Mounting sd card

Post by Almoturg » Sat May 28, 2016 9:18 pm

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.

Post Reply