Copying from USB to SD in Panda II

Hey gents, I’m working on a project involving a music shield playing from SD, and I wanted to set it up so that when a USB flash drive is inserted, it would stop playing and autocopy the files from USB to SD.

It works, but on larger files over 5 meg or so, everything freezes for a while before it starts to copy the file. I have a separate thread running that controls some PWM’d RGB LED strips, and they should flash a certain sequence to indicate it is in copy mode.

I’m testing with a couple 20 meg files, and everything just stops for about 2 minutes before each file copy, then Garbage Collector runs saying it only took 4 milliseconds or so, and then it starts to copy as seen by the thumb drive light flashing and my LED strips starting their sequence. The actual copy time is a couple minutes per file, which is probably acceptable.

Any idea what this initial delay could be?

Here is the relevant portion of code:

static void RemovableMedia_Insert(object sender, MediaEventArgs e)
        {
            Debug.Print("Storage \"" + e.Volume.RootDirectory + 
                        "\" is inserted.");
            
            if (e.Volume.IsFormatted && e.Volume.RootDirectory != @ "\SD")
            {
                Debug.Print("Attempting File Transfer");
                _transferInProgress = true;
                if (_musicShield.IsBusy == true)
                {
                    _musicShield.StopPlaying();
                    Thread.Sleep(500);
                    fs.Close();
                }

                
                string[] files = System.IO.Directory.GetFiles(@ "\USB\");

                // Copy the files and overwrite destination files if they already exist.
                string fileName;
                string destFile;
                foreach (string s in files)
                {
                    // Use static Path methods to extract only the file name from the path.
                    fileName = System.IO.Path.GetFileName(s);
                    Debug.Print("Copying " + fileName + " from USB to SD");
                    destFile = System.IO.Path.Combine(@ "\SD\", fileName);
                    System.IO.File.Copy(s, destFile, true);
                }

                _transferInProgress = false;
                CheckSwitch();

            }
            else
            {
                Debug.Print("Storage is not formatted. Format on PC with FAT32/FAT16 first.");
            }
        }

:frowning: No ideas? Would using a filestream CopyTo() maybe correct the problem? I’m at work so I can’t test that right now… and it seems like it would be adding quite a bit of code when System.IO.File.Copy should work.

I would check power maybe

I appreciate the reply, Gus.

I don’t think power should be a problem though… I’m powering the Panda II through the 5V pin with a 1 amp supply, and the USB drive is connected through a powered 4 port Hub.

One last thing to try maybe. Add a 47uF capacitor or larger as close as possible to the SD card. We want to see fi this si hardware problem.

Also try FAT and FAT32, try different cards also.

I’ll see if I can dig up a cap at work. I’ve only tried the one MicroSD card and USB thumb drive since once it starts copying everything works.

I will play around with it more this evening and let you know if any of your suggestions work.

Well, the SD card was originally FAT. I formatted to FAT32, and there was no difference. I don’t have another SD card handy right now, but this one has been working great for music shield playback if that means anything.

I tried two different USB Flash Drives, both in FAT32. I don’t have one under 8GB, so FAT is out of the question unless there is a way to force Win7 to format it for only 2GB.

I also tried with and without the powered USB hub.

Unfortunately I don’t have a 47uF cap to try. I’ll grab one next week… maybe I’ll toy with using filestreams to copy as well.

I shot a little video of the problem. This was copying a couple small text files and one 6MB MP3. When it first tries to copy the 6MB file, all threads freeze up for about 30 seconds… then GC runs saying it took a few milliseconds, and it copies the file fine.

This also maybe a bug in FAT libs. I know there were fixes in NETMF 4.2 so maybe the only way to wait till we have 4.2 in near future. This is a guess.

Well, that’s good to know, thanks!

This isn’t exactly mission critical for my little project… the user could always pull the SD card and update it that way. But since this is my first FEZ and C# project, I figured I’d give it a try and see what I could learn.

What about copying between USB drives?

Hmm, interesting idea. I’m on my way out the door, but I’ll try it this afternoon.

Ok, I’m sort of new to C#, and I can’t quite get the USB to USB file copy working.

I added a third persistent storage device for the second USB drive, and when it is inserted “DeviceConnectedEvent” is called and _usb2.MountFileSystem() occurs, but the RemovableMedia_Insert function isn’t happening after.

What am I missing?

I still need to change the actual copy paths, but I was going to see what the root directory for the second drive was before I did that.

static void DeviceConnectedEvent(USBH_Device device)
        {
            if (device.TYPE == USBH_DeviceType.MassStorage)
            {
                _usbDrives ++;

                if (_usbDrives == 1)
                {
                    Debug.Print("First USB Mass Storage detected...");
                    _usb = new PersistentStorage(device);
                    _usb.MountFileSystem();
                }
                if (_usbDrives == 2)
                {
                    _usb2 = new PersistentStorage(device);
                    _usb2.MountFileSystem();
                    Debug.Print("Second USB Mass Storage detected...");
                }

                
            }
        }
 
        static void RemovableMedia_Insert(object sender, MediaEventArgs e)
        {
            Debug.Print("Storage \"" + e.Volume.RootDirectory + 
                        "\" is inserted.");
            
            if (e.Volume.IsFormatted && e.Volume.RootDirectory != @ "\SD")
            {
                Debug.Print("Attempting File Transfer");
                _transferInProgress = true;
                if (_musicShield.IsBusy == true)
                {
                    _musicShield.StopPlaying();
                    Thread.Sleep(500);
                    fs.Close();
                }

                
                string[] files = System.IO.Directory.GetFiles(@ "\USB\");

                // Copy the files and overwrite destination files if they already exist.
                string fileName;
                string destFile;
                foreach (string s in files)
                {
                    // Use static Path methods to extract only the file name from the path.
                    fileName = System.IO.Path.GetFileName(s);
                    Debug.Print("Copying " + fileName + " from USB to SD");
                    destFile = System.IO.Path.Combine(@ "\SD\", fileName);
                    System.IO.File.Copy(s, destFile, true);
                }

                _transferInProgress = false;
                CheckSwitch();

            }
            else
            {
                Debug.Print("Storage is not formatted. Format on PC with FAT32/FAT16 first.");
            }
        }