FEZ REAPER : Not detecting USB memory stick inserts after 4 ~ 5 times

Hi,

Yesterday got Spider-II and Reaper boards in hand. Lovely little babies :slight_smile:
Quickly put in test code w/ help from GHI support docs.

I am keen on testing the consistency of USB memory stick mount, read and write.
Have given below the test code (ran in both Spider-II and Reaper with minimal change).

Spider-II is working well with out issues (so far tested some 100 detect / mount / unmounts) without power down or reset.
But, Reaper does not detect any USB memory stick insert after 4 - 5 times ( of detect / mount / unmount cycles).

Is it because G120E has built-in USB hub ? I should thank GUS here, bcuz it was his suggestion to take along a Spider-II sample board.

Still, my project is very simple and has limited budget. So my preference is G80 (or Reaper).

So please indicate the problem or missing areas in the below code for G80 (Reaper).

Thanks.


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


namespace MF_Start
{
    public class Program
    {
        static OutputPort dLED = new OutputPort(GHI.Pins.G80.Gpio.PB2, false); //G80 aka Reaper
        //static OutputPort dLED = new OutputPort(GHI.Pins.G120E.Gpio.P1_31, false); //G120E aka SpiderII
        
        //private static AutoResetEvent evt = new AutoResetEvent(false);
        static MassStorage usbStick;
        static string rootDirectory;

        public static void Main()
        {
            Debug.Print("Starting MF_Start application");    

            //****************************************
            // BLINK DEBUG LED
            // Pins : SPIDERII = P1.31, REAPER = PB2
            //****************************************

            Debug.Print("Entering Debug LED Blinking segment");

            dLED.Write(true);
            Thread.Sleep(2000);
            dLED.Write(false);

            Debug.Print("Exiting Debug LED Blinking segment");

            //****************************************



            //****************************************
            // SERIAL WRITE & READ 
            // SPIDER = COM2, REAPER = COM3
            //****************************************

            Debug.Print("Entering serial port segment");

            SerialPort liftPort = new SerialPort("COM3", 115200, Parity.None, 8, StopBits.One);
            string txData = "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG\r\n";
            byte[] txBuf = Encoding.UTF8.GetBytes(txData);
            int rxCount;
            int rxReadBytes;
            byte[] rxBuf = new byte[128];
            char[] rxChar = new char[128];

            liftPort.Open();
            liftPort.Write(txBuf, 0, txData.Length);
            Thread.Sleep(10);

            rxCount = liftPort.BytesToRead;
            if (rxCount > 0)
            {
                rxReadBytes = liftPort.Read(rxBuf, 0, rxCount);
                rxChar = Encoding.UTF8.GetChars(rxBuf);
                string rxData = new string(rxChar);
                Debug.Print(rxData);
            }
            liftPort.Close();
            liftPort.Dispose();

            Debug.Print("Exiting serial port segment");

            //****************************************



            //****************************************
            // USB HOST events, FAT16/32 format
            //****************************************

            Debug.Print("Entering USB Host port segment");

            RemovableMedia.Insert += new InsertEventHandler(RemovableMedia_Insert);
            RemovableMedia.Eject += new EjectEventHandler(RemovableMedia_Eject);

            
            GHI.Usb.Host.Controller.MassStorageConnected += Controller_MassStorageConnected;

            /*
            Controller.MassStorageConnected += (sender, massStorage) =>
            {
                usbStick = massStorage;
                usbStick.Mount();    // fires the insert event when finished
            };
            */
            GHI.Usb.Host.Controller.Start();

            //evt.WaitOne();
            

            Thread.Sleep(-1);

            Debug.Print("Exiting USB Host port segment");

            //****************************************

            Debug.Print("Quiting MF_Start application");
        }

        static void Controller_MassStorageConnected(object sender, MassStorage e)
        {
            //throw new NotImplementedException();
            usbStick = e;
            usbStick.Mount();
        }

        static void RemovableMedia_Eject(object sender, MediaEventArgs e)
        {
            //throw new NotImplementedException();
            Debug.Print("Thumb drive EJECTED! You may REMOVE it now.");
            dLED.Write(false);
            Thread.Sleep(100);
            usbStick.Dispose();
        }

