EMX – SD card reader June 2, 2010
Posted by wesaday in Programming.Tags: EMX, GHIElectronics
trackback
This is the first real use that I came up with for the EMX board and it would appear that many people have questions regarding the SD card reader capability of the EMX. An SD card would be a quick and easy way to increase the available memory for this primarily embedded system. You might need to save or read text files or log your application to help debug it, updating your application in the field, or other uses.
Create a new project in Visual Studio 2008. Use the Visual C#->Micro Framework->Window Application project template, give the project a meaningful name and click OK.
One thing that I do not like about the template is that the Program.cs file contains the Main method and the window definition. I prefer to separate the functionality but if you want to leave it alone feel free. The only thing that you really need in your Main method is:
Program myApplication = new Program();// Start the application
myApplication.Run(new MainWindow());
I delete the rest of the stuff. Now to the MainWindow. I add a new class called MainWindow to the project. The MainWindow must inherit from Window. Add these using statements:
using System;
using GHIElectronics.NETMF.IO;
using Microsoft.SPOT.Hardware;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Media;
using System.IO;
using Microsoft.SPOT;
using Microsoft.SPOT.IO;private PersistentStorage _sdcard;
private InterruptPort _sdDetect = new InterruptPort(GHIElectronics.NETMF.Hardware.EMX.Pin.IO36, true, Port.ResistorMode.PullDown, Port.InterruptMode.InterruptEdgeBoth);
You will need to run a jumper wire from SD Detect to whatever IO line you want to use. Be very careful!Width = SystemMetrics.ScreenWidth;
Height = SystemMetrics.ScreenHeight;
Background = new SolidColorBrush(0x000000);
_sdDetect.OnInterrupt += OnSdInterrupt;
private void OnSdInterrupt(uint data1, uint data2, DateTime time)
{
if (data2 == 0)
{
_sdcard = new PersistentStorage("SD");
_sdcard.MountFileSystem();
}
else
{
if (_sdcard != null)
{
_sdcard.UnmountFileSystem();
}
}
}
private void GetFiles()
{
if (!VolumeInfo.GetVolumes()[0].IsFormatted)
{
return;
}
string[] files = Directory.GetFiles(VolumeInfo.GetVolumes()[0].RootDirectory);
foreach (string s in files)
{
Debug.Print(s);
}
}