FEZ Cerberus + SDCard = IndexOutOfRangeException

Hi guys,

I’m working on a FEZ Cerberus (HW Rev. 1.3 - FW
After an hour of loop “writing code and then test” , now I’m encountering a strange exception.

I’ll try to explain…

In the immediate window of VS2012 I read this text:

[quote]Using mainboard GHI Electronics FEZ Cerberus version 1.3
Eccezione first-chance di tipo ‘System.InvalidOperationException’ in GHI.Hardware.dll[/quote]

That exception is a little bit strange because is thrown before the code enters in ProgramStarted method.
So I checked and the exception comes from the “InitializeModules” of the Main() method, during the initialization of the SDCard module.

Investigating a little bit more, I downloaded the SDCard_43.cs from bit bucket and I found that the exception is thrown by this line:


in the constructor of SDCard class:

public SDCard(int socketNumber)
            Socket socket = Socket.GetSocket(socketNumber, true, this, null);
            socket.EnsureTypeIsSupported('F', this);

            socket.ReservePin(Socket.Pin.Four, this);
            socket.ReservePin(Socket.Pin.Five, this);
            socket.ReservePin(Socket.Pin.Six, this);
            socket.ReservePin(Socket.Pin.Seven, this);
            socket.ReservePin(Socket.Pin.Eight, this);
            socket.ReservePin(Socket.Pin.Nine, this);

            RemovableMedia.Insert += this.OnInsert;
            RemovableMedia.Eject += this.OnEject;

            this.IsCardMounted = false;

            this.cardDetect = GTI.InterruptInputFactory.Create(socket, Socket.Pin.Three, GTI.GlitchFilterMode.On, GTI.ResistorMode.PullUp, GTI.InterruptMode.RisingAndFallingEdge, this);
            this.cardDetect.Interrupt += this.OnCardDetect;

            if (this.IsCardInserted)

The code of the Mount() method is:

public bool Mount()
            if (this.IsCardMounted) throw new InvalidOperationException("The card is already mounted.");

            return Mainboard.MountStorageDevice("SD");

I’m not able to reproduce the exception each time I go step-by-step in the source code, so it seems to be like a timing issue in the code contained in Mainboard.MountStorageDevice(“SD”) method…

What do you suggest?
Is a known issue?

@ BigFoot - Do you have the SD card inserted on startup? If you take it out and insert it after your program has began, does everything function properly?

@ John - Yes, during all my tests I had the SD card inserted on startup.
Stupidly I didn’t try to insert it after the program has begun…

But I think it’s an issue of that “FEZ Cerberus mainboard”, because:

  • I tried to change the SD card and the exception still occurs
  • I tried to change the SD card module and the exception still occurs
  • I tried to reload the loader (with STDFU) and then the firmware, but the exception still occurs
  • I tried to change the FEZ Cerberus (I own another one with HW Rev. 1.3 - FW … IT WORKED!

I’m a little bit embarassed because I don’t know why the first Cerberus has stopped to work with SD card module in this way (suddenly after an hour of development/test).
Maybe some hardware part fails on socket 7?

@ BigFoot - I have a feeling it is a race condition actually, as you mentioned with it not happening when you step through the code. The second Cerberus is likely just a bit different where it doesn’t show the issue easily. Does it happen on the first Cerberus every time if you don’t step through it?

@ John - Yes, every time.

@ BigFoot - If you modify the driver and add a sleep like below, does the issue go away?

if (this.IsCardInserted) {


@ John - I’m so sorry for the late reply!
I’m trying to reproduce the issue because FEZ Cerberus doesn’t throw that exception anymore.
It seems like if it has cooled during these 15 days of no work and now is ready to work! ::slight_smile:

I’ll tell you if working on it for some hour causes that exception (maybe not a race condition but an hardware issue?).

@ John - Sorry again, I have the answer you expect from weeks.
That exception occurred again so I made the change you suggested (adding a Thread.Sleep(250) ) and this solved the issue.

I really think that you are right, it was a race condition.

Thanks for help, keep in mind this change to the SDCard class for the next SDK release (if appropriate).