Main Site Documentation

Handling Mount() UnMount() SD events


#1

Hi, i found a very usefull code snippet on the codeshare : https://www.ghielectronics.com/community/codeshare/entry/625

Is it possible to use this code in 4.3 netmf? And where to find a GHI.Premium.IO.dll? Already checked codeplex and bitbucket, didn’t find.
Thank you!


#2

Ok, i set the using GHI.IO and GHI.IO.Storage references also include GHI.hardware reference to the project. Also i changed PersistentStorage class on SdCard class.

It seems like the SDCard initialization on Cerbuino bee is under the hood.

I am trying to create


SDCard sd = new SDCard();

and use sd.Mount() and sd.UnMount() methods but it throws an exception.

How to handle this events on Cerbuino bee? I want to eject and insert sd during the programm runing on the device and after i want to write some data to sd.

How to make it work:


SDCard sd = new SDCard();
        // This method is run when the mainboard is powered up or reset.   
        void ProgramStarted()
        {
           
            RemovableMedia.Insert += RemovableMedia_Insert;
            RemovableMedia.Eject += RemovableMedia_Eject;
        }

        void RemovableMedia_Eject(object sender, MediaEventArgs e)
        {
            sd.Unmount();
        }

        void RemovableMedia_Insert(object sender, MediaEventArgs e)
        {
            sd.Mount();
        }


#3

@ Alex Bilityuk -



if we have an exception right there, it is usually:
- Connection is not good, check wire, socket....
- The device is not happy with this SD (rarely), replace by different card.

If we have exception in 


```cs]sd.Mount();[/code


Usually:
- The SD card need to be formatted (PC can do that).
- Change the clock lower

And, 


```cs
 RemovableMedia.Insert += RemovableMedia_Insert;
 RemovableMedia.Eject += RemovableMedia_Eject;

I don’t think they are used for SD, and they are raised after we mounted, if I remember correctly :d


#4

#5

I also get an exception when I try to execute [em]new SDCard()[/em]. In the past few weeks there were some complaints about it (do a search within this forum). I understood that we don’t need to use it anymore, we should use events and properties that reside in Mainboard class instead. E. g. [em]Mainboard.IsSDCardMounted[/em]. But I am not 100% sure here.


#6

Alex, can I ask what you want to achieve ? Why were you looking at this addition, that you felt you needed that the framework didn’t ?


#7

Yes, sure! I want to be able to eject and insert sd card when the programm is running on the device.

  1. Now i turn the power on, send command to capture image, image saves on sd and everything is fine before i eject the sd and insert it back. After sd insertion am trying to capture the image and save it on sd one more time, but it throws an exception. It seems like SD not Mounted properly.

#8

The below solved my problem. I checked if the device is mounted before I call the Mount() method.

I noticed that the SdCard is always auto-mounted when the device restart/power-up. Adding the IsMounted check and prevent a re-mount.

        private void SetupSDCard()
        {
            if (SdCard.IsCardInserted && SdCard.IsCardMounted == false )
            {
                SdCard.Mount();

                var cardReady = false;
                RemovableMedia.Insert += (a, b) =>
                {
                    cardReady = true;
                };
                while (!cardReady)
                {
                    Thread.Sleep(50);
                }
            }

            SdCard.Mounted += SdCard_Mounted;
            SdCard.Unmounted += SdCard_Unmounted;
        }

#9

Hi, jeeshenlee! Thank you for the code snippet, but i don’t have IsCardInserted and IsCardMounted properties in GHI.IO.Storage.SdCard class. Also SdCard class doesn’t have Mounted and Unmounted events to be fired… What dll do you use?


#10

@ Alex, i’m using NETMF 4.3.

Just checked, my SdCard is of the class - [em]Gadgeteer.Modules.GHIElectronics.SdCard[/em]


#11

i suppose that you are using connectable SDCard module in you case. And you declare the SDCard variable setting up the socket number like SDCard sd = new SdCard(3).

But i am trying to set up the onboard FEZ Cerbuino bee SDCard, it doesn’t have a socket number in constructor and it of the class: GHI.IO.Storage.Sdcard. Because of it your code doesn’t work in my scenario… thank you anyway.


#12

Alex, yes. You are right, I’m using the sdcard module with FEZRaptor.

Didn’t notice you are using Cerbuino and didn’t realise it comes with built-in sd card reader.

Not sure if you can check the source code of the Ghi sd card implementation and write your own library for the built-in sdcard reader…


#13

I hope on the GHI employees help… :wall:


#14

I remember I posted somewhere! :smiley:

Anyway, after mounted, if the SD is inserted again, you need to Dispose() the sd object and create a new one.

Unmount is not enough in this case. That is why you get exception for second inserted.

It should work after that.


#15

Dat, is this a good approach ? Is it consistent across all devices ? And can I suggest documenting an established “pattern” of how to achieve this reliably ?


#16

@ Brett -

Yes, we have to do that on all devices. As I know, reading some basic information about the SD is done before mounting. Renew the object is the best way in case we mounted an SD then take it out, re-insert a different one. :smiley:


#17

Sorry Dat, I am not sure I agree with how this should be considered - is this a 4.3 framework change? I always look at the SD card as really the abstraction of the hardware device that allows you to read cards, plural. I should be able to instantiate the hardware (once) and wait for card(s) to come to me… When I load a card, that’s the point I should get volume info and a file system etc, but until that time I’m just waiting around. I shouldn’t have to dispose the SD card object and re-instantiate this every time - and the reason it should be before any physical media info is collected is exactly the same one you quote, you can’t predict what an end-user would insert!


#18

[quote]is this a 4.3 framework change
[/quote]

No Brett, I think 4.2 still same.


#19

@ Brett - I always wondered: Does SDCard represent the media that is being inserted or the SD card hardware?


#20

@ jasdev - I believe it is the media inserted.