Main Site Documentation

Spider & SD card not working


#1

Hi,

While I’m working on a larger project using the FEZ Spider board and several modules, I got to the point of adding the SD card support and I couldn’t get it to work. So, I made a new project to simply try the SD card function [em]only[/em] and … I still can’t get it to work!

I got took a new Spider board (v4.3.6.0), a SDcard module (v1.4 connected to port 5) and a CharacterDisplay module (v1.0 connected to port 11) and tried the following v4.3 VB code. I use VS Express 2012 with Framework SDK 4.3 (QFE1) and Gadgeteer package 2014 R5.


Imports GT = Gadgeteer
Imports GTM = Gadgeteer.Modules
Imports Gadgeteer.Modules.GHIElectronics

Namespace SD_Test
  Partial Public Class Program

    Dim TempString As String = "Test proces data " & vbCrLf

    Public Sub ProgramStarted()

      Display.Clear()
      Display.CursorHome()
      Display.BacklightEnabled = True

      If FlashDisk.IsCardInserted Then

        DisplayText("Step 1")
        FlashDisk.Mount()

        DisplayText("Step 2")
        Dim sdStorage As GT.StorageDevice = FlashDisk.StorageDevice

        DisplayText("Step 3")
        sdStorage.WriteFile("\CurrentParameters.csv", System.Text.Encoding.UTF8.GetBytes(TempString))

        DisplayText("Step 4")
        FlashDisk.Unmount()

      Else
        DisplayText("No flash disk")
      End If

    End Sub

    Private Sub DisplayText(Text As String)
      Display.Clear()
      Display.SetCursorPosition(0, 0)
      Display.Print(Text)
    End Sub

  End Class
End Namespace

I tried several different SD cards:

  • an 8GB SDHC card got to “step 1”, so mounting issues.
  • an 16MB card got to “step 3”, so file IO issues.

What am I doing wrong here?
Thanks for your input…

Kind regards,
Aubert


#2

Hi,
I had a similar issue,

Try changing:



To:


