SDCard functionality problem on G120E custom board

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.

@ nezvek_fel -

If it raised an exception from this line:



Then there is connection problem.

@ Dat - So do you suggest the SD slot is improperly soldered? Or incorrectly routed? However, I’m certain that the routes are correct. The SD slot WP and CD pins are soldered to GND, perhaps this could be the problem?

@ nezvek_fel -

It is hard to say what problem exactly because we don’t have your schematic. But as my understanding, if there is problem with new SDCard() then nothing to do in software, problem should be in hardware connection.

@ Dat - Thank you. I might have found a problem. The pin SD_PWR should power the SD slot/card, I suppose then it should have an output of 3.3 V then, but when i connect the pin to a LED and resistor, it doesn’t light up, so I think the pin doesn’t supply the slot/card. I’ll add an image of the schematic.

@ Dat - So I bypassed the SD_PWR signal from the G120E and connected the SD_PWR pin of the slot to 3.3 V and the Insert event fired, so the SD_PWR pin of the Module truly doesnt supply power to the slot. Has this ever happened? Is it possible that there is a fault on the module? Or could it be somewhat software-driven to work?

@ nezvek_fel -

So does it work now?

Yes, the pin 68 (SD_PWR - G120E) does not supply power for SD. You should supply power to your SD socket by 3.3V.

This is miss-leading in the documents then John. You need to update them to indicate that this pin should be unconnected or better still, do not give it a title. The G120E dev board leaves it unconnected but in the case of nezvek_fel’s design he assumed that this pin was a valid way to power the SD card.

There is no sd power pin. Maybe this was a copy paste error from the emx documentation.

Looks like it :whistle:

It’s also referenced in the EMX to G120E document too.

https://www.ghielectronics.com/downloads/man/EMX%20to%20G120E%20Upgrade%20Guide.pdf

@ nezvek_fel - Thank you everyone for the help. I rerouted power to the SD slot from my 3.3V power supply, problem is solved. I indeed assumed that the SD_PWR pin would provide power for the SD. I still have issues with card detection but it is nothing crucial, as long as read/write works, which it does, I am satisfied. Thanks!

@ nezvek_fel -

[quote]
I still have issues with card detection[/quote]

If you use raw NETMF then I think just make the pin as Interrupt port and check the status.

@ nezvek_fel - The G120E does have the SD_PWR pin – the documentation is correct. There is a bug in the current firmware that prevents it from working. A future release will correct this.

@ John - Thanks, that is good to know:-) So is it viable to just power the SD card via 3.3V instead of the SD_PWR pin? Does the SD_PWR have any special features?

@ nezvek_fel - There is nothing special about the pin, it just provides 3.3V for SD cards.

@ John - Perfect, that’s what I needed to “hear”. Thanks

Hi,
I’m facing the very same issue linked to the SDPower +3.3v not enabled on the G120E.
We are using the same custom board with both an EMX and a G120E chip, and with the G120E, I was receiving an exception when instantiating the SDCard (with or without a card in the reader).
To validate the assumption that our issue was the same, I directly powered the SDCard with +3.3v… and it worked, no more exception when a card was in the reader, and I can now read/write data.

So, instead of modifying the hardware layer on all boards we have, I would much prefer having a G120E firmware enabling Pin 68 (SDPower), as mentionned in this post :slight_smile:

Do you have a working firmware ? And if not, by when will you be able to deliver one enabling SDPower ?

Thanks for your help.
JML

@ jm.laurenti - In the current 2016 R1 firmware only, you can create an OutputPort object on the SD_PWR pin and set it low to enable the power circuit. This is only required until the issue is fixed in a future SDK.


var sdPower = new OutputPort((Cpu.Pin)37, false);

@ John -
Hi, thanks a lot, it works fine, and we can definitively live with that additional line of code until it’s fully fixed ! :slight_smile:

Thanks for the support, and Happy New Year !

JML