USB Mass Storage

I am using the example located here:
https://www.ghielectronics.com/docs/36/usb-host

The debugging messages are all displayed the first time I install the application. After time first installation when debugging I don’t get any debugging messages. If I install another application and then reinstall the example above I get the debugging messages but only the first time I start the application. I am using the FEZ Panda III.

Thanks for any help in advance

The debugger is automatically attached after deployment. If you didn’t deploy you application but want to debug, you have to attach the debugger manually. Just click in the “Debug”-menu on “Attach to process…” in Visual-Studio and select you device. Debugging is active, when Visual-Studios status-bar is red, if it is blue then you are not in debugging-mode.

If I understood your problem right :D…

Thanks for the reply good info. but I still don’t see the debug information.

I am using USB to debug and I switched to USB?

The link above is the exact code I used I started a new project and posted the code verbatim.

I can repro this behaviour when you attempt to connect a USB storage device to the Panda3. If you don’t do that, deploy/redeploy works fine.

Deploy the app below. Once you get the counting up started in the debug output of VS, hit your Ctrl-Shift-F5 or Restart Debugging option. It will redeploy successfully and reattach to the debugger.

If you repeat the same test, and connect a USB storage stick to the USB Host port, and then attempt to restart debugging, the deployment appears to work (“deployment succeeded” message) but then eventually the debugger never reconnects after it reboots the CLR.

The debugging output also shows that the autoresetevent/WaitOne is never passed, and I suspect that’s somehow related (but don’t know how/why). I have no freakin idea about WaitOne so can’t comment further, but that seems to be the hiccup that causes the repeatable behaviour.

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()
    {
        Debug.Print("App starting");
        Thread.Sleep(10000);
        Debug.Print("after startup nap");

        Controller.MassStorageConnected += Controller_MassStorageConnected;

        Controller.Start();

        Debug.Print("Controller started");

        int n=0;

        while (true)
        {
            Thread.Sleep(1000);
            Debug.Print(n.ToString());
            n++;
        }
    }

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

        massStorage.Mount();
        Debug.Print("B4 waitone");
        evt.WaitOne();
        Debug.Print("after 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();
    }
}
1 Like

Brett,

Thanks for looking into this issue!!!

Brett,

I have tried just above everything to get the USB mass storage device to work and have not been successful. I just read the this posting https://www.ghielectronics.com/community/forum/topic?id=23883. It sounds like the USB Host Mass Storage has a bug in the new firmware.

Not relate to that one. You may need hard reset to reset the USB core. This is different thing.

The thread not returning is what I thought could be related?

I’m not smart enough to know what the WaitOne is meant to do (Dat?). I just wanted to see if the same code was working on my device (that might then point to your USB storage stick or something else specific in your environment) but I could repro it on G80 and G120.

I have the USB mass storage code working! The debugging issue is not solved. The code

is downloaded and the USB mass storage device is NOT mounted. However when the

device is reset it seems to always mount. To debug use the Debug | Attach to Process

method discussed above. The issue was the files were not always being saved adding this

statement solved the issues.

VolumeInfo.GetVolumes()[0].FlushAll()

For more information see the replay by StephW in this post.

https://www.ghielectronics.com/community/forum/topic?id=13546&page=1#msg138957

I have several applications written in VB that I would like to use a USB mass storage

device. Unfortunately VB is missing Microsoft.Spot.Io.RemovableMedia.Insert

needed to use USB mass storage. The code below uses a class library named USBExt.

The class library can be compiled to USBExt.dll that can be used in VB. The class library

and example files are below.

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

/// <summary>
/// Start a class libray and insert this code then build the dll to use in vb project
/// VB is missing Microsoft.Spot.Io.RemovableMedia.Insert needed to use USB mass storage
/// </summary>

namespace USBExt
{
    public class MS
    {

        // Boolean varable to test for mounted mass storage device.
         
        public static Boolean Mounted = false;

        private static AutoResetEvent evt = new AutoResetEvent(false);

        // Rem remove if not using PandaIII

        public static OutputPort LEDRemoveable = new OutputPort(GHI.Pins.FEZPandaIII.Gpio.Led1, false);
        public static OutputPort LEDBeforeMount = new OutputPort(GHI.Pins.FEZPandaIII.Gpio.Led2, false);
        public static OutputPort LEDAfterMount = new OutputPort(GHI.Pins.FEZPandaIII.Gpio.Led3, false);
        public static OutputPort LEDInsert = new OutputPort(GHI.Pins.FEZPandaIII.Gpio.Led4, false);

