|
1 | 1 | import os |
2 | 2 | import machine |
| 3 | +import vfs |
3 | 4 |
|
4 | | -sdcard = None |
| 5 | +class SDCardManager: |
| 6 | + def __init__(self, spi_bus, cs_pin): |
| 7 | + self._sdcard = None |
| 8 | + try: |
| 9 | + self._sdcard = machine.SDCard(spi_bus=spi_bus, cs=cs_pin) |
| 10 | + self._sdcard.info() |
| 11 | + print("SD card initialized") |
| 12 | + except Exception as e: |
| 13 | + print(f"ERROR: Failed to initialize SD card: {e}") |
5 | 14 |
|
6 | | -# This gets called by (the device-specific) boot*.py |
7 | | -def inform_sdcard(so): |
8 | | - global sdcard |
9 | | - sdcard = so |
| 15 | + def mount(self, mount_point): |
| 16 | + if not self._sdcard: |
| 17 | + return False |
| 18 | + try: |
| 19 | + os.mount(self._sdcard, mount_point) |
| 20 | + print(f"Mounted at {mount_point}") |
| 21 | + return True |
| 22 | + except OSError as e: |
| 23 | + print(f"Mount failed: {e}") |
| 24 | + return False |
10 | 25 |
|
11 | | -def format_sdcard(sdcard): |
12 | | - """Format the SD card with FAT filesystem""" |
13 | | - try: |
| 26 | + def format_and_mount(self, mount_point): |
| 27 | + if not self._sdcard: |
| 28 | + return False |
14 | 29 | print("Formatting SD card...") |
15 | | - # Unmount if already mounted |
16 | 30 | try: |
17 | | - os.umount('/sdcard') |
| 31 | + os.umount(mount_point) |
18 | 32 | except: |
19 | 33 | pass |
20 | | - |
21 | | - # Format the SD card (this erases all data!) |
22 | | - sdcard.format() |
23 | | - print("SD card formatted successfully") |
24 | | - return True |
25 | | - except Exception as e: |
26 | | - print(f"WARNING: Failed to format SD card: {e}") |
27 | | - return False |
| 34 | + try: |
| 35 | + vfs.VfsFat.mkfs(self._sdcard) |
| 36 | + print("Formatted") |
| 37 | + return self.mount(mount_point) |
| 38 | + except OSError as e: |
| 39 | + print(f"Format failed: {e}") |
| 40 | + return False |
28 | 41 |
|
29 | | -# Tries to mount and returns True if it worked |
30 | | -def try_mount(sdcard): |
31 | | - try: |
32 | | - os.mount(sdcard, '/sdcard') |
33 | | - print("SD card mounted successfully at /sdcard") |
34 | | - return True |
35 | | - except Exception as e: |
36 | | - print(f"WARNING: failed to mount SD card: {e}") |
37 | | - return False |
| 42 | + def is_mounted(self, mount_point): |
| 43 | + try: |
| 44 | + return mount_point in os.listdir('/') and os.path.ismount(mount_point) |
| 45 | + except: |
| 46 | + return False |
38 | 47 |
|
39 | | -# Returns True if everything successful, otherwise False if it failed to mount |
40 | | -def mount_sdcard_optional_format(): |
41 | | - global sdcard # should have been initialized by inform_sdcard() |
| 48 | + def list(self, mount_point): |
| 49 | + try: |
| 50 | + return os.listdir(mount_point) |
| 51 | + except: |
| 52 | + return [] |
42 | 53 |
|
43 | | - if not sdcard: |
44 | | - print("WARNING: no machine.SDCard object has been initialized at boot, aborting...") |
45 | | - return False |
| 54 | +# --- Global instance (singleton) --- |
| 55 | +_manager = None |
46 | 56 |
|
47 | | - # Try to detect SD card |
48 | | - try: |
49 | | - sdcard.info() |
50 | | - print("SD card detected") |
51 | | - except Exception as e: |
52 | | - print(f"WARNING: sdcard.info() got exception: {e}") |
53 | | - print("SD card not detected or hardware issue") |
54 | | - return False |
| 57 | +def init(spi_bus, cs_pin): |
| 58 | + """Initialize the global SD card manager.""" |
| 59 | + global _manager |
| 60 | + if _manager is None: |
| 61 | + _manager = SDCardManager(spi_bus, cs_pin) |
| 62 | + return _manager |
55 | 63 |
|
56 | | - if not try_mount(sdcard): |
57 | | - if format_sdcard(sdcard): |
58 | | - if not try_mount(sdcard): |
59 | | - print(f"WARNING: failed to mount SD card even after formatting, aborting...") |
60 | | - return False |
61 | | - else: |
62 | | - print("WARNING: Could not format SD card - check hardware connections or reformat on a PC. Aborting...") |
63 | | - return False |
| 64 | +def get(): |
| 65 | + """Get the global SD card manager instance.""" |
| 66 | + return _manager |
64 | 67 |
|
65 | | - try: |
66 | | - print("SD card contents:", os.listdir('/sdcard')) |
67 | | - except Exception as e: |
68 | | - print(f"WARNING: SD card did not fail to mount but could not list SD card contents: {e}") |
69 | | - return False |
| 68 | +# Optional: convenience functions |
| 69 | +def mount(mount_point): |
| 70 | + mgr = get() |
| 71 | + return mgr and mgr.mount(mount_point) |
70 | 72 |
|
71 | | - print("SD card mounted successfully!") |
72 | | - return True |
| 73 | +def mount_with_format(mount_point): |
| 74 | + mgr = get() |
| 75 | + if mgr and not mgr.mount(mount_point): |
| 76 | + return mgr.format_and_mount(mount_point) |
| 77 | + return mgr and mgr.is_mounted(mount_point) |
0 commit comments