Probelm with PersistentStorage() function?

I’ve been trying to get either an SD-based of USB-based pseudo-flashdrive to work on a FEZ Spider (4.2 libraries).

I’ve got the following HW configuration:

FEZ SPider
USBClientDP (wired to socket 1)
USBHost (wired to socket 3)
RS232 (wired to socket 11)

The USBHost and Client are connected in the IDE to the FEZ Spider. THe RS232 is not, as it’s just for debugging.

The code :

using System;
using System.IO;
using System.Collections;
using System.Threading;

using Microsoft.SPOT;
using Microsoft.SPOT.IO;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Touch;

using Gadgeteer.Networking;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using Gadgeteer.Modules.GHIElectronics;
using GHI.Premium.USBHost;
using GHI.Premium.IO;
using GHI.Premium.System;

namespace GadgeteerApp1
{
    public partial class Program
    {
        static PersistentStorage ps;

        // 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();
            *******************************************************************************************/
            RemovableMedia.Insert += RemovableMedia_Insert;
            RemovableMedia.Eject += RemovableMedia_Eject;

            // Subscribe to USB events
            USBHostController.DeviceConnectedEvent += DeviceConnectedEvent;

            // Use Debug.Print to show messages in Visual Studio's "Output" window during debugging.
            Debug.Print("Program Started");
        }

        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.");
        }
    }
}

the program starts fine, but when I plug in the USB flash drive to the USBHost Module, an exception is thrown:

Program Started
USB Mass Storage detected…
#### Exception System.InvalidOperationException - CLR_E_INVALID_OPERATION (4) ####
#### Message:
#### GHI.Premium.IO.PersistentStorage::PersistentStorage_Helper [IP: 0000] ####
#### GHI.Premium.IO.PersistentStorage::.ctor [IP: 0011] ####
#### GadgeteerApp1.Program::DeviceConnectedEvent [IP: 001b] ####
#### GHI.Premium.USBHost.USBH_DeviceConnectionEventHandler::Invoke [IP: 8017e487] ####
#### GHI.Premium.USBHost.USBHostController::nativeEventDispatcher_OnInterrupt [IP: 0037] ####
#### GHI.Premium.System.InternalEvent::nativeEventDispatcher_OnInterrupt [IP: 0054] ####
A first chance exception of type ‘System.InvalidOperationException’ occurred in GHI.Premium.IO.dll
An unhandled exception of type ‘System.InvalidOperationException’ occurred in GHI.Premium.IO.dll

This is actually the same behavior that PersistentStorage gives with SD cards (using, of course, those examples).

I’m beginning to believe that something’s wrong in the Premium.IO library - but don’t know how to debug it. Any thoughts/help?

Also, if this belongs in another forum, please let me know…)

I think you might be mixing gadgeteer and non-gadgeteer APIs.

the first thing I would do is check the gadgeteer source code to see what is being done with the USB host module. there might be some redundant operations in your code.

you can chech this by removing the USB host module from designer and then run your code.

@ Mike - Hi Mike, I commented out the references to Gadgeteer (all four of them) and rebuilt the program with the same results.

I then disconnected the USBHost module in the designer and re-ran the program. I didn’t see any automatic changes to the program.cs file that was generated previously. (all the “using” statements remained. The code built and was deployed. Interestingly (to me), that ran successfully.

So, it appears that “wiring up” the USBHost in the IDE is what caused the problem. Can you explain what you think happened in more detail? I’d love a better understanding of this so I don’t do it again (or in other contexts)

Gadgeteer is a layer on top of the MF APIs. For many of the modules it performs additional steps that you would normally do. If you put the USB Host module back into the designed, select it, and hit F1 you will get the associated documentation. I think there is an event(s) for mass storage mounted.

I recommended that you get the Gadgeteer source code, from gadgeteer.codeplex.com. To fully understand Gadgeteer you need to read the source code to understand what is being done for you.

@ Mike - Thanks for the help with this.

If I was using USB host, I would not use the gadgeteer drivers. Those are there to help beginners but serious users should just use it directly. This means, do not add the USB host in the designer but add it physically and use GHI premium libs directly.

@ Gus - I’ve switched away from that. :slight_smile: You can see the current code/issues at http://www.tinyclr.com/forum/topic?id=10531. Please look - it’s a much shorter code snippet and much better explained.