        /// <summary>
        /// Mount USB Mass Storage
        /// </summary>
        public static void Mount()
        {

            Controller.MassStorageConnected += Controller_MassStorageConnected;

            Controller.Start();

            Debug.Print("Controller started");

        }



        /// <summary>
        /// Connected to Mass Storage
        /// </summary>
        private static void Controller_MassStorageConnected(object sender, MassStorage massStorage)
        {

            LEDRemoveable.Write(true);

            RemovableMedia.Insert += RemovableMedia_Insert;

            LEDBeforeMount.Write(true);

            massStorage.Mount();

            LEDAfterMount.Write(true);

            Mounted = true;

            evt.Set();
            
        }

        /// <summary>
        /// Media Inserted
        /// </summary>
        private static void RemovableMedia_Insert(object sender, MediaEventArgs e)
        {
            LEDInsert.Write(true);
            
            evt.Set();
        }
    }
}


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


public static class program
{


    public static void Main()
    {
       
        Debug.Print("Connecting");

        // The USB mass storage device will not be mounted in the debug mode
        // When the application is reset the USB storeage device will be mounted and the files created
        // To see debug statements use the Debug|Attach to Process see this post for more information
        // https://www.ghielectronics.com/community/forum/topic?id=23879

        USBExt.MS.Mount();
               
        //Should mount much faster this could be changed

        Thread.Sleep(3000);

        Debug.Print("Mounted= " + USBExt.MS.Mounted.ToString());
        
        if (USBExt.MS.Mounted)
        {

           
            try
            {
                int i;
                for (i = 1; i <= 5; i++)
                {

                    string min = DateTime.Now.Minute.ToString();

                    string sec = DateTime.Now.Second.ToString();

                    string st = min + sec;

                    int ln = st.Length;


                    using (StreamWriter sw = new StreamWriter("\\USB\\" + st + ".txt"))
                    {

                        sw.WriteLine(st);
                        sw.Close();
                        sw.Dispose();

                    }
                    Thread.Sleep(2000);
                }
                
                //*************The VolumeInfo... is what makes it work!!!!!
                //See the reply by StephW
                //https://www.ghielectronics.com/community/forum/topic?id=13546&page=1#msg138957
                
                VolumeInfo.GetVolumes()[0].FlushAll();


                Thread.Sleep(1000);

                Debug.Print("Getting files");

                string[] dir = Directory.GetFiles("\\USB");

                for (i = 0; i <= dir.Length - 1; i++)
                {
                   
                    Debug.Print(dir[i].ToString());

                }
               
                              
                //Turn off the leds
                
                USBExt.MS.LEDAfterMount.Write(false);
                USBExt.MS.LEDBeforeMount.Write(false);
                USBExt.MS.LEDRemoveable.Write(false);
                USBExt.MS.LEDInsert.Write(false);

            }


            catch (Exception ex)
            {
                Debug.Print("File error: " + ex.Message);

            }
        }
    }
}
    


Option Explicit On
Option Strict On
Imports System
Imports System.Threading
Imports System.IO
Imports Microsoft.SPOT
Imports System.Text
Imports Microsoft.SPOT.IO
Imports GHI.Usb
Imports GHI.Usb.Host
Imports GHI.IO.Storage
Imports Microsoft.VisualBasic

'*****This is the class library created in C#
Imports USBExt

Namespace MassStorageTest

    Public Module Module1
       

        Sub Main()
            

            Debug.Print("Connecting")

            MS.Mount()

            Thread.Sleep(3000)

            Debug.Print("Mounted= " & USBExt.MS.Mounted.ToString)

            If MS.Mounted Then

                Dim dir() As String = Directory.GetFiles("\USB")

                Debug.Print(dir.Length.ToString)

                For i = 0 To dir.Length - 1
                    Debug.Print(dir(i).ToString)

                Next


                Dim min As String = String.Empty
                Dim sec As String = String.Empty
                Dim st As String = String.Empty
                Dim ln As Integer = 0

                Try


                    For i = 1 To 5
                        min = DateTime.Now.Minute.ToString

                        Thread.Sleep(100)

                        sec = DateTime.Now.Second.ToString

                        Thread.Sleep(100)

                        st = min & sec

                        ln = st.Length

                        Using fs = New FileStream("\USB\" & st & ".txt", FileMode.OpenOrCreate)
                            fs.Write(Encoding.UTF8.GetBytes(st), 0, ln)
                            fs.Close()
                        End Using

                        Debug.Print("Wrote: " & st & ".txt")

                        Thread.Sleep(2000)

                    Next

                    VolumeInfo.GetVolumes()(0).FlushAll()

                Catch ex As Exception
                    Debug.Print(ex.ToString)
                End Try

            End If


        End Sub
        
    End Module

End Namespace