USBHost fails

Testing USBHost. (SC20260 Tiny 2.0 preview 5)

When my application starts, it reads files from the SD card. If I have a USB mass storage device in USB Host, the SD file read fails. Also, I dont see the “DeviceConnectionStatus.Disconnected” event when I remove the USB device, and the system locks up.

Can you be a bit more specific on steps steps and needed and what code to try?

boot/Startup sequence: (USB Device already in connector)

  1. various initializations
  2. initialize and mount SD card
  3. initialize USBHost, sets onconnectionchangedevent (using the sample code)
  4. receive connection event and mounts usb device
  5. read files from SD card -> fails.

If the USB device is not in connector, then SD reads OK.

IF the application is running and I have connected the USB device, I do not recieve the disconnect event when removing the device and the system locks

How did your determine what drive goes with what letter? Are you sure that part of the code is correct?

on sd card drive.name -> “a:\”

on usb drive.name -> “b:\”

I think the first drive registered is A so that might he your problem. You should not hardcode the driver name.

When accessing the files I use:

                file = new FileStream($@"{drive.Name}{filename}", fm, FileAccess.ReadWrite);

note that “drive” is a different object for sd and usb

We will check, thanks

any news on this issue ?

Fixed already SD and USB likely don't work if both mounted · Issue #558 · ghi-electronics/TinyCLR-Libraries · GitHub

Release comes this week

Hi, we corrected file system when both SD and USB are mounted. But we couldn’t reproduce disconnect even issue.

Can we know what kind of usb thumb drive you are using?

Thanks

Hi, we tried a simple code below to reproduce USB host disconnect event (128GB). it works well to us, keep repeating remove, inserted… 11 times (0->10) as picture below.

But because it is simple project so the bug may not happened yet. If you can, please read the code below and do some change in your project, and share to us what you find out.

We know the bug is there, but not sure how to reproduce yet.

Thanks.

const string FSSD_API_CONTROLLER = @"GHIElectronics.TinyCLR.NativeApis.STM32H7.SdCardStorageController\0";
        const string FSUSB_API_CONTROLLER = @"GHIElectronics.TinyCLR.NativeApis.STM32H7.UsbHostMassStorageStorageController\0";

#if DEVICE_SC20260
        const int LED1 = SC20260.GpioPin.PB0; // PI0
        const int LDR1 = SC20260.GpioPin.PB7; // PC3;
#endif

#if DEVICE_SC20260E
        const int LED1 = SC20260.GpioPin.PA3; // PI0
        const int LDR1 = SC20260.GpioPin.PB7; // PC3;
#endif

#if DEVICE_FEZ_DUINO
        const int LED1 = SC20260.GpioPin.PB0; // PI0
        const int LDR1 = SC20260.GpioPin.PB7; // PC3;
#endif

#if FEZ_COBRA
        const int LED1 = SC20260.GpioPin.PA0; // PI0
        const int LDR1 = SC20260.GpioPin.PB7; // PC3;
#endif


#if ST100_DEV
        const int LED1 = 1 * 16 + 0; // PB0
        const int LDR1 = 1 * 16 + 7; // PB7;
