Enable Jump drive mode

Hello, the following code is posted in the documentation for accessing an SDCARD attached to the microcontroller as a jump drive on the PC. Good stuff, it works as described.

First observation I have made, if the SDCARD was mounted by the microcontroller (G120) when this code runs it will not be mounted in the Windows PC (please insert drive error will appear on the windows PC). I’m guessing this is to prevent any attempt for simultaneous access to the SD card by the microcontroller and windows? if I unmount the SDCARD before attaching to the logical unit, everybody is happy.


using System;
using System.Threading;
using GHI.Usb.Client;
using GHI.Usb;
using GHI.IO.Storage;


public class Program
{
     public static void Main()
     {
         // Start MS
         MassStorage ms = new MassStorage();
         Controller.ActiveDevice = ms;

         // Assume SD card is connected
         SDCard sdCard;
         try
         {
             sdCard = new SDCard();
         }
         catch
         {
             throw new Exception("SD card not detected");
         }
         ms.AttachLogicalUnit(sdCard,0, " ", " ");

         // enable host access 
         ms.EnableLogicalUnit(0);

         Thread.Sleep(Timeout.Infinite);
     }
}

The second, and equally important, observation is the lack of examples for returning the SDCARD access to the microcontroller. My setup has a switch that the user will enable when requesting jump drive access to the SDCARD. When the user turns off the switch, what steps should I undertake to properly return SDCARD access to the microcontroller. The following code crashes the thread when the switch is turned off.


        MassStorage storage = null;


///port is the object referencing the physical switch
if (port.PortValue)
{
    lock (synchObj)
    {
        if (storage == null)
        {
            storageCard.Unmount();
            storage = new MassStorage();
            Controller.ActiveDevice = storage;
            storage.AttachLogicalUnit(storageCard, 0, "", "");
            storage.EnableLogicalUnit(0);
        }
    }
}
else
{
    ///After doing this, the SDCARD is no longer usable by anyone...
    lock (synchObj)
    {
        if (storage != null)
        {
            storage.DisableLogicalUnit(0);
            Controller.ActiveDevice = null;
            storage = null;
            storageCard.Mount();
        }
    }
}

A deeper search of the forums seemed to provide an answer to my dilemma. Setting the Controller.ActiveDevice to null crashes the controller in the 2014 R5 SDK. https://www.ghielectronics.com/community/forum/topic?id=17235&page=1 The following code seems to work just fine and accomplishes the desired outcome.


lock (synchObj)
{
  if (port.PortValue)
  {
      if (storage == null)
      {
         storageCard.Unmount();
         storage = new MassStorage();
         Controller.ActiveDevice = storage;
         storage.AttachLogicalUnit(storageCard, 0, "", "");
         storage.EnableLogicalUnit(0);
       }
   }
   else
   {
      if (storage != null)
      {
         storage.DisableLogicalUnit(0);
         Controller.ActiveDevice = new MassStorage();
         storage = null;
         storageCard.Mount();
       }
    }
}