[Fez Panda 2] Reboot\Crash on SD insertion

Hi, I’m really newb with NETMF and start facing strange problems >:( !

I wrote (with the help of the beginner guide) this code:


using System;
using System.IO;
using System.Threading;
using System.Text;
using Microsoft.SPOT;
using Microsoft.SPOT.IO;
using Microsoft.SPOT.Hardware;
using GHIElectronics.NETMF.FEZ;

using GHIElectronics.NETMF.IO;
using GHIElectronics.NETMF.Hardware;

namespace AutoMount
{
    public class Program
    {
        static Boolean sdOK;
        static InterruptPort IntButton;
        static int nb;
        static OutputPort LED;

        public static void Main()
        {
            RemovableMedia.Insert += new InsertEventHandler(RemovableMedia_Insert);
            RemovableMedia.Eject += new EjectEventHandler(RemovableMedia_Eject);
            Debug.Print("START!!!");
            sdOK = new Boolean();
            sdOK = false;
            nb = 0;
            
            // Start auto mounting thread
            LED = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.LED, false);
            new Thread(SDMountThread).Start();

            // Your program goes here
            // ...

            // la broche génèrera une interruption à chaque flanc haut et bas
            IntButton = new InterruptPort((Cpu.Pin)FEZ_Pin.Interrupt.LDR, true,Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeLow);
            // ajoute un gestionnaire d'interruption à la broche
            IntButton.OnInterrupt += new NativeEventHandler(IntButton_OnInterrupt);

            Thread.Sleep(Timeout.Infinite);
        }

        static void IntButton_OnInterrupt(uint port, uint state, DateTime time)
        {
           Debug.Print("LDR press!");
           LED.Write(true);
           if (sdOK)
            {
                // Considère qu'un périphérique de stockage est disponible et y accède à travers NETMF
                string rootDirectory = VolumeInfo.GetVolumes()[0].RootDirectory;
                FileStream FileHandle = new FileStream(rootDirectory + @ "\hello.txt", FileMode.Append);
                byte[] data = Encoding.UTF8.GetBytes(nb+" Cette chaine ira dans le fichier !\r\n");
                // Ecrit les données et ferme le fichier
                FileHandle.Write(data, 0, data.Length);
                FileHandle.Close();
                //VolumeInfo.GetVolumes()[0].FlushAll();
                Debug.Print("SD writting! "+nb+" Cette chaine ira dans le fichier !");
                nb++;
            }
            else
            {
                Debug.Print("Pas de SD ou mauvais systeme de fichier (formater en FAT16/32)");
            }
        }

        static void RemovableMedia_Eject(object sender, MediaEventArgs e)
        {
            Debug.Print("SD card ejected");
            sdOK = false;
        }

        static void RemovableMedia_Insert(object sender, MediaEventArgs e)
        {
            Debug.Print("SD card inserted");

            if (e.Volume.IsFormatted)
            {
                Debug.Print("SD card is OK and ready");
                sdOK = true;
            }
            else
            {
                Debug.Print("SD card is not formatted");
                sdOK = false;
            }
        }

        public static void SDMountThread()
        {
            PersistentStorage sdPS = null;
            const int POLL_TIME = 3000; // check every 3 second

            bool sdExists;

            
            while (true)
            {
                try // If SD card was removed while mounting, it may throw exceptions
                {
                    sdExists = PersistentStorage.DetectSDCard();
                    // make sure it is fully inserted and stable
                    if (sdExists)
                    {
                        Thread.Sleep(50);
                        sdExists = PersistentStorage.DetectSDCard();
                    }

                    if (sdExists && (sdPS == null))
                    {
                        sdPS = new PersistentStorage("SD");
                        sdPS.MountFileSystem();
                        Thread.Sleep(250);
                        sdOK = true;
                    }
                    else if (!sdExists && (sdPS != null))
                    {
                        sdOK = false;
                        sdPS.UnmountFileSystem();
                        Thread.Sleep(250);
                        sdPS.Dispose();
                        Thread.Sleep(250);
                        sdPS = null;
                    }
                }
                catch
                {
                    if (sdPS != null)
                    {
                        sdPS.Dispose();
                        Thread.Sleep(250);
                        sdPS = null;
                    }
                }
                Thread.Sleep(POLL_TIME);
            }
        }
    }
}

Quickly, this code must turn the onboard LED on when I press the LDR key and write a new line (starting by a nomber i incremeent eatch time) in a file on the SDcart

But every time I reinsert the SD card, the Panda crash! ( the led shutdown, and If I press LDR the counter add in the beginning of my line restart from 0 in my file, and I lost the Debug.Print ability in VisualStudio)

An other fact, when I remove or insert a sd card, the onboard led seem to fadeaway like if theire is a short on the Panda board (is it possible?) but few sec later the led go back to full light…

So my question (finaly :wink: ) WHAT I’M DOING WRONG!!! this turning me to mad! I only need to automount/unmount card (btw I already try with sdDetect pin it’s the same) and raise a flag (sdOK here) to know that a SDcard is in and I can write on it…

FInal point, I work on Panda 2 with 4.1.5.0 solution revision.

You probably have weak power source. Connect Panda directly yo USB not through hub or add power pack.

I’m already plug on USB directly.
Moreover, I tried with a fresh new 9V cell and it’s the same! :frowning:

Try a simple blink LED then plug the SD card, would it reset the board?

Yes still the same, every time I insert an SD card (and I tried with differents cards 1Gb and 4GbSDHC for Sandisk) the onboard LED fade, the board reboot and I loose the Debug interface…

The only cause for this is power but you checked that?!

You can call GHI directly for an RMA