You've already forked MicroPythonOS
mirror of
https://github.com/m5stack/MicroPythonOS.git
synced 2026-05-20 11:51:27 -07:00
Add SDCard support
This commit is contained in:
@@ -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")
|
||||
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user