SD Card Error

I am getting weird behavior problems with the FEZ Spider SD Card module.

I am running EMX Version 4.1.8.0. I have the GHI 4.1 Package installed.

I was running into problems with a program I am creating using the SD Card. I decided to create a simple program to see if I could get the SD Card working.


void ProgramStarted()
        {
            /*******************************************************************************************
            Modules added in the Program.gadgeteer designer view are used by typing 
            their name followed by a period, e.g.  button.  or  camera.
            
            Many modules generate useful events. Type +=<tab><tab> to add a handler to an event, e.g.:
                button.ButtonPressed +=<tab><tab>
            
            If you want to do something periodically, use a GT.Timer and handle its Tick event, e.g.:
                GT.Timer timer = new GT.Timer(1000); // every second (1000ms)
                timer.Tick +=<tab><tab>
                timer.Start();
            *******************************************************************************************/


            // Use Debug.Print to show messages in Visual Studio's "Output" window during debugging.
            Debug.Print("Program Started");

            sdCard.SDCardMounted += new SDCard.SDCardMountedEventHandler(sdCard_SDCardMounted);
            sdCard.SDCardUnmounted += new SDCard.SDCardUnmountedEventHandler(sdCard_SDCardUnmounted);
        }

        void sdCard_SDCardUnmounted(SDCard sender)
        {
            Debug.Print("unmounted");
        }

        void sdCard_SDCardMounted(SDCard sender, GT.StorageDevice SDCard)
        {
            Debug.Print("mounted");
        }

The output I get from the debugger when I inserted my SD Card.

The thread ‘’ (0x2) has exited with code 0 (0x0).
Using mainboard GHIElectronics-FEZSpider version 1.0
Program Started

#### Exception System.Exception - 0xffffffff (1) ####
#### Message: 
#### GHIElectronics.NETMF.IO.PersistentStorage::.ctor [IP: 0000] ####
#### GHIElectronics.Gadgeteer.FEZSpider::_MountStorageDevice [IP: 0005] ####
#### Gadgeteer.Modules.GHIElectronics.SDCard::MountSDCard [IP: 0012] ####
#### Gadgeteer.Modules.GHIElectronics.SDCard::_sdCardDetect_Interrupt [IP: 000a] ####
#### Gadgeteer.Interfaces.InterruptInput::OnInterruptEvent [IP: 0055] ####
#### System.Reflection.MethodBase::Invoke [IP: 0000] ####
#### Gadgeteer.Program::DoOperation [IP: 001a] ####
#### Microsoft.SPOT.Dispatcher::PushFrameImpl [IP: 004a] ####
#### Microsoft.SPOT.Dispatcher::PushFrame [IP: 001d] ####
#### Microsoft.SPOT.Dispatcher::Run [IP: 0006] ####
#### Gadgeteer.Program::Run [IP: 001c] ####
#### GadgeteerApp1.Program::Main [IP: 001a] ####

A first chance exception of type ‘System.Exception’ occurred in GHIElectronics.NETMF.IO.dll
SDCard ERROR : Error mounting SD card - no card detected.
mounted
unmounted

The ‘mounted’ and ‘unmounted’ status happened at the same time when I inserted my SD Card. My SD Card was formatted as FAT32 and I reformatted it to be sure.
Any idea what is going on?

Thanks,
Weston

While USB specifications say ports should provide 500mA, we rarely see a port that can actually provide 500mA. We always recommend the use of powered hubs or a power pack instead of relying on the USB power. Use a powered hub from a known brand, not a cheap hub. If your device has the option of using a power pack (for example the USB Client DP Module) then you can use a power pack instead. 9V 1A is typically recommended but check the bottom of the circuit board for printed voltage range. All power connectors on GHI products are 2.1mm with positive on the inside and negative on the outer ring.

Not using a powered hub or power pack can cause:
[ul]Unexplained behavior
Device does not function
Device functions intermittently
Device functions but network fails
Device functions but SD card fails
Device functions but firmware update fails[/ul]
(Generated by QuickReply)

Thanks WouterH, I will give that a try when I get home. The weird thing was that it was working fine yesterday until about 4 hours later. I thought it was because of a change I made in my program so I created a simple one to debug but that did not even work.

I was able to reproduce the error using the following code:


