Using the SD Card

Apr 09, 2014

The first thing to know about the SD card reader is that it is automatically mounted at startup.  If you try to use the SecretLabs.NETMF.IO.StorageDevice.MountSD() function you will get a System.NotSupportedException because you're trying to mount an already mounted storage device.

The next thing you need to know is that the 4.2 framework does not support SDHC cards, which means that you are limited to 2 gigabyte SD cards.

Next you need to know how to get to the files on the SD card.  In the .Net Micro Framework (NetMF) 4.2 the root of the currently inserted SD card is "\SD" you can check if the SD card is present with the following code:

if (Directory.Exists("\\SD")) {
    Debug.Print("SD Card ready");
}else{
    Debug.Print("No SD Card present");
}

 

The SD card looks like a normal directory and you use normal file operations to read and write data on the card.  The following is a simple example of creating a file (or opening it in append mode if it already exits), writing one line to the file, and then closing the file:

StreamWriter m_Writer;
m_Writer = new StreamWriter("\\SD\\LogFile.txt", true);
m_Writer.WriteLine("Output line");
m_Writer.Close();

 

You can create directories and files and use the SD card as if it's a disk drive.  It's great!  If you're using the file as a log, you should call the Flush() function each time you write to the file.  That forces the data to be written on the SD card immediately, instead of sitting in the buffer.

But, what happens when you eject the card?  If you're writing data to the SD card and NOT using the Flush() function your program continues to run with no problem until the output buffer fills up and it tries to write to the SD card (about 4K bytes).  When it tries to write the buffer you will get a System.IO.IOException.  If that happens to be the first time the buffer is written the file probably won't even be created.

If you are using the Flush() function after every write then you will get the IOException the first time that you call Flush() after ejecting the SD card.  The file will be created and it may or may not contain data (there seems to be addition buffering somewhere).

There are two events that help us manage this situation: the insert event and the eject event.  With the following code you can set up handlers for these events:

RemovableMedia.Insert  = new InsertEventHandler(SD_Insert);
RemovableMedia.Eject  = new EjectEventHandler(SD_Eject);

void SD_Insert(object sender, MediaEventArgs args)
{
    Debug.Print("SD card inserted");
}

void SD_Eject(object sender, MediaEventArgs args)
{
    Debug.Print("SD card ejected");
}

 

Note that if you're defining these in the Program class the event handler functions will need to be declared static.  Now you'll see the debug output as soon as you eject the SD card.  It's too late to flush the data but you can at least avoid an exception from further writes if you create a boolean variable to indicate if the SD card is present.  When the card is re-inserted the framework will automatically re-mount it.

Here the interfaces for mounting and unmounting, I haven't really found any use for them:

SecretLabs.NETMF.IO.StorageDevice.Unmount("\\SD");
SecretLabs.NETMF.IO.StorageDevice.MountSD(
    "\\SD", SPI.SPI_module.SPI1, Cpu.Pin.GPIO_NONE);

 

It would seem that a push button could be used as a soft eject.  When the button is pressed the StreamWriter could be flushed and closed.  Then the SD card could be removed.  When a card is inserted the insert event handler would re-open the file and logging could continue -- perhaps I will try this in a future project Wink.

 

 



Category: Tutorials

John Lyman

Please add your bio info through your member profile page, or through your dashboard.


Add Pingback

Please add a comment

You must be logged in to leave a reply. Login »