USB Host not working on custom G120 based mainboard

I am using the following code to test the USB Host on a custom G120 based board. The MassStorageConnected event is never fired. I am using the latest SDK. The relevant part of the circuit is attached. Any help is appreciated!


using GHI.Usb.Host;
using Microsoft.SPOT;
using Microsoft.SPOT.IO;
using System.IO;
using System.Text;
using System.Threading;

public class Program
{
    private static AutoResetEvent evt = new AutoResetEvent(false);

    public static void Main()
    {
        Controller.MassStorageConnected += Controller_MassStorageConnected;

        Controller.Start();

        Thread.Sleep(-1);
    }

    private static void Controller_MassStorageConnected(object sender, MassStorage massStorage)
    {
        RemovableMedia.Insert += RemovableMedia_Insert;

        massStorage.Mount();

        evt.WaitOne();

        using (var fs = new FileStream("\\USB\\Hello.txt", FileMode.OpenOrCreate))
            fs.Write(Encoding.UTF8.GetBytes("Hello, World!"), 0, 13);

        massStorage.Unmount();
    }

    private static void RemovableMedia_Insert(object sender, MediaEventArgs e)
    {
        Debug.Print("Inserted.");

        evt.Set();
    }

I tested this on a Cobra II mainboard and although the MassStorageConnected event was fired, the RemovableMedia.Insert was NOT fired! What am I doing wrong?

Not speaking about hardware here, I would say that you should not Write() and Unmount() in the MassStorageConnected event.

To me, this event should only contain a Mount() command. Then, when this command finishes, the RemovableMedia_Insert event should be fired.

Your code should look like this, I think :

using GHI.Usb.Host;
using Microsoft.SPOT;
using Microsoft.SPOT.IO;
using System.IO;
using System.Text;
using System.Threading;

public class Program
{
    private static AutoResetEvent evt = new AutoResetEvent(false);

    public static void Main()
    {
        Controller.MassStorageConnected += Controller_MassStorageConnected;
        Controller.Start();

        evt.WaitOne();
    using (var fs = new FileStream("\\USB\\Hello.txt", FileMode.OpenOrCreate))
            fs.Write(Encoding.UTF8.GetBytes("Hello, World!"), 0, 13);

        massStorage.Unmount();

        Thread.Sleep(-1);
    }

    private static void Controller_MassStorageConnected(object sender, MassStorage massStorage)
    {
        RemovableMedia.Insert += RemovableMedia_Insert;

        massStorage.Mount();
    }

    private static void RemovableMedia_Insert(object sender, MediaEventArgs e)
    {
        Debug.Print("Inserted.");

        evt.Set();
    }
1 Like

@ Bec a Fuel - Except the real issue is that the MassStorageConnected event is never being fired…

@ cyberh0me - MassStorageConnected is NOT fired on the custom board. MassStorageConnected is fired on the Cobra II board, but RemovableMedia.Insert is not fired.

The circuit is the same as the Cobra II/III, so I am not sure why it does not work.

Got it working!! Thanks @ Bec a Fuel! It was a combination of a loose wire and the event.Wait statement in the wrong place.