Hello there!
I have encountered another problem with my G120E based custom board. I have searched the forum for quite a while and found many results concerning SD cards, but none of them quite matched my problem, so I had to create a new topic. I’m using a SD Card to log some data every few hours, so there is a SD slot on the board. The problem is that when I deploy the sample code in the Support section ([url]https://www.ghielectronics.com/docs/51/accessing-folders-and-files#476[/url]), neither the Insert nor Eject event gets fired. Instead, I get the following Exception:
#### Exception System.Exception - 0xffffffff (3) ####
#### Message:
#### GHI.IO.Storage.SDCard::NativeConstructor [IP: 0000] ####
#### GHI.IO.Storage.SDCard::.ctor [IP: 0029] ####
#### GHI.IO.Storage.SDCard::.ctor [IP: 0005] ####
#### Program::SDMountThread [IP: 003c] ####
A first chance exception of type ‘System.Exception’ occurred in GHI.Hardware.dll
Sample code:
using System;
using System.IO;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.IO;
using Microsoft.SPOT.Hardware;
using GHI.IO.Storage;
using GHI.Pins;
public class Program
{
// evt is used to avoid the possibility that accesses to the
// mounted file system do not occur until mount()
// is fully done.
private static AutoResetEvent evt = new AutoResetEvent(false);
//Make sure to set the pin to your sd card detect pin.
private static InputPort sdCardDetect = new InputPort((Cpu.Pin)G120E.Gpio.P1_12, false, Port.ResistorMode.PullUp);
public static void Main()
{
RemovableMedia.Insert += new InsertEventHandler(RemovableMedia_Insert);
RemovableMedia.Eject += new EjectEventHandler(RemovableMedia_Eject);
// Start auto mounting thread
new Thread(SDMountThread).Start();
evt.WaitOne(); // yield here until mounting and initializing is finished
// Your program goes here
// ...
}
// This event is fired by unmount; not neccesarily by physical ejection of media
static void RemovableMedia_Eject(object sender, MediaEventArgs e)
{
Debug.Print("SD card unmounted, eject event fired");
// as desired signal other thread(s) in application
// that unmount occurred
}
static void RemovableMedia_Insert(object sender, MediaEventArgs e)
{
Debug.Print("Insert event fired; SD card mount is finished.");
// insert code here for anything the program wants to do immediately
// after mounting occurs...
if (e.Volume.IsFormatted)
{
Debug.Print("Available folders:");
string[] strs = Directory.GetDirectories(e.Volume.RootDirectory);
for (int i = 0; i < strs.Length; i++)
Debug.Print(strs[i]);
Debug.Print("Available files:");
strs = Directory.GetFiles(e.Volume.RootDirectory);
for (int i = 0; i < strs.Length; i++)
Debug.Print(strs[i]);
}
else
{
Debug.Print("SD card is not formatted. Formatting...");
// VolumeInfo is the class that contains volume information for a specific
// media.
// .GetVolumes()[0] aquires the first volume on the device. Change the
// index for different volumes.
// .Format("FAT", 0); Selects the "FAT" file system as the format type.
VolumeInfo.GetVolumes()[0].Format("FAT", 0);
}
evt.Set(); // proceed with other processing
}
public static void SDMountThread()
{
SDCard SD = null;
const int POLL_TIME = 500; // check every 500 millisecond
bool sdExists;
while (true)
{
try // If SD card was removed while mounting, it may throw exceptions
{
sdExists = sdCardDetect.Read();
// make sure it is fully inserted and stable
if (sdExists)
{
Thread.Sleep(50);
sdExists = sdCardDetect.Read();
}
if (sdExists && SD == null)
{
// here comes the line which throws the exception
SD = new SDCard();
SD.Mount();
}
else if (!sdExists && SD != null)
{
SD.Unmount();
SD.Dispose();
SD = null;
}
}
catch
{
if (SD != null)
{
SD.Dispose();
SD = null;
}
}
Thread.Sleep(POLL_TIME);
}
}
}
I’m using the SD DAT3 pin (GPIO P1.12 on the G120E) for card detection with resistor in Pull-up Mode. With this setting, I get the above-mentioned Exception every few seconds, regardless the state of the SD card (inserted/ejected). If I change the resistor mode to Disabled, nothing happens, not even the Exception. What could be causing the problem? I’d be grateful for your advice.
I’ve tried 3 SD cards from 3 manufacturers, all formatted in my Windows PC, no progress. The exception occurs on the line SD = new SDCard() in the SDMountThread, so according to the code and debugger, sdExists = sdCardDetect.Read() reads TRUE, but then is unable to initialize the SDCard object and throws the Exception.