Skip to content

Commit 1f74151

Browse files
authored
Update SDcard mount sample for latest update (#396)
1 parent b5d950c commit 1f74151

File tree

1 file changed

+64
-25
lines changed
  • samples/System.IO.FileSystem/MountExample

1 file changed

+64
-25
lines changed

samples/System.IO.FileSystem/MountExample/Program.cs

Lines changed: 64 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ namespace MountExample
1111
/// </summary>
1212
public class Program
1313
{
14-
static SDCard mycard;
14+
static SDCard mycard0;
15+
//static SDCard mycard1;
16+
//static SDCard mycard2;
1517

1618
public static void Main()
1719
{
@@ -20,40 +22,53 @@ public static void Main()
2022
// Initialise an instance of SDCard using constructor specific to your board/adapter
2123

2224
// Boards with SD card interface and parameters built in to firmware
23-
// mycard = new SDCard();
25+
// mycard0 = new SDCard();
2426

2527
// Boards with SDIO/MMC interface with 1 bit data lines ( e.g. ESP32 Olimex EVB , POE )
26-
// mycard = new SDCard(new SDCard.SDCardMmcParameters { dataWidth = SDCard.SDDataWidth._1_bit });
28+
//mycard0 = new SDCard(new SDCardMmcParameters { dataWidth = SDCard.SDDataWidth._1_bit });
2729

2830
// Boards with SDIO/MMC and 4 data lines like Espressif Esp32 Wrover all models, these also support card detect
29-
//mycard = new SDCard(new SDCard.SDCardMmcParameters { dataWidth = SDCard.SDDataWidth._4_bit, enableCardDetectPin = true, cardDetectPin = 21 });
31+
//mycard0 = new SDCard(
32+
// new SDCardMmcParameters { dataWidth = SDCard.SDDataWidth._4_bit },
33+
// new CardDetectParameters { autoMount = true, enableCardDetectPin = true, cardDetectedState = false, cardDetectPin = 21 });
3034

3135
// SPI constructors, check SPI bus pins and chip select pins are correct for your board
3236
// Boards with SDCard on SPI bus, SPI 1 default , no card detect
33-
mycard = new SDCard(new SDCard.SDCardSpiParameters { spiBus = 1, chipSelectPin = 22 });
37+
mycard0 = new SDCard(new SDCardSpiParameters { spiBus = 1, chipSelectPin = 22 });
3438

3539
// SPI adapter with Card detect
36-
// mycard = new SDCard(new SDCard.SDCardSpiParameters { spiBus = 1, chipSelectPin = 22, enableCardDetectPin = true, cardDetectPin = 21 });
40+
//mycard0 = new SDCard(
41+
// new SDCardSpiParameters { spiBus = 1, chipSelectPin = 22 },
42+
// new CardDetectParameters { autoMount = true, enableCardDetectPin = true, cardDetectedState = false, cardDetectPin = 21 });
3743

38-
Debug.WriteLine("SDcard inited");
44+
// It also possible to mount a 2nd or 3rd SD card by specifying the SD card slotIndex which defaults to 0.
45+
// i.e a SDIO as first card and SPI as 2nd. Some boards support 2 SDIO devices. ESP32, ESP32_S3 & ESP32_P4
46+
//mycard1 = new SDCard(new SDCardSpiParameters { slotIndex = 1, spiBus = 1, chipSelectPin = 22 });
47+
//mycard2 = new SDCard(new SDCardSpiParameters { slotIndex = 2, spiBus = 1, chipSelectPin = 22 });
3948

40-
// Option 1 - No card detect
41-
// Try to mount card
42-
MountMyCard();
49+
Debug.WriteLine("SDcard inited");
4350

44-
// Option 2 use events to mount
45-
// if Card detect available, enable events and mount when card inserted
46-
// Enable Storage events if you have Card detect on adapter
51+
// Use storage events to detect when a drive had been mounted or unmounted from system
4752
StorageEventManager.RemovableDeviceInserted += StorageEventManager_RemovableDeviceInserted;
4853
StorageEventManager.RemovableDeviceRemoved += StorageEventManager_RemovableDeviceRemoved;
4954

55+
56+
// Mount option 1 - No card detect
57+
// Try to manually mount card
58+
MountMyCard();
59+
60+
// Mount Option 2, use events to mount
61+
// Enable Card Detect events if you have Card detect on adapter
62+
//mycard0.CardDetectChanged += Mycard_CardDetectChanged;
63+
64+
5065
// Unmount drive
51-
UnMountIfMounted();
66+
//UnMountIfMounted(mycard0);
5267

5368
Thread.Sleep(Timeout.Infinite);
5469
}
5570

56-
static void UnMountIfMounted()
71+
static void UnMountIfMounted(SDCard mycard)
5772
{
5873
if (mycard.IsMounted)
5974
{
@@ -65,26 +80,23 @@ static bool MountMyCard()
6580
{
6681
try
6782
{
68-
mycard.Mount();
83+
mycard0.Mount();
6984
Debug.WriteLine("Card Mounted");
7085

7186
return true;
7287
}
7388
catch (Exception ex)
7489
{
7590
Debug.WriteLine($"Card failed to mount : {ex.Message}");
76-
Debug.WriteLine($"IsMounted {mycard.IsMounted}");
91+
Debug.WriteLine($"IsMounted {mycard0.IsMounted}");
7792
}
7893

7994
return false;
8095
}
8196

82-
#region Storage Events
83-
84-
// Storage events can be used to automatically mount SD cards when inserted
85-
// This only works for SD card adapter that include card detect pin tied to GPIO pin
86-
// If no Card Detect pin then events not required
97+
#region Events
8798

99+
// Storage events are fired when a drive is successfully mounted in the system and can be used.
88100
private static void StorageEventManager_RemovableDeviceRemoved(object sender, RemovableDriveEventArgs e)
89101
{
90102
Debug.WriteLine($"Card removed - Event:{e.Event} Path:{e.Drive}");
@@ -93,12 +105,39 @@ private static void StorageEventManager_RemovableDeviceRemoved(object sender, Re
93105
private static void StorageEventManager_RemovableDeviceInserted(object sender, RemovableDriveEventArgs e)
94106
{
95107
Debug.WriteLine($"Card inserted - Event:{e.Event} Path:{e.Drive}");
108+
}
96109

97-
// Card just inserted lets try to mount it
98-
MountMyCard();
110+
// The CardDetectChanged event can be used to automatically mount SD cards when a card is inserted.
111+
// This only works for SD card adapter that has a card detect pin tied to GPIO pin.
112+
// If no Card Detect pin then this event is not required.
113+
// This event can be used to automatically mount device if AutoMount is not being used in CardDetectParameters
114+
115+
private static void Mycard_CardDetectChanged(object sender, CardDetectChangedEventArgs e)
116+
{
117+
if (e.CardState == CardDetectState.Inserted)
118+
{
119+
Console.WriteLine($"Card inserted");
120+
121+
// When using manual mount (uncomment)
122+
//card.Mount();
123+
}
124+
else
125+
{
126+
Console.WriteLine($"Card removed");
127+
128+
SDCard card = sender as SDCard;
129+
if (card.IsMounted)
130+
{
131+
try
132+
{
133+
card.Unmount();
134+
}
135+
catch { }
136+
137+
}
138+
}
99139
}
100140

101141
#endregion
102-
103142
}
104143
}

0 commit comments

Comments
 (0)