Files
tonberry-pico/software/src/utils/sdcontext.py
Matthias Blankertz e9bd4f72b6
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
Handle partitioned and unpartitioned SD cards
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>
2025-08-05 21:16:09 +02:00

40 lines
1.1 KiB
Python

# 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()