-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Automount SD cards #10129
Automount SD cards #10129
Conversation
And make them available over USB MSC. They can be remount to make them writable (slowly) from the host PC. Fixes micropython#9954. Fixes micropython#8678. Fixes micropython#3477
I have been able to cause safemode crashes accessing the automounted SD cards. I've been trying to reduce the code needed to reproduce and this is what I currently have as code.py: import sdioio
import board
import storage,os
sd = sdioio.SDCard(clock=board.GP36,command=board.GP35,data=[board.GP37, board.GP33, board.GP38, board.GP34],frequency=25000000)
vfs=storage.VfsFat(sd)
storage.mount(vfs,'/sd')
os.chdir('/sd')
while True:
tFSize = 0
nFiles = 0
nDirs = 2
print(f'Directory of {os.getcwd()}.')
for dir in os.listdir():
if os.stat(dir)[0] & (2**15)== 0:
print(f'{dir}{" "*(24-len(dir))}<DIR>{" "*18}')
nDirs += 1
tmpDir = os.getcwd()
try:
availDisk = os.statvfs(tmpDir)[1]*os.statvfs(tmpDir)[4]
except:
availDisk = 0
for dir in os.listdir():
if os.stat(dir)[0] & (2**15) != 0:
fSize = str(os.stat(dir)[6])
tFSize += os.stat(dir)[6]
print(f'{dir}{" "*(35-len(dir)+10-len(fSize))} {fSize}')
nFiles += 1 My current theory/guess is that this has something to do with accessing the SD card during the process of mounting it to the host computer. I put a 20 second delay between the mount and the start of the directory listing and that seems to prevent the crash. My host PC is running Ubuntu. It doesn't always result in a crash but putting ~100 files and a couple empty sub directories on the SD card seems to make it crash more reliably. I originally noticed the crash using an SD card with a linux boot image so there were actually two partitions automounted and more than 100 files being listed. I have also formatted a 32G SD card and seen the error on that as well. Before the board hard crashes I sometimes get the following error:
I'm using the Waveshare S3 Geek board which has a display and since my current theory is this is a timing/race condition issue outputting to the LCD screen may be part of the issue. I don't think I have a board that has an SD card but no display, but I'll look around. |
I assume this isn't actually supposed to automatically mount the SD card to CircuitPython but when an SD card is mounted it "automounts" the cards to the host computer. That's what I'm seeing anyway. On the sparkfun thingplus RP2040 (no display) the automount to the Host PC does occur, but it takes considerably longer than the S3 board. I was also unable to get the code.py (slightly altered to use sdcardio and busio) to cause the crash, although with the code.py running, the mount to the host PC didn't occur before I gave up (a couple minutes). |
Does the Waveshare S3 Geek share the SPI bus between the display and SD card? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried the artifact build on a PyPortal with a 4GB SD card in the slot.
With no code.py I can see some attempt to mount the SD card, but it does not complete. From /var/log/syslog
:
2025-03-28T11:00:38.921872-04:00 cod kernel: sd 4:0:0:1: [sdc] 7744512 512-byte logical blocks: (3.97 GB/3.69 GiB)
2025-03-28T11:00:38.923918-04:00 cod kernel: sdc: detected capacity change from 0 to 7744512
But it never shows up in mount
or df
. This is on Ubuntu 24.04. sdb1
is CIRCUITPY.
If I try running the NASA PyPortal demo, CircuitPython hangs and boot loops (slowly, many seconds between tries)
Some more testing. The
Also tested on Windows 11: Usually, neither CIRCUITPY nor the SD card show up. Once, both showed up as D: and E:, but were non-functional. On macOS 15.3.2: CIRCUITPY shows up, but there's no sign of the other drive in System Information. |
Thanks for the testing! I'll get back to this next week. |
@RetiredWizard I think your crash may be due to I'm testing with automounted |
Add Metro RP2350 definitions
Ok, I've made the MSC LUN -> filesystem checks stricter. Only sdcardio that is not allocated to the VM heap will be exposed my USB MSC. This basically requires the automounting definitions for the board. I've added them to Metro RP2350 in addition to Fruit Jam. Other ports/boards will be need to be added as they can be tested. I tested on the PyPortal briefly but had issues with sdcardio that I didn't want to debug. The same card works on RP2350. PyPortal shares the clock line between the SD card and the ESP32. I also tested on Mac in addition to Linux and the SD card appears and disappears as I expect on both platforms. |
The mac was my old 2019 Macbook Pro running 15.0.1. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tested with a Metro RP2350 on Ubuntu 24.04, Windows 11, and macOS 15.3.2.
All work!
The SD card is normally read-only to the host computer.
If I put import storage; storage.remount("/sd", readonly=True)
in boot.py
, it becomes R/W to the host. Doing that after USB enumeration, say in the REPL, does not change the host view of the drive, but does not give an error.
- Maybe raise an error if
storage.remount()
is attempted after USB starts? - Document the feature, maybe after this section on CIRCUITPY? https://docs.circuitpython.org/en/latest/docs/workflows.html#circuitpy-drive
I've added more documentation. I think it may have worked for you but you didn't realize. It takes a few seconds for CP to "eject" and then make the drive available again. |
In the case of |
It is not strictly true for
CircuitPython only grabs the underlying write lock when a file is open for writing. It knows more about when manipulations are happening. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the documentation!
And make them available over USB MSC. They can be remount to make them writable (slowly) from the host PC.
Fixes #9954. Fixes #8678. Fixes #3477