#endif
        static GpioPin led1;
        static GpioPin ldr1;

        static void Main()
        {
            var controller = GpioController.GetDefault();

            led1 = controller.OpenPin(LED1);
            ldr1 = controller.OpenPin(LDR1);

            led1.SetDriveMode(GpioPinDriveMode.Output);
            ldr1.SetDriveMode(GpioPinDriveMode.InputPullUp);

            var cnt = 0;
            while (ldr1.Read() == GpioPinValue.High)
            {
                led1.Write(led1.Read() == GpioPinValue.Low ? GpioPinValue.High : GpioPinValue.Low);
                if (cnt % 10 == 0)
                    System.Diagnostics.Debug.WriteLine("Waiting for pressing LDR1: " + (cnt / 10) + " seconds.");
                Thread.Sleep(100);
                cnt++;
            }

            while (ldr1.Read() == GpioPinValue.Low) ;

            storageController = GHIElectronics.TinyCLR.Devices.Storage.StorageController.FromName(FSUSB_API_CONTROLLER);

            var usnhostController = UsbHostController.GetDefault();

            usnhostController.OnConnectionChangedEvent += UsnhostController_OnConnectionChangedEvent;

            usnhostController.Enable();

            //DoTestSpeed();

            Thread.Sleep(-1);
        }

        static bool usbConnected = false;
        static StorageController storageController;
        static IDriveProvider drive;
        static bool usbMounted = false;

        static int countInserted = 0;
        static int countRemoved = 0;

        private static void UsnhostController_OnConnectionChangedEvent(UsbHostController sender, DeviceConnectionEventArgs e)
        {
            Debug.WriteLine("id: " + e.Id);
            Debug.WriteLine("InterfaceIndex: " + e.InterfaceIndex);
            Debug.WriteLine("Type: " + ((object)e.Type).ToString());
            Debug.WriteLine("idProductId " + e.ProductId);
            Debug.WriteLine("VendorId: " + e.VendorId);
            Debug.WriteLine("PortNumber: " + e.PortNumber);

            if (e.Type == BaseDevice.DeviceType.MassStorage)
            {
                if (e.DeviceStatus == DeviceConnectionStatus.Disconnected)
                {
                    Debug.WriteLine("Massatorage disconnected " + countRemoved++);
                    usbConnected = false;
                }
                else if (e.DeviceStatus == DeviceConnectionStatus.Connected)
                {                    
                    Debug.WriteLine("Massatorage connected : " + countInserted++);
                    usbConnected = true;
                   

                }
                else if (e.DeviceStatus == DeviceConnectionStatus.Bad)
                {
                    Debug.WriteLine("Massatorage bad");
                    usbConnected = false;
                }

                if (usbConnected && !usbMounted)
                {
                    var usbThumdrive = GHIElectronics.TinyCLR.IO.FileSystem.Mount(storageController.Hdc);

                    DriveInfo driveInfo = new DriveInfo(usbThumdrive.Name);


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

                    usbMounted = true;
                }
                else if (!usbConnected && usbMounted)
                {
                    GHIElectronics.TinyCLR.IO.FileSystem.Unmount(storageController.Hdc);

                    usbMounted = false;
                }
            }
        }

First device : Corsair Voyager GT

id: 604319376
InterfaceIndex: 0
Type: 6
idProductId 6665
VendorId: 6940
PortNumber: 0
Massatorage connected : 0
Free: 31623970816
TotalSize: 31624003584
VolumeLabel:CORSAIR
RootDirectory: A:
DriveFormat: FAT

Second device : Basic XL SD/SDHC/MMC card reader to USB

id: 604319376
InterfaceIndex: 0
Type: 6
idProductId 4639
VendorId: 5325
PortNumber: 0
Massatorage connected : 0
Free: 15567912960
TotalSize: 15657336832
VolumeLabel:16GB
RootDirectory: A:
DriveFormat: FAT

In both cases, I removed the drive and the board hung, until something (watchdog ?) gave control back to VS, which then says :

The debugger engine failed to receive any debug events from the debugging target

A hard reset is needed to deploy again.

No “Massatorage :slight_smile: disconnected” message.

Please try with power source or powered hub.

I see it in SC20100 Dev, but not on 260. Not sure why lost disconnect event on this board. Thanks.

2 Likes

I just tried with external power source, with different devices (keyboards, Quail :wink: ) but get the same bad results.

From your last message, it seems related to the board. I knew I was lucky…
Please share your findings, I will put our board on hold until it’s solved, in case there are some hardware changes needed.

Also… once the device is disconnected, the board hangs and is not seen anymore in VS or TinyCLR config.
Perhaps a hard-fault in the firmware ?

If it can help.

This is in the works and will be fixed in the next release.

Thank you

Are you saying that this is “only” a firmware issue ? No hardware involved ?