Main Site Documentation

CerbuinoBee SDCard mf4.2 Vs mf4.3


#1

Hello guys, I’ve been trying to switch from mf4.2 to 4.3 but no matter what, I end downgrading firmware because I can’t get things to work right.

It seems that pin enum isn’t available anymore for Bee and no matter what I try, just can’t get them right or working correctly.


'MF4.2 example:
Imports CerbuinoBEE_Pins = GHI.Hardware.FEZCerb.Pin
Private WithEvents SD_Pin As New InterruptPort(CerbuinoBEE_Pins.PC2, False, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeBoth)

'MF4.3 example:
Imports CerbuinoBEE_Pins = GHI.Pins.Generic
Private WithEvents SD_Pin As New InterruptPort(DirectCast(34, Cpu.Pin), False, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeBoth)

'or

Private WithEvents SD_PinPC2 As New InterruptPort(CerbuinoBEE_Pins.GetPin("C", 2), False, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeBoth)

No matter what I do, I always get hardware exception on mf4.3.
I really want to switch to MF4.3 to try SDCard since mf4.2 keeps damaging all my microsd.

Any idea?


#2

Well, saw that. It’s what I’m using on mf4.3 example. And that’s what causes the hardware exception.
Generic.GetPin(“C”, 2) which from what I understood, should be the equivalent to PC2 on CerbuinoBee


#3

2 is a power pin. I think valid values are 3-9.


#4

Thanks, actually pin 9 trigged something but not all the time. Doesn’t work as well as on MF4.2 PC2 pin, no clue why.
Isn’t any other way to detect sd mount and unmount?
I’ve read a lot on the forum and a lot of ppl has the exact same problem.
Also seen that GHIElectronics.Gadgeteer.FEZCerbuinoBee (in my case Mainboard) exposes a delegate sub SDCardMountedEventHandler.
Did also try to


Public Sub ProgramStarted()
  AddHandler Mainboard.SDCardMounted, AddressOf Delegated_SDCardMountedEventHandler
  Debug.Print("Program Started")
End Sub

Private Sub D_SDCardMountedEventHandler(sender As GHIElectronics.Gadgeteer.FEZCerbuinoBee, device As Gadgeteer.StorageDevice)
  char_display.Clear()
  char_display.Print("SD mounted")
End Sub

but no luck too.


#5

@ Alexandre Dias - Yes, Cerbuino Gadgeteer mainboad handles on board SD. The most recent discussion about it is here:

https://www.ghielectronics.com/community/forum/topic?id=16981


#6

@ Architect - thank you, did read that thread over and over. Most things there aren’t applicable on Bee, most of those are applicable on SD Module and not onboard sd card reader.

Anyway, found some glitches but found a reasonable way to detect SDcard insert , mount, and unmount.

this is the test I’ve been working on


Partial Public Class Program

        Public Event SDCardMounted_event()
        Public Event SDCardUnMounted_event()

        Public Sub ProgramStarted()
            Debug.Print("[+] Program Started")
            Debug.EnableGCMessages(True)
            Debug.Print("[+] " & Debug.GC(True).ToString & " mem free")

            AddHandler SDCardMounted_event, AddressOf SDCardMountedEventHandler
            AddHandler SDCardUnMounted_event, AddressOf SDCardUnMountedEventHandler

            With New Thread(AddressOf SDMountThread)
                .Start()
            End With


        End Sub


        Private Sub SDCardMountedEventHandler()
 #If DEBUG Then
            Debug.Print("[+] SD Mounted")
 #End If
            char_display.Clear()
            char_display.Print("SD mounted")
        End Sub
        Private Sub SDCardUnMountedEventHandler()
 #If DEBUG Then
            Debug.Print("[+] SD UnMounted")
 #End If
            char_display.Clear()
            char_display.Print("SD unmounted")
        End Sub

        Private Sub DebugLed_OnConnect()
            Mainboard.SetDebugLED(True)
            Thread.Sleep(100)
            Mainboard.SetDebugLED(False)
        End Sub

        Private Sub DebugLed_OnDisconnect()
            Mainboard.SetDebugLED(True)
            Thread.Sleep(20)
            Mainboard.SetDebugLED(False)
            Thread.Sleep(20)
            Mainboard.SetDebugLED(True)
            Thread.Sleep(20)
            Mainboard.SetDebugLED(False)
            Thread.Sleep(20)
            Mainboard.SetDebugLED(True)
            Thread.Sleep(20)
            Mainboard.SetDebugLED(False)
        End Sub

        Private FirstStateMount As Boolean = True
        Private LastStateMounted As Boolean = False
        Private Sub SDMountThread()
            While True
                Debug.Print("[+] " & Debug.GC(True).ToString & " mem free")

 #If DEBUG Then
                Debug.Print("[+] " & Mainboard.IsSDCardMounted.ToString)
                Debug.Print("[+] " & Mainboard.IsSDCardInserted.ToString)
 #End If
                If FirstStateMount Then
                    FirstStateMount = False 'no longer firstrun
                    If Not Mainboard.IsSDCardMounted AndAlso Mainboard.IsSDCardInserted Then
                        Dim __Result As Boolean = Mainboard.MountStorageDevice("SD")
 #If DEBUG Then
                        Debug.Print("[+] Mount Result: " & __Result.ToString)
 #End If
                        Dim __counter As Integer = 0
                        Dim __Mounted As Boolean = True
                        While Not Mainboard.IsSDCardMounted
                            Thread.Sleep(100)
                            If __counter >= 20 Then
                                __Mounted = False
                                Exit While
                            End If
                            __counter += 1
                        End While
                        If __Mounted Then
                            DebugLed_OnConnect()
                            LastStateMounted = True
                            RaiseEvent SDCardMounted_event()
                        Else
                            DebugLed_OnDisconnect()
                            RaiseEvent SDCardUnMounted_event()
                            LastStateMounted = False
                        End If
                        __counter = Nothing
                        __Mounted = Nothing
                        __Result = Nothing
                    ElseIf Mainboard.IsSDCardMounted AndAlso Mainboard.IsSDCardInserted Then
                        'already mounted, probably was booted that way
                        DebugLed_OnConnect()
                        LastStateMounted = True
                        RaiseEvent SDCardMounted_event()
                    End If
                Else
                    If Not Mainboard.IsSDCardMounted AndAlso Mainboard.IsSDCardInserted Then
                        Dim __Result As Boolean = Mainboard.MountStorageDevice("SD")
 #If DEBUG Then
                        Debug.Print("[+] Mount Result: " & __Result.ToString)
 #End If
                        Dim __counter As Integer = 0
                        Dim __Mounted As Boolean = True
                        While Not Mainboard.IsSDCardMounted
                            Thread.Sleep(100)
                            If __counter >= 20 Then
                                __Mounted = False
                                Exit While
                            End If
                            __counter += 1
                        End While
                        If __Mounted Then
                            DebugLed_OnConnect()
                            LastStateMounted = True
                            RaiseEvent SDCardMounted_event()
                        Else
                            DebugLed_OnDisconnect()
                            RaiseEvent SDCardUnMounted_event()
                            LastStateMounted = False
                        End If
                        __counter = Nothing
                        __Mounted = Nothing
                        __Result = Nothing
                    End If
                    If LastStateMounted AndAlso Not Mainboard.IsSDCardInserted Then
                        DebugLed_OnDisconnect()
                        RaiseEvent SDCardUnMounted_event()
                        LastStateMounted = False
                    End If
                End If
                Thread.Sleep(300)
            End While
        End Sub
    End Class

