Handle partitioned and unpartitioned SD cards
All checks were successful
Build RPi Pico firmware image / Build-Firmware (push) Successful in 3m28s
Check code formatting / Check-C-Format (push) Successful in 7s
Check code formatting / Check-Python-Flake8 (push) Successful in 9s
Check code formatting / Check-Bash-Shellcheck (push) Successful in 5s
Run unit tests on host / Run-Unit-Tests (push) Successful in 9s
All checks were successful
Build RPi Pico firmware image / Build-Firmware (push) Successful in 3m28s
Check code formatting / Check-C-Format (push) Successful in 7s
Check code formatting / Check-Python-Flake8 (push) Successful in 9s
Check code formatting / Check-Bash-Shellcheck (push) Successful in 5s
Run unit tests on host / Run-Unit-Tests (push) Successful in 9s
Add utils.MBRPartition to implement basic partitioned device support. Try to mount partition 1 of SDCard first, if that fails, try to mount entire device as FAT file system. Signed-off-by: Matthias Blankertz <matthias@blankertz.org>
This commit is contained in:
39
software/src/utils/sdcontext.py
Normal file
39
software/src/utils/sdcontext.py
Normal file
@@ -0,0 +1,39 @@
|
||||
# SPDX-License-Identifier: MIT
|
||||
# Copyright (c) 2025 Matthias Blankertz <matthias@blankertz.org>
|
||||
|
||||
import os
|
||||
|
||||
from . import MBRPartition
|
||||
from rp2_sd import SDCard
|
||||
|
||||
|
||||
class SDContext:
|
||||
def __init__(self, mosi, miso, sck, ss, baudrate):
|
||||
self.mosi = mosi
|
||||
self.miso = miso
|
||||
self.sck = sck
|
||||
self.ss = ss
|
||||
self.baudrate = baudrate
|
||||
|
||||
def __enter__(self):
|
||||
self.sdcard = SDCard(self.mosi, self.miso, self.sck, self.ss, self.baudrate)
|
||||
# Try first partition
|
||||
try:
|
||||
self.part = MBRPartition(self.sdcard, 0)
|
||||
os.mount(self.part, '/sd')
|
||||
return self
|
||||
except Exception:
|
||||
print("Failed to mount SDCard partition, trying whole device...")
|
||||
# Try whole device
|
||||
try:
|
||||
os.mount(self.sdcard, '/sd')
|
||||
return self
|
||||
except Exception as ex:
|
||||
self.sdcard.deinit()
|
||||
raise RuntimeError("Could not mount SD card") from ex
|
||||
|
||||
def __exit__(self, exc_type, exc_value, traceback):
|
||||
try:
|
||||
os.umount('/sd')
|
||||
finally:
|
||||
self.sdcard.deinit()
|
||||
Reference in New Issue
Block a user