        static void RemovableMedia_Insert(object sender, MediaEventArgs e)
        {

            dLED.Write(true);

            //throw new NotImplementedException();
            Debug.Print("Thumb drive MOUNTED!");
            //evt.Set();

            if (e.Volume.IsFormatted)
            {
                rootDirectory = e.Volume.RootDirectory;

                Debug.Print("Available folders:");
                string[] strs = Directory.GetDirectories(e.Volume.RootDirectory);
                for (int i = 0; i < strs.Length; i++)
                    Debug.Print(strs[i]);

                Debug.Print("Available files:");
                strs = Directory.GetFiles(e.Volume.RootDirectory);
                for (int i = 0; i < strs.Length; i++)
                    Debug.Print(strs[i]);
            }
            else
            {
                Debug.Print("Media is not formatted. Formatting...");
                e.Volume.Format("FAT", 0);
                rootDirectory = e.Volume.RootDirectory;
            }


            Thread.Sleep(100);
            usbStick.Unmount();
            Debug.Print("Unmount ordered.");
        }        
    }
} 
```cs]

[/code

In follow-up to the above code, i tried :

static void RemovableMedia_Eject(object sender, MediaEventArgs e)
        {
            //throw new NotImplementedException();
            Debug.Print("Thumb drive EJECTED! You may REMOVE it now.");
            dLED.Write(false);
            Thread.Sleep(100);
            usbStick.Dispose();
            GHI.Usb.Host.Controller.Reset();
            GHI.Usb.Host.Controller.Start();
        }

Reset USB host driver, helped a little bit (4-5 mount / unmount cycles now increased to 10 mount / umount).
What i am doing is relevant ?

Please advice. I am eager to hear from GHI support. Thanks.

Your error will possibly be because of netmf running out of memory. Do you have debugging enabled on the device while you are doing these tests ? Can you display available memory in debug window and watch how that changes over time. Perhaps even print it out at 10-sec intervals ?

I’d also suggest if you want proper support, you’re going to need to get on the phone to GHI. While they are regulars on the forum and often see these posts, there’s no substitute to speaking to them and telling them a commercial customer has a challenge they need help with.

@ aavinan - I ran the first code you posted, but I wasn’t able to reproduce your error. I was able to insert and remove more than 20 times without issue twice. Have you tried different USB drives? Does it happen around 5-10 cycles every time?

@ John - Did you try it on FEZ Reaper ? Tried 2 different thumb drives. Standard ones of Transcend make.

Yes, after 5 - 10 times, usb drive insert is not detected.

@ Brett - Yes, debug enabled. Will check on memory as you said. Thanks.

@ aavinan - I tried it on the FEZ Reaper, yes.

I gave this a whirl too. I used a Reaper and the originally posted code.

I could only repro the issue if I removed and re-inserted the drive within a half-second or so, and the program continued to work ok if I pulled out the thumb drive, waited, and then re-inserted it.

Is it the case for aavinan that once it failes, it never comes back to life? I am definitely not seeing that behavior. I am only seeing missed insert notifications if the drive is re-inserted quickly.

[fyi - I tried debug and release code with and without a debugger attached, 50 trials each, SanDisk Cruzer 16Gb drive, Fez Reaper with only USB client DP and USB Host adapters installed]

@ mcalsyn - Thanks for your time.

Yes, once it fails it never comes back to life.

As for the hardware modules, i used - FEZ Reaper + USB client DP (X1) + USB Host (X2) + RS232 module (X3)
I am going to remove RS232 module and check the codes again.

Also with FEZ Config, i got the below message.


Firmware (TinyCLR) version information:
4.3.7.10 on this computer.
4.3.7.9 on this device.

The Firmware (TinyCLR) is not up to date. <<<
Please wait for the device to reboot… Done.


Could it be a reason ? Do you recommend a Firmware update ?

Update your firmware. Debugging a problem with a mismatch is not productive.

fwiw, my board and firmware were 4.3.7.9

@ Mike
Updated firmware 4.3.7.10

@ mcalsyn
After firmware update, i connected FEZ reaper + USB Client DP + USB Host + RS232 module.

I tried 2 models of transcend make. 4Gb & 2Gb capacity. Both are detected every time i tried (probably around 20 trials without power reset).
Another model of transcend, which is 2Gb - fails. After such a failure, no other usb drive is detected.

But with SPIDER-II running the same code, all 3 models of usb drives work well. There is no detection problem. Even the trancend 2Gb which causes failure in Reaper, works well in SPIDER-II.

I am confused. :wall:

@ aavinan - How quickly are you removing and reinserting the sticks? Are you running the debugger while you do so?

@ aavinan -

is this one always failed for just after few time inserted - remove quickly?

@ John @ Dat

Working usb drives :
Wait period between removal and insertion - 10 seconds min
After inserting usb drive, time taken to detect and mount - 5 seconds max

Failed usb drive :
Wait period between removal and insertion - 30 seconds
After inserting that usb drive which never detects, i wait for up to 60 seconds.
I tried debug code w/ debugger on. Also deployed release code and run the reaper board standalone.

Do you want me to do any specific test ? Please let me know.

Suspecting power requirement of that usb drive, I also tried using an external 12V dc @ 1A power supply.
But problem persists.

Since i live about 10 hours ahead of you, i will do the test in my day time and let you know the results.
Also i am sick now, so could not call your phones.

And thanks for your time.

@ aavinan -

Is there any way we can have the drive that FEZ Reaper is not happy with it?

@ Dat - Let me check. Give me a day to confirm.

@ Dat - Ok. Sending it by EMS speed post to the address mentioned in your Invoice (which came with products).
Please let me know the contact person’s full name.

@ aavinan -

A private msg sent to you.
Thank for your help!