Domino - USB thumb drive and SD card - two persistent storages

Hi,

I am using a Domino to collect data which I write to the SD card. This works fine.

What I want to do is to be able to copy the contents of my SD card to the USB thumb drive when I plug it in. I have used the examples in the Beginners guide but have the following problems:

The SD card remains in the device. I do not want to remove it. When the device boots it is found by the RemovableMedia.Insert event. I print out the files (to debug) and all is OK.

When I plug the USB drive in the USBHostController.DeviceConnectedEvent event runs. Note that the RemovableMedia.Insert event does NOT run in this case.

In the USBHostController.DeviceConnectedEvent I instantiate a new PersistentStorage(device), which is declared as a static global, and mount the file system. What I am stuck on is how to associate the USB drive to a MediaEventArgs instance if the RemovableMedia.Insert event does not run. It appears that I need this to be able to access the files. I am confused as to the relationship between the PersistentStorage instances and access to the file systems.

If both devices are plugged in at boot time then both are discovered in RemovableMedia.Insert events and both file systems are read properly.

I am not sure what you mean. You should always get two insert events. One for SD and one for USB.

Didn’t I read that only one PersistentStorage could be mounted at any one time?

-Eric

That’s incorrect. We support multiple USB drives and SD card.

I have got to check my facts before I chime in … I have been reading everything pertaining to .NETMF and the various devices that I can get my hands on and that must have been for something else.

Sorry about the confusion. (Just reread the section and you are right no such limitation exists.)

-Eric

When I insert the SD card the RemovableMedia.Insert event fires.
When I insert the USB drive the USBHostController.DeviceConnectedEvent event fires.

How do I access the USB file system? I find the documentation confusing on this point. Can anyone provide an example that is different to 23.2 in the Beginners Guide?

To be clear: I do not get a RemovableMedia.Insert event when the USB drive is inserted. I can see how to access the file system if I get this event but not if, as is the case, I do not.

Can anybody tell me how to access the USB file system on an inserted thumb drive?

When I insert the USB drive I get the USBHostController.DeviceConnectedEvent event but there is never a RemovableMedia.Insert event.

The example in the Beginners Guide does not explain how to access the USB drive without the Insert event.

The Insert event provides the MediaEventArgs e parameter which allows access to the file system. But I never get this.

Has no one ever had this problem?

I am still not sure what you mean…
The example in the beginner book, that you referred to, already uses the insert event! Here’s a copy paste:


using System;
using System.IO;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.IO;
using GHIElectronics.NETMF.IO;
using GHIElectronics.NETMF.USBHost;
namespace Test
{
class Program
{
// Hold a static reference in case the GC kicks in and disposes it
// automatically, note that we only support one in this example!
static PersistentStorage ps;
public static void Main()
{
// Subscribe to RemovableMedia events
RemovableMedia.Insert += RemovableMedia_Insert;
RemovableMedia.Eject += RemovableMedia_Eject;
// Subscribe to USB events
USBHostController.DeviceConnectedEvent += DeviceConnectedEvent;
// Sleep forever
Thread.Sleep(Timeout.Infinite);
}
static void DeviceConnectedEvent(USBH_Device device)
{
if (device.TYPE == USBH_DeviceType.MassStorage)
{
Debug.Print("USB Mass Storage detected...");
ps = new PersistentStorage(device);
ps.MountFileSystem();
}
}
static void RemovableMedia_Insert(object sender, MediaEventArgs e)
{
Debug.Print("Storage \"" + e.Volume.RootDirectory +
"\" is inserted.");
Debug.Print("Getting files and folders:");
if (e.Volume.IsFormatted)
{
string[] files = Directory.GetFiles(e.Volume.RootDirectory);
string[] folders =
Directory.GetDirectories(e.Volume.RootDirectory);
Debug.Print("Files available on " +
e.Volume.RootDirectory + ":");
for (int i = 0; i < files.Length; i++)
Debug.Print(files[i]);
Debug.Print("Folders available on " +
e.Volume.RootDirectory + ":");
for (int i = 0; i < folders.Length; i++)
Debug.Print(folders[i]);
}
else
{
Debug.Print("Storage is not formatted. Format on PC with
FAT32/FAT16 first.");
}
}
static void RemovableMedia_Eject(object sender, MediaEventArgs e)
{
Debug.Print("Storage \"" + e.Volume.RootDirectory +
"\" is ejected.");
}
}
}

So where is the problem? The insert event is called the as it would be called with an SD card.

To repeat, that is my problem. The Insert event is NOT called. It is NOT called.

If it was called I could handle the event. But it is not called. The INSERT event does not fire so I cannot use it.

I understand what SHOULD happen but that is not what I see. The Insert event fires for the SD card but not for the USB drive.

So you cannot access thumb drive files at all? Then there is some problem.
Please copy the example from the book as is and run it. Try different thumb drivers. Any of them work? Does it throw any exceptions? Step in the code, you should always receive an Insert event AFTER ps.MountFileSystem. Note that you will receive the event some time after the Mount call.

It’s interesting… not sure exactly what goes on for me, this is the first time I’ve tried adding a USB thumb drive to my Domino

I’ve just updated with the latest firmware and that’s ok.

I created a project with the code posted here by Gus. Breakpointed in the deviceconnected, insert and remove handlers.

I deploy to Domino.

I insert USB. Windows gives me the disconnect/reconnect noises, since it looks like Domino resets, and debugger obviously doesn’t show it hitting the breakpoints.

I added a timer and led flashing, and when i insert the storage I can see the LED fade to that “restarting” state, and then start flashing again as the code inits.

If I attach to the debugger with mfdeploy and force it to restart the CLR, I can see it doing the debug.prints as it goes thru the handlers, so they are firing but the insert is causing a restart.

I am only powered via USB cable at the moment. It seems like it could be a lack of power browning out the Fez, but with nothing else connected (even uSD removed) it resets with multiple USB drives, none make a difference.

Am I doing something wrong, or is it power? And perhaps is that your issue Peter?

Peter, check your power supply :slight_smile:

I plugged in a regulated 12v supply and things work without browning out and resetting. I now see the debug messages without problem.

Storage "\USB" is ejected.
USB Mass Storage detected...
Storage "\USB" is inserted.
Getting files and folders:
Files available on \USB:
\USB\Archive4.zip
\USB\NRT.TXT
Folders available on \USB:

My power supply is OK. I do not have any problems with resetting.

I tried a different USB drive and it operates perfectly every time. The original USB drive worked occasionally, but usually not. The second USB drive works fine. That must have been the cause of the problem and I should have tried a different drive sooner.

The faulty drive works fine otherwise, in the PC. In the FEZ the faulty drive would register a USBHostController.DeviceConnectedEvent but not the RemovableMedia.Insert. I also noted that when it was connected to the FEZ the light on the drive would flash continuously.

I now notice that the SD card does not fire the RemovableMedia.Insert event. Fortunately, I can check for its’ presence and access the files independently of the event.

Thanks all. I now have the tools I need to copy files from my SD card to a USB drive so I don’t need to pop out the SD card.

Cheers!

If you plug something into fez which causes it to reset then you need power supply or good powered USB hub