From 9707d73db3568a08f0872a2fbc9bc6090dc23067 Mon Sep 17 00:00:00 2001 From: Thomas Farstrike Date: Mon, 27 Oct 2025 21:38:56 +0100 Subject: [PATCH] Add SDCard support --- internal_filesystem/boot_fri3d-2024.py | 6 ++- internal_filesystem/lib/mpos/sdcard.py | 72 ++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 internal_filesystem/lib/mpos/sdcard.py diff --git a/internal_filesystem/boot_fri3d-2024.py b/internal_filesystem/boot_fri3d-2024.py index a5787ee7..19646459 100644 --- a/internal_filesystem/boot_fri3d-2024.py +++ b/internal_filesystem/boot_fri3d-2024.py @@ -1,5 +1,5 @@ # Hardware initialization for Fri3d Camp 2024 Badge -from machine import Pin, SPI +from machine import Pin, SPI, SDCard import st7789 import lcd_bus import machine @@ -263,4 +263,8 @@ indev.enable(True) # NOQA import mpos.battery_voltage mpos.battery_voltage.init_adc(13, 2 / 1000) +# SD card doesn't need to be plugged in at boot, can be plugged in later +import mpos.sdcard +mpos.sdcard.inform_sdcard(machine.SDCard(spi_bus=spi_bus,cs=14)) + print("boot.py finished") diff --git a/internal_filesystem/lib/mpos/sdcard.py b/internal_filesystem/lib/mpos/sdcard.py new file mode 100644 index 00000000..11e031aa --- /dev/null +++ b/internal_filesystem/lib/mpos/sdcard.py @@ -0,0 +1,72 @@ +import os +import machine + +sdcard = None + +# This gets called by (the device-specific) boot*.py +def inform_sdcard(so): + global sdcard + sdcard = so + +def format_sdcard(sdcard): + """Format the SD card with FAT filesystem""" + try: + print("Formatting SD card...") + # Unmount if already mounted + try: + os.umount('/sdcard') + except: + pass + + # Format the SD card (this erases all data!) + sdcard.format() + print("SD card formatted successfully") + return True + except Exception as e: + print(f"WARNING: Failed to format SD card: {e}") + return False + +# Tries to mount and returns True if it worked +def try_mount(sdcard): + try: + os.mount(sdcard, '/sdcard') + print("SD card mounted successfully at /sdcard") + return True + except Exception as e: + print(f"WARNING: failed to mount SD card: {e}") + return False + +# Returns True if everything successful, otherwise False if it failed to mount +def mount_sdcard_optional_format(): + global sdcard # should have been initialized by inform_sdcard() + + if not sdcard: + print("WARNING: no machine.SDCard object has been initialized at boot, aborting...") + return False + + # Try to detect SD card + try: + sdcard.info() + print("SD card detected") + except Exception as e: + print(f"WARNING: sdcard.info() got exception: {e}") + print("SD card not detected or hardware issue") + return False + + if not try_mount(sdcard): + if format_sdcard(sdcard): + if not try_mount(sdcard): + print(f"WARNING: failed to mount SD card even after formatting, aborting...") + return False + else: + print("WARNING: Could not format SD card - check hardware connections or reformat on a PC. Aborting...") + return False + + try: + print("SD card contents:", os.listdir('/sdcard')) + except Exception as e: + print(f"WARNING: SD card did not fail to mount but could not list SD card contents: {e}") + return False + + print("SD card mounted successfully!") + return True