Something I’ve noticed was:
A first chance exception of type ‘System.Exception’ occurred in GHI.Hardware.dll
When SD card is inserted. Any clue what this is?


#7

Well, this last solution isn’t actually working for a long period of time.
After a few minutes the following props, return:
Mainboard.IsSDCardMounted --> false
Mainboard.IsSDCardInserted --> true
Mainboard.MountStorageDevice(“SD”) --> false

If i just reset the board, everything gets to work again.


#8

@ Alexandre Dias - Pin PC2 is the correct pin for the SD card detect. We use it internally in the Gadgeteer mainboard drivers so you will get an exception when you try to use it. The code you are using looks correct. However, see my last post in the thread Architect linked: https://www.ghielectronics.com/community/forum/topic?id=16981&page=8#msg169401 The thread actually talks about the onboard SD card reader, the module itself was only mentioned early on.


#9

Hello John, thanks for the reply.

Thing is, the code is working fine and when program is running, it detect successfully when SD is inserted and mounts it without a problem. Issue is, after a long time during checking, the SD get unmounted automatically and isn’t possible to mount it again.

Any idea what might be causing this?


#10

Hello Andre, unfortunatly it’s not gc, just tested that and the error persists.
I also made an other test, I’ve replaced my SDMountThread() with the following code


While True
  Debug.Print("[+] " & Mainboard.IsSDCardMounted.ToString)
  Debug.Print("[+] " & Mainboard.IsSDCardInserted.ToString)
  Thread.Sleep(300)
End While

Error still persists, There must be something in the mf4.3 because this didn’t happened on mf4.2.

I guess I’ll downgrade, once again. This is sooo fustrating because I was looking for the sqlite support.


#11

The only exceptions i get are when sd is inserted or removed.
A first chance exception of type ‘System.Exception’ occurred in GHI.Hardware.dll

It just gets unmounted by itself. Also tryed with 3 other microsd cards (mostly kingston, and sandisk from my camera).

Once the sd gets unounted (and still inserted), there won’t be possible to mount it without reseting the board. Mainboard.MountStorageDevice(“SD”) will always return false.

Let me know if you want me to test something else. I’ll continue project with an other board, mf4.2 unfortunatly.


#12

Hi Alexandre! I was struggling with the same problem for a long time and finally found a solution. Try to declare a timer with 5 seconds interval or adjust interval you need and put my code inside of it. I am still getting the Hardware exception but anyway SD is mounted and unmounted well:


GT.Timer timer = new GT.Timer(5000)

void sd_check_Tick(GT.Timer timer)
        {
            Debug.Print("Inside the SD timer");
            try
            {
                if (Mainboard.IsSDCardInserted == true)
                {
                    Debug.Print("SD card inserted");
                    if (Mainboard.IsSDCardMounted == false)
                    {
                        Debug.Print("SD card ready to be mounted");
                        string volname = Mainboard.GetStorageDeviceVolumeNames()[0];
                        Mainboard.MountStorageDevice(volname);
                        Debug.Print("SD card ready for use");
                        
                    }
                    else
                    {
                        sd_check.Stop();
                        Debug.Print("SD timer stoped");
                    }
                }
            }
            catch (Exception exp)
            {              
                Debug.Print(exp.Message.ToString());
            }
        }


Also you should start the timer in SDUnmountedEvent.

Code in C# but it’s not the problem to translate it to VB


#13

Thanks Alex, I’ll give it a try.
I usually program in C#, I’m doing this in VB on purpose as Ii’ll give the entire project to someone that isn’t used to C#, only VB. But yeah, it’s quite easy to translate it.
I’d prefer a thread so I’ll try both approaches with 5sec delay.
GT.Timer also have a different working method then normal timers, I wonder if it will solve this issue.
I’ll let you know the result