Skip to content

Commit f393f27

Browse files
authored
Merge pull request #3062 from FoamyGuy/sd_excepts
update sd examples for 10.x behavior
2 parents 736bedf + 748f988 commit f393f27

File tree

4 files changed

+38
-77
lines changed

4 files changed

+38
-77
lines changed

Metro_RP2350_Examples/CircuitPython_SDCard_ListFiles/code-cp10x.py

Lines changed: 0 additions & 37 deletions
This file was deleted.

Metro_RP2350_Examples/CircuitPython_SDCard_ListFiles/code.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
"""
55
CircuitPython Essentials SD Card Read Demo
6-
* Not compatible with CircuitPython 10.x
76
"""
87

98
import os
@@ -16,14 +15,28 @@
1615
# The SD_CS pin is the chip select line.
1716
SD_CS = board.SD_CS
1817

19-
# Connect to the card and mount the filesystem.
20-
cs = digitalio.DigitalInOut(SD_CS)
21-
sdcard = adafruit_sdcard.SDCard(busio.SPI(board.SD_SCK, board.SD_MOSI, board.SD_MISO), cs)
22-
vfs = storage.VfsFat(sdcard)
23-
storage.mount(vfs, "/sd")
18+
# For CircuitPython 9.x we must initialize and mount the SD card manually.
19+
# On CircuitPython 10.x the core will automatically initialize and mount the SD.
20+
# This try/except block can be removed with 10.x has a stable release.
21+
try:
22+
# Initialize the Chip Select pin for the SD card
23+
cs = digitalio.DigitalInOut(SD_CS)
24+
# Initialize the SD card
25+
sdcard = adafruit_sdcard.SDCard(
26+
busio.SPI(board.SD_SCK, board.SD_MOSI, board.SD_MISO), cs
27+
)
28+
# Mount the SD card
29+
vfs = storage.VfsFat(sdcard)
30+
storage.mount(vfs, "/sd")
31+
except ValueError:
32+
# "ValueError SD_CS in use" error happen on CircuitPython 10.x
33+
# because the core initialized the SD automatically. The error
34+
# can be ignored on 10.x.
35+
pass
2436

2537
# Use the filesystem as normal! Our files are under /sd
2638

39+
2740
# This helper function will print the contents of the SD
2841
def print_directory(path, tabs=0):
2942
for file in os.listdir(path):

Metro_RP2350_Examples/CircuitPython_SDCard_Write/code-cp10x.py

Lines changed: 0 additions & 23 deletions
This file was deleted.

Metro_RP2350_Examples/CircuitPython_SDCard_Write/code.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,6 @@
44

55
"""
66
CircuitPython Essentials SD Card Write Demo
7-
REMOVE THIS LINE AND ALL BELOW IT BEFORE SUBMITTING TO LEARN
8-
Update CHIP_SELECT_PIN to match the CS pin on your board.
9-
10-
For example, for the Metro ESP32-S3, you would use: board.SD_CS.
11-
12-
This code stops working with CircuitPython 10.x which automounts /sd
137
"""
148

159
import time
@@ -23,11 +17,25 @@
2317
# The SD_CS pin is the chip select line.
2418
SD_CS = board.SD_CS
2519

26-
# Connect to the card and mount the filesystem.
27-
cs = digitalio.DigitalInOut(SD_CS)
28-
sdcard = adafruit_sdcard.SDCard(busio.SPI(board.SD_SCK, board.SD_MOSI, board.SD_MISO), cs)
29-
vfs = storage.VfsFat(sdcard)
30-
storage.mount(vfs, "/sd")
20+
21+
# For CircuitPython 9.x we must initialize and mount the SD card manually.
22+
# On CircuitPython 10.x the core will automatically initialize and mount the SD.
23+
# This try/except block can be removed with 10.x has a stable release.
24+
try:
25+
# Initialize the Chip Select pin for the SD card
26+
cs = digitalio.DigitalInOut(SD_CS)
27+
# Initialize the SD card
28+
sdcard = adafruit_sdcard.SDCard(
29+
busio.SPI(board.SD_SCK, board.SD_MOSI, board.SD_MISO), cs
30+
)
31+
# Mount the SD card
32+
vfs = storage.VfsFat(sdcard)
33+
storage.mount(vfs, "/sd")
34+
except ValueError:
35+
# "ValueError SD_CS in use" error happen on CircuitPython 10.x
36+
# because the core initialized the SD automatically. The error
37+
# can be ignored on 10.x.
38+
pass
3139

3240
# Use the filesystem as normal! Our files are under /sd
3341

0 commit comments

Comments
 (0)