FileSystem.Mount() throws internal exception during mounting file system on a USB stick

Recently, we encountered an issue in production where multiple devices failed to mount the file system. The issue occurs during FileSystem.Mount()

The problem then magically seems to fix itself by simply inserting the USB stick into a PC and then inserting it into the device again.

Before inserting it into a pc:

Disk management details of the usb stick:

After inserting it into a pc:

using GHIElectronics.TinyCLR.Devices.UsbHost;
using System;
using Hardware.Platform.Mappings;
using GHIElectronics.TinyCLR.Devices.Storage;
using GHIElectronics.TinyCLR.IO;
using System.IO;

namespace Hardware.USB
{
    public class USBController : IUSBController
    {
        IDriveProvider _driver;
        StorageController _storageController;
        public DriveInfo[] _driveInfo;
        public USBController(StorageMapping controller)
        {
            _storageController = controller.Controller;
            var usbHostController = UsbHostController.GetDefault();

            usbHostController.OnConnectionChangedEvent +=
                UsbHostController_OnConnectionChangedEvent;

            usbHostController.Enable();
        }

        public IDriveProvider Driver
        {
            get { return _driver; }
        }

        public StorageController Controller
        {
            get { return _storageController; }
        }

        private void UsbHostController_OnConnectionChangedEvent
        (UsbHostController sender, DeviceConnectionEventArgs e)
        {
            switch (e.DeviceStatus)
            {
                case DeviceConnectionStatus.Connected:
                    switch (e.Type)
                    {
                        case BaseDevice.DeviceType.MassStorage:
                            try
                            {
                                _driver = FileSystem.Mount(_storageController.Hdc);
                                _driveInfo = DriveInfo.GetDrives();
                            }
                            catch (Exception ex)
                            {

                                Logger.Logger.Fatal("USBController | " + ex.ToString());
                            }
                            break;

                        default:
                            Logger.Logger.Debug("USBController | USB device is not storage!");
                            break;
                    }
                    break;

                case DeviceConnectionStatus.Disconnected:
                    Logger.Logger.Debug("USBController | Device Disconnected");
                    FileSystem.Unmount(_storageController.Hdc);
                    _driveInfo = DriveInfo.GetDrives();
                    //Unmount filesystem if it was mounted.
                    break;

                case DeviceConnectionStatus.Bad:
                    Logger.Logger.Debug("USBController | Bad Device");
                    break;
            }
        }
    }
}

Did you try with different USB sticks?

Each device has its own USB stick, and swapping it with the USB stick from my personal test device (which has the same part number as the other USB sticks) resolved the issue. The troubling part is that we had five different devices, each using a USB stick of the same brand, size, and model (part number), where the devices wouldn’t mount the filesystem. However, simply inserting these USB sticks into a PC made them work.

Of course, this wouldn’t be a major issue in production, as we can test and resolve it during the production stage. However, our concern is the reliability of the filesystem once the device is running at a customer.

If you would like, we can ship you a USB stick that causes the error.

Sounds like the stick has some error that windows can recover and fix and then it will work with your device. Maybe they were not formatted properly by the manufacture of the stick.

1 Like