Fez Spider EMX SDCARD PersistentStorage

Hi,

I’m trying to access the SD card but I can not. By monitoring with debug always gives error within PersistentStorage (“SD”);

I read several other threads on the reasons that can cause this error, some spoke in the power supply, comment on other versions of gadgeteer and also talk about the type of formatting required for the SD card. I took every care seen in previous threads but did not succeed.

I formatted the card with FAT32, also used the formatting of VolumeInfo:

Volume.Format (“FAT32”, 0, true);
Volume.Format (“FAT”, 0);
Volume.Format (0, true);

I left only the gadgeteers required for my test to reduce the use of power.

Follows the settings of my environment:

Hardware:

Fez Spider Mainboard 1.0
SDCard 1.4
USBClient DP 1.3
SanDisk MicroSDHC 4Gb class 2
SanDisk MicorSDHC Adapter

Fez Config:

Loader (TinyBooter) version information:
4.2.11.1 on this computer.
4.2.11.1 on this device

Firmware (TinyCLR) version information:
4.2.11.1 on this computer.
4.2.11.1 on this device.

Software

VS2012
MicroFrameworkSDK NETMF SDK 4.3 (RTM) for VS2012
MicroFrameworkSDK NETMF SDK 4.2 (RTM QFE2) for VS2010
GHI NETMF v4.2 and Gadgeteer Package 2013 R3

Error, inside RemovableMedia_Insert


using System;
using System.Text;
using System.Collections;
using System.Threading;
using System.IO;
using Microsoft.SPOT;
using Microsoft.SPOT.IO;
using Gadgeteer.Modules.GHIElectronics;
using GHI.Premium.IO;

namespace GadgeteerApp2
{
    public partial class Program
    {
        protected PersistentStorage _pStorage;

        void ProgramStarted()
        {
            RemovableMedia.Insert += RemovableMedia_Insert;
            RemovableMedia.Eject += RemovableMedia_Eject;

            Debug.Print("Program Started");
        }

        private void RemovableMedia_Insert(object sender, MediaEventArgs e)
        {
            if (this.IsStorageAvailable("SD"))
            {
                Debug.Print("Storage \"" + e.Volume.RootDirectory + "\" is inserted.");

                if (e.Volume.IsFormatted)
                {
                    try
                    {
                        _pStorage = new PersistentStorage("SD"); // Create a new storage device

                        if (_pStorage != null)
                        {
                            _pStorage.MountFileSystem(); // Mount the file system

                            _pStorage.UnmountFileSystem();
                            _pStorage.Dispose();
                            _pStorage = null;
                        }
                    }
                    catch (Exception ex)
                    {
                        /*
                        Storage "\SD" is inserted.
                        #### Exception System.InvalidOperationException - CLR_E_INVALID_OPERATION (5) ####
                        #### Message: 
                        #### GHI.Premium.IO.PersistentStorage::.ctor [IP: 0000] ####
                        #### GadgeteerApp2.Program::RemovableMedia_Insert [IP: 003c] ####
                        #### Microsoft.SPOT.IO.InsertEventHandler::Invoke [IP: 80171067] ####
                        #### Microsoft.SPOT.IO.RemovableMedia::MessageHandler [IP: 0041] ####
                        A first chance exception of type 'System.InvalidOperationException' occurred in GHI.Premium.IO.dll
                         */
                    }
                }
                else
                {
                    Debug.Print("Storage is not formatted. Format on PC with FAT32/FAT16 first.");
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void RemovableMedia_Eject(object sender, MediaEventArgs e)
        {
            Debug.Print("Storage \"" + e.Volume.RootDirectory + "\" is ejected.");
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        private bool IsStorageAvailable(string baseDir)
        {
            bool flag = false;

            try // If SD card was removed while mounting, it may throw exceptions
            {
                switch (baseDir)
                {
                    case "SD":
                        bool sdExists = PersistentStorage.DetectSDCard();

                        if (sdExists) // make sure it is fully inserted and stable
                        {
                            Thread.Sleep(50);
                            sdExists = PersistentStorage.DetectSDCard();
                        }

                        if (sdExists)
                        {
                            flag = true;
                        }
                        else
                        {
                            flag = false;
                        }

                        break;
                }
            }
            catch
            {
                flag = false;
            }

            return flag;
        }
    }
}

I have no idea what might be causing this error.

Thanks for the help,
Andrew Paes

Not sure b/c I’m not a Gadgeteer guy, but.
If you add the sd card module in designer, I think you should access it by this.
According to your screenshot there should be an class var called sdCard.
I think the Gadgeteer driver internally already initializes the SD card (new PersistentStorrage(“SD”)).
Not sure if it’s also mounted, probably not.

So you should remove all PersistenStorrage / RemovaleMedia stuff and see what the sdCard object offers to you.

1 Like

there’s a Gadgeteer program that helps you test SD card storage and prove it’s working before you add your own code… https://www.ghielectronics.com/community/codeshare/entry/643

Part of your issue is you mount then dismount/dispose the SD object in the handler - that’s bad. But as @ Reinhard says, you should just use the Gadgeteer provided controls and not do all the PersistentStorage hard work yourself.

1 Like

Hi Reinhard,

You were right, no need to start PersistentStorage.

Brett Thanks for the tip, it worked fine.

Now I’m having problems with my display, searched the forum and saw that they had similar problems. Did my question in this thread.

https://www.ghielectronics.com/community/forum/topic?id=5472&page=2 # msg146990

Thank you all,
Andrew Paes