2525
2626
2727import machine
28- from machine import Pin , SPI
29- import sdcard
3028
3129
3230import os
3533import random
3634import json
3735
36+ SDCARD_MOUNT_POINT = "/sdcard"
37+
38+ # global variable to track if the SD card is mounted
39+ _mounted_sd_card = False
40+
3841# Define the boards we support with this deme - This is a dictionary the key being
3942# the board uname.machine value, and value a tuple that contains SPI bus number and CS ping number.
4043SupportedBoards = {
4144 "SparkFun IoT RedBoard RP2350 with RP2350" : (1 , 9 ),
42- "SparkFun IoT RedBoard ESP32 with ESP32" : (2 , 5 )
45+ "SparkFun IoT RedBoard ESP32 with ESP32" : (2 , 5 ),
46+ "Teensy 4.1 with MIMXRT1062DVJ6A" : (- 1 , - 1 )
4347}
4448
4549# ------------------------------------------------------------
4650
4751
48- def mount_sd_card ():
49- """
50- Mounts an SD card to the filesystem.
51-
52- This function checks if the current board is supported, initializes the SPI bus and CS pin,
53- creates the SD card object, and mounts the SD card to the filesystem.
54-
55- Returns:
56- bool: True if the SD card was successfully mounted, False otherwise.
57-
58- """
52+ def mount_sd_card (spi_bus , cs_pin ):
5953
60- # is this a supported board?
61- board_name = os .uname ().machine
62- if board_name not in SupportedBoards :
63- print ("This board is not supported" )
54+ global _mounted_sd_card
55+ try :
56+ import sdcard
57+ except ImportError :
58+ print ("[Error] sdcard module not found. Please install it." )
6459 return False
6560
66- # Get the SPI bus and CS pin for this board
67- spi_bus , cs_pin = SupportedBoards [board_name ]
61+ from machine import Pin , SPI
6862
6963 # Create the SPI object
7064 spi = SPI (spi_bus , baudrate = 1000000 , polarity = 0 , phase = 0 )
@@ -81,15 +75,48 @@ def mount_sd_card():
8175 # Mount the SD card
8276 try :
8377 vfs = uos .VfsFat (sd )
84- uos .mount (vfs , "/sd" )
78+ uos .mount (vfs , SDCARD_MOUNT_POINT )
8579 except Exception as e :
8680 print ("[Error] Failed to mount the SD Card" , e )
8781 return False
8882
89- print ("SD Card mounted successfully" )
90-
83+ _mounted_sd_card = True
9184 return True
9285
86+
87+ def setup_sd_card ():
88+ """
89+ Mounts an SD card to the filesystem.
90+
91+ This function checks if the current board is supported, initializes the SPI bus and CS pin,
92+ creates the SD card object, and mounts the SD card to the filesystem.
93+
94+ Returns:
95+ bool: True if the SD card was successfully mounted, False otherwise.
96+
97+ """
98+
99+ # is this a supported board?
100+ board_name = os .uname ().machine
101+ if board_name not in SupportedBoards :
102+ print ("This board is not supported" )
103+ return False
104+
105+ # Get the SPI bus and CS pin for this board
106+ spi_bus , cs_pin = SupportedBoards [board_name ]
107+
108+ # do we need to mount the sd card? (Teensy auto mounts)
109+ status = False
110+ if spi_bus != - 1 :
111+ status = mount_sd_card (spi_bus , cs_pin )
112+ else :
113+ status = True
114+
115+ if status == True :
116+ print ("SD Card mounted successfully" )
117+
118+ return status
119+
93120# ------------------------------------------------------------
94121
95122
@@ -149,7 +176,7 @@ def setup_log_file(filename):
149176 """
150177
151178 try :
152- fs = open ("/sd/" + filename , "w" )
179+ fs = open (SDCARD_MOUNT_POINT + os . sep + filename , "w" )
153180 except Exception as e :
154181 print ("[Error] Failed to open log file:" , e )
155182 return None
@@ -219,10 +246,11 @@ def sdcard_log_example(filename="mpy_sdcard_log.txt", count=20, interval=2):
219246 None
220247 """
221248
249+ global _mounted_sd_card
222250 print ("Logging to: {filename}, every {interval} seconds for {count} iterations.\n " .format (
223251 filename = filename , interval = interval , count = count ))
224252 # Mount the SD card
225- if not mount_sd_card ():
253+ if not setup_sd_card ():
226254 print ("Failed to mount SD card" )
227255 return
228256
@@ -231,10 +259,11 @@ def sdcard_log_example(filename="mpy_sdcard_log.txt", count=20, interval=2):
231259 # Log the data
232260 log_data (filename , count = count , interval = interval , to_console = True )
233261
234- # Unmount the SD card
235- uos .umount ("/sd" )
236-
237- print ("\n SD Card unmounted successfully" )
262+ # Unmount the SD card if we need to
263+ if _mounted_sd_card :
264+ uos .umount (SDCARD_MOUNT_POINT )
265+ _mounted_sd_card = False
266+ print ("\n SD Card unmounted successfully" )
238267
239268# ------------------------------------------------------------
240269# Run method for the example
0 commit comments