Hi,
Experimenting with cerberus (last firmware) and micro sd card module (http://www.ghielectronics.com/catalog/product/359) I’ve got around a few rough edges. Mainly :
- never try to mount an already mounted card (causes boot lock up)
- when attempting to reinsert an sdcard after ejecting it first, the first access to the sdcard will eventually causes a lock up.
- you can workaround the previous issue, by checking if it is the second time the card has been mounted and rebooting the board instead.
Working code :
using System;
using System.Collections;
using System.Threading;
using Microsoft.SPOT;
using Gadgeteer.Networking;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using Gadgeteer.Modules.GHIElectronics;
using Microsoft.SPOT.IO;
using System.IO;
namespace TestSD
{
public partial class Program
{
static private string root;
static private GT.Timer writeTimer;
static private GT.Timer BlinkTimer;
static private bool MountedOnce = false;
// This method is run when the mainboard is powered up or reset.
void ProgramStarted()
{
/*******************************************************************************************
Modules added in the Program.gadgeteer designer view are used by typing
their name followed by a period, e.g. button. or camera.
Many modules generate useful events. Type +=<tab><tab> to add a handler to an event, e.g.:
button.ButtonPressed +=<tab><tab>
If you want to do something periodically, use a GT.Timer and handle its Tick event, e.g.:
GT.Timer timer = new GT.Timer(1000); // every second (1000ms)
timer.Tick +=<tab><tab>
timer.Start();
*******************************************************************************************/
BlinkTimer = new GT.Timer(1000);
BlinkTimer.Tick += new GT.Timer.TickEventHandler(BlinkTimer_Tick);
BlinkTimer.Start();
writeTimer = new GT.Timer(5000);
writeTimer.Tick += new GT.Timer.TickEventHandler(writeTimer_Tick);
sdCard.SDCardMounted += new SDCard.SDCardMountedEventHandler(sdCard_SDCardMounted);
sdCard.SDCardUnmounted += new SDCard.SDCardUnmountedEventHandler(sdCard_SDCardUnmounted);
button.ButtonPressed += new Button.ButtonEventHandler(button_ButtonPressed);
// Use Debug.Print to show messages in Visual Studio's "Output" window during debugging.
Debug.Print("Program Started");
}
static private bool toggleled = true;
void BlinkTimer_Tick(GT.Timer timer)
{
if (sdCard.IsCardMounted)
{
Mainboard.SetDebugLED(toggleled);
toggleled = !toggleled;
}
else
{
Mainboard.SetDebugLED(false);
}
}
void button_ButtonPressed(Button sender, Button.ButtonState state)
{
if (sdCard.IsCardMounted)
{
sdCard.UnmountSDCard();
}
}
void sdCard_SDCardUnmounted(SDCard sender)
{
writeTimer.Stop();
}
void sdCard_SDCardMounted(SDCard sender, GT.StorageDevice SDCard)
{
if (MountedOnce)
{
Thread.Sleep(100);
Microsoft.SPOT.Hardware.PowerState.RebootDevice(true);
}
else
{
root = SDCard.RootDirectory;
writeTimer.Start();
MountedOnce = true;
}
}
void writeTimer_Tick(GT.Timer timer)
{
if (sdCard.IsCardMounted)
{
string fileName = Path.Combine(root, "file.txt");
Stream stream;
if (File.Exists(fileName))
{
stream = File.OpenWrite(fileName);
stream.Position = stream.Length;
}
else
{
stream = File.Create(fileName);
}
using (var writer = new StreamWriter(stream))
{
Debug.Print("Writing ok");
writer.WriteLine(Guid.NewGuid().ToString());
}
stream.Dispose();
}
else timer.Stop();
}
}
}
Is anybody experiencing the same thing, did I forgot something obvious and/or arcane ?
Regards
