25
25
26
26
27
27
import machine
28
- from machine import Pin , SPI
29
- import sdcard
30
28
31
29
32
30
import os
35
33
import random
36
34
import json
37
35
36
+ SDCARD_MOUNT_POINT = "/sdcard"
37
+
38
+ # global variable to track if the SD card is mounted
39
+ _mounted_sd_card = False
40
+
38
41
# Define the boards we support with this deme - This is a dictionary the key being
39
42
# the board uname.machine value, and value a tuple that contains SPI bus number and CS ping number.
40
43
SupportedBoards = {
41
44
"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 )
43
47
}
44
48
45
49
# ------------------------------------------------------------
46
50
47
51
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 ):
59
53
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." )
64
59
return False
65
60
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
68
62
69
63
# Create the SPI object
70
64
spi = SPI (spi_bus , baudrate = 1000000 , polarity = 0 , phase = 0 )
@@ -81,15 +75,48 @@ def mount_sd_card():
81
75
# Mount the SD card
82
76
try :
83
77
vfs = uos .VfsFat (sd )
84
- uos .mount (vfs , "/sd" )
78
+ uos .mount (vfs , SDCARD_MOUNT_POINT )
85
79
except Exception as e :
86
80
print ("[Error] Failed to mount the SD Card" , e )
87
81
return False
88
82
89
- print ("SD Card mounted successfully" )
90
-
83
+ _mounted_sd_card = True
91
84
return True
92
85
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
+
93
120
# ------------------------------------------------------------
94
121
95
122
@@ -149,7 +176,7 @@ def setup_log_file(filename):
149
176
"""
150
177
151
178
try :
152
- fs = open ("/sd/" + filename , "w" )
179
+ fs = open (SDCARD_MOUNT_POINT + os . sep + filename , "w" )
153
180
except Exception as e :
154
181
print ("[Error] Failed to open log file:" , e )
155
182
return None
@@ -219,10 +246,11 @@ def sdcard_log_example(filename="mpy_sdcard_log.txt", count=20, interval=2):
219
246
None
220
247
"""
221
248
249
+ global _mounted_sd_card
222
250
print ("Logging to: {filename}, every {interval} seconds for {count} iterations.\n " .format (
223
251
filename = filename , interval = interval , count = count ))
224
252
# Mount the SD card
225
- if not mount_sd_card ():
253
+ if not setup_sd_card ():
226
254
print ("Failed to mount SD card" )
227
255
return
228
256
@@ -231,10 +259,11 @@ def sdcard_log_example(filename="mpy_sdcard_log.txt", count=20, interval=2):
231
259
# Log the data
232
260
log_data (filename , count = count , interval = interval , to_console = True )
233
261
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" )
238
267
239
268
# ------------------------------------------------------------
240
269
# Run method for the example
0 commit comments