```vb]sdStorage.WriteFile("CurrentParameters.csv", System.Text.Encoding.UTF8.GetBytes(TempString))[/code


Note drop the \ at the beginning.

The \ used to work on earlier versions but 4.3 it causes an IO error.

#3

@ A_Dupont - You’ll also want to subscribe to the Mounted and Unounted events on the SDCard object. Mount performs some operations asynchronously so there is a race condition in your code where mounting may not complete by the time you start to do file IO.


#4

@ jasuk70: thanks for your advice. Unfortunately it did not make any difference. I removed the “”, but the result is that it still stops at step 3, so, it does not create the file…

@ John: ok, could explain in further details? I did not see anything about this in the documentation.


#5

I got some more information. I debugged the code and found that the .Mount() function returns False, which I did not check. The error message:

[quote]SDCard ERROR : The SD card does not have a valid filesystem.
#### Exception System.IO.IOException - CLR_E_VOLUME_NOT_FOUND (1) ####
#### Message:
#### System.IO.Path::NormalizePath [IP: 0070] ####
#### System.IO.Path::GetFullPath [IP: 001a] ####
#### System.IO.FileStream::.ctor [IP: 0009] ####
#### System.IO.File::OpenWrite [IP: 0008] ####
#### SD_Test.SD_Test.Program::ProgramStarted [IP: 004c] ####
A first chance exception of type ‘System.IO.IOException’ occurred in System.IO.dll
An unhandled exception of type ‘System.IO.IOException’ occurred in System.IO.dll
[/quote]

However, this is very strange. I use a clean, 16MB formatted, using FAT filesystem SD card.

Does anybody know what is wrong here?


#6

I tried different SD cards, with different format options (FAT/FAT32 file system) without any succes. The debugger keeps coming up with this error message:

[quote]SDCard ERROR : The SD card does not have a valid filesystem.
[/quote]

@ John: could you share the exact file system requirements for the Spider board with me? I can’t find anything on this online, except it should support FAT & FAT32. How about the MBR type, the cluster size? SD card type and size? What exact SD specs would work?

My project is on halt now, as I’m not able to write to SD
Thanks for your help…


#7

what size SD card are you trying ? Do you have a 1Gb or less card, somewhere in the discarded pile ?

I’d suggest that you need to be very explicit about what brand and capacity SD cards you have tried, and where (what PC OS) you’ve formatted them on. Formatting on a standard Windows OS with default FAT32 “quick” format is all you need.

Have you also tried swapping cables on the SD card ? The notes in the documents section here says try a shorter cable if possible. I’d also suggest you try a sample app that uses the SD card (from codeshare for example, eg https://www.ghielectronics.com/community/codeshare/entry/643 although I can’t be sure whether that’s only a 4.2 snippet) and then you might show whether the problem is in your hardware or the code.


#8

@ A_Dupont - Not every SD card will work. The ones we have tried do, but we only have a limited number to test. What Brett suggested usually helps.


#9

@ Brett: thanks for pointing the code snippet out. I rewrote it for VB and tried it. Found interestingly that a waitstate of a few seconds is required in order to auto mount the SD card, before attempting to access it.

@ John: this waitstate is probably the mount event you were refering to?

Anyway, now I got the [em]some[/em] SD cards working on a Cerberus board using below code. Strangely the Microsoft.SPOT.IO library and its functions is not available when using the Spider board?

Imports GT = Gadgeteer
Imports GTM = Gadgeteer.Modules
Imports Gadgeteer.Modules.GHIElectronics
Imports Microsoft.SPOT.IO

Namespace SD_Test___Cerberus

  Partial Public Class Program

    Public Sub ProgramStarted()

      Debug.Print("Program SD Card Test - Started")
      Thread.Sleep(4000)

      If FlashDisk.IsCardInserted Then

        If FlashDisk.IsCardMounted Then

          Dim vi As Microsoft.SPOT.IO.VolumeInfo = FlashDisk.StorageDevice.Volume

          Debug.Print(vi.Name)
          Debug.Print(vi.IsFormatted.ToString())
          Debug.Print(vi.FileSystem)
          Debug.Print(vi.TotalFreeSpace.ToString())

          Dim root As String = FlashDisk.StorageDevice.RootDirectory

          Debug.Print("Root: " & root)
          Debug.Print("Program SD Card Test - Finished")

        Else
          Debug.Print("Error: SD Card not mounted!")
        End If

      Else
        Debug.Print("Error: No SD Card found!")
      End If

    End Sub

  End Class

End Namespace

Thanks for the help so far!


#10

Hi,

I just gave this a go myself using the following code (This is on the FEZ Raptor, with just the sd card attached)

Imports GT = Gadgeteer
Imports GTM = Gadgeteer.Modules
Imports Gadgeteer.Modules.GHIElectronics


Namespace GadgeteerApp1
    Partial Public Class Program

        Private storageDev As GT.StorageDevice

        Public Sub ProgramStarted()
            Debug.Print("Program Started")

            InitialiseSDStorage()

            Dim TempString As String = "Test proces data " & vbCrLf

            Debug.Print("Writing file")

            storageDev.WriteFile("CurrentParameters.csv", System.Text.Encoding.UTF8.GetBytes(TempString))

            Debug.Print("File written")

        End Sub

        Private Sub InitialiseSDStorage()
            If sdCard.IsCardInserted = False Then
                Debug.Print("Waiting for SD card to be mounted")
                While sdCard.IsCardInserted = False
                    Thread.Sleep(500)
                End While
            End If

            If sdCard.IsCardMounted = False Then
                Debug.Print("Mounting SD Card")
                Try
                    sdCard.Mount()
                Catch ex As Exception
                    FatalError(ex.Message, "InitialiseSDStorage")
                End Try

                Thread.Sleep(1000)
            End If
            If sdCard.IsCardMounted = False Then
                FatalError("Cannot Mount SD Card", "InitialiseSDStorage")
            End If
            Debug.Print("SD Card Mounted")

            storageDev = sdCard.StorageDevice
            ' Setup routines to cat the ejecting of sd card
            AddHandler sdCard.Unmounted, AddressOf sdCard_SDCardUnmounted
            AddHandler sdCard.Mounted, AddressOf sdCard_SDCardMounted
        End Sub

        Private Sub sdCard_SDCardUnmounted(sender As Gadgeteer.Modules.GHIElectronics.SDCard, e As EventArgs)
            FatalError("SD Card Removed", "Cannot Continue")
            InitialiseSDStorage()
        End Sub

        Private Sub sdCard_SDCardMounted(sender As Gadgeteer.Modules.GHIElectronics.SDCard, SDCard As Gadgeteer.StorageDevice)
            Debug.Print("SD Card mounted")
            ' Place holder, not currently needed
        End Sub

        Public Sub FatalError(msg As String, location As String)
            Debug.Print("FATAL ERROR (" & location & "):" & msg)
            Thread.Sleep(Timeout.Infinite)
        End Sub

    End Class
End Namespace

And got this to write the file. I’m using a Sandisk 16GB SDHC card formatted FAT 32.

Jas


#11

@ jasuk70: thank you very much for the extensive solution! Appreciated.