public partial class Program
    {
        // This method is run when the mainboard is powered up or reset.   
        void ProgramStarted()
        {
            /*******************************************************************************************
            Modules added in the Program.gadgeteer designer view are used by typing 
            their name followed by a period, e.g.  button.  or  camera.
            
            Many modules generate useful events. Type +=<tab><tab> to add a handler to an event, e.g.:
                button.ButtonPressed +=<tab><tab>
            
            If you want to do something periodically, use a GT.Timer and handle its Tick event, e.g.:
                GT.Timer timer = new GT.Timer(1000); // every second (1000ms)
                timer.Tick +=<tab><tab>
                timer.Start();
            *******************************************************************************************/


            // Use Debug.Print to show messages in Visual Studio's "Output" window during debugging.
            Debug.Print("Program Started");


            RemovableMedia.Eject += new EjectEventHandler(RemovableMedia_Eject);
            RemovableMedia.Insert += new InsertEventHandler(RemovableMedia_Insert);

            // Set the SD detect pin
            Cpu.Pin SD_CARD_INSERTED_PIN = GT.Socket.GetSocket(5, true, null, null).CpuPins[3];
            sdDetectPin = new InputPort(SD_CARD_INSERTED_PIN, true, Port.ResistorMode.PullUp);

            // Start auto mounting thread
            new Thread(SDMountThread).Start();

            // Your program goes here
            // ...

            Thread.Sleep(Timeout.Infinite);
        }

        static void RemovableMedia_Eject(object sender, MediaEventArgs e)
        {
            Debug.Print("SD card ejected");
        }
 
        static void RemovableMedia_Insert(object sender, MediaEventArgs e)
        {
            Debug.Print("SD card inserted");
 
            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");
            }
        }
 
        static InputPort sdDetectPin;
        public static void SDMountThread()
        {
            PersistentStorage sdPS = 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 = sdDetectPin.Read();

                    Debug.Print("SD Exists: " + sdExists);

                    // make sure it is fully inserted and stable
                    if (sdExists)
                    {
                        Thread.Sleep(50);
                        sdExists = sdDetectPin.Read();
                    }
 
                    if (sdExists && sdPS == null)
                    {
                        sdPS = new PersistentStorage("SD");
                        sdPS.MountFileSystem();
                    }
                    else if (!sdExists && sdPS != null)
                    {
                        sdPS.UnmountFileSystem();
                        sdPS.Dispose();
                        sdPS = null;
                    }
                }
                catch
                {
                    if (sdPS != null)
                    {
                        sdPS.Dispose();
                        sdPS = null;
                    }
                }
 
                Thread.Sleep(POLL_TIME);
            }
        }
    }

The pin indicated that the SD Card was inserted and the persistence storage was trying to mount the device. Does this mean my SD Card Hardware is not working?
Debug Output:

SD Exists: True
SD Exists: True
SD Exists: True
SD Exists: True
SD Exists: True
SD Exists: True
SD Exists: True
SD Exists: True
SD Exists: True
SD Exists: True
SD Exists: True
SD Exists: True
SD Exists: True
SD Exists: True
SD Exists: True
SD Exists: True
SD Exists: True
SD Exists: True
SD Exists: True
SD Exists: True
SD Exists: True
SD Exists: True
SD Exists: True
SD Exists: True
SD Exists: True
SD Exists: True
SD Exists: True
SD Exists: True
SD Exists: True
SD Exists: True
SD Exists: True
SD Exists: True
SD Exists: True
SD Exists: True
SD Exists: True
SD Exists: True
SD Exists: False
SD card ejected
SD Exists: True
SD card inserted
Available folders:
Available files:
\SD\config.ini
\SD\Application_log.csv
SD Exists: False
SD card ejected
SD Exists: True
#### Exception System.Exception - 0xffffffff (3) ####
#### Message:
#### GHIElectronics.NETMF.IO.PersistentStorage::.ctor [IP: 0000] ####
#### GadgeteerApp2.Program::SDMountThread [IP: 004c] ####
A first chance exception of type ‘System.Exception’ occurred in GHIElectronics.NETMF.IO.dll
SD Exists: True
#### Exception System.Exception - 0xffffffff (3) ####
#### Message:
#### GHIElectronics.NETMF.IO.PersistentStorage::.ctor [IP: 0000] ####
#### GadgeteerApp2.Program::SDMountThread [IP: 004c] ####
A first chance exception of type ‘System.Exception’ occurred in GHIElectronics.NETMF.IO.dll
SD Exists: True
#### Exception System.Exception - 0xffffffff (3) ####
#### Message:
#### GHIElectronics.NETMF.IO.PersistentStorage::.ctor [IP: 0000] ####
#### GadgeteerApp2.Program::SDMountThread [IP: 004c] ####