USB file system mount fails w/InvalidOperationException

FileSystem.Mount(storageController.Hdc) is failing for me with the following:

An unhandled exception of type 'System.InvalidOperationException' occurred in GHIElectronics.TinyCLR.IO.dll occurred

GHIElectronics.TinyCLR.IO.FileSystem::Initialize
GHIElectronics.TinyCLR.IO.FileSystem::Mount
Ecu.ECU::UsbHostController_OnCon

I’m getting the USB connection events correctly, type MassStorage, etc. I’ve verified it is formatted FAT32. Are there any other requirements that I’m missing?

A piece of code would be welcome :wink:

1 Like

I’ve tried a few variants but this is my current (based on the example in the USB handling doc):

private void StartUsbHost()
    {
        var usbHostController = UsbHostController.GetDefault();

        usbHostController.OnConnectionChangedEvent += UsbHostController_OnConnectionChangedEvent;

        usbHostController.Enable();

        System.Threading.Thread.Sleep(-1);
    }

    private void UsbHostController_OnConnectionChangedEvent(UsbHostController sender, DeviceConnectionEventArgs e)
    {
        p.WriteLine($"USB e.Id            : {e.Id}");
        p.WriteLine($"USB e.InterfaceIndex: {e.InterfaceIndex }");
        p.WriteLine($"USB e.PortNumber    : {e.PortNumber}");
        p.WriteLine($"USB e.Type          : {e.Type.ToString()}");
        p.WriteLine($"USB e.VendorId      : {e.VendorId}");
        p.WriteLine($"USB e.ProductId     : {e.ProductId}");

        switch (e.DeviceStatus)
        {
            case DeviceConnectionStatus.Connected:
                switch (e.Type)
                {
                    case BaseDevice.DeviceType.MassStorage:

                        var storageController = StorageController.FromName(SC20260.StorageController.UsbHostMassStorage);
                        var driveProvider = FileSystem.Mount(storageController.Hdc);  **<-- Exception thrown here**
                        var driveInfo = new DriveInfo(driveProvider.Name);

                        p.WriteLine("Free: " + driveInfo.TotalFreeSpace);
                        p.WriteLine("TotalSize: " + driveInfo.TotalSize);
                        p.WriteLine("VolumeLabel:" + driveInfo.VolumeLabel);
                        p.WriteLine("RootDirectory: " + driveInfo.RootDirectory);
                        p.WriteLine("DriveFormat: " + driveInfo.DriveFormat);

                        CheckForAndUpdateFirmwareIfAvailable(driveProvider);
                        break;
                }
                break;

            case DeviceConnectionStatus.Disconnected:
                p.WriteLine("Device Disconnected");
                //Unmount filesystem if it was mounted.
                break;
        }
    }

Tried another USB drive, also FAT32, and it works… :face_with_raised_eyebrow:

Send the one that does not work to GHI…

1 Like

Found the issue. GPT partition table doesn’t work. The USB drive must be configured as MBR. Should probably mention this in the USB storage page just as an FYI

2 Likes