Main Site Documentation

Cannot write to SD card, but can read. Cannot start Mass Storage


#1

I’m having two problems that I need help with. I’ve searched the forums and have seen similar problems but nothing exactly like what I am seeing. First I would like to simply read and write text files to the SD card to save settings and other information inside my application. To flush out the interfaces I started by simply using the example code, which I cannot get to work.

I formatted my 2gb Kingston card as Fat32 with 4k blocks. I created 4 directories on it (while still in my PC). I then inserted it into my FEZ Domino and ran the following example:


using System;
using System.IO;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.IO;
using GHIElectronics.NETMF.IO;

namespace Test
{
    class Program
    {
        public static void Main()
        {
        // ...
        // SD Card is inserted
        // Create a new storage device
        PersistentStorage sdPS = new PersistentStorage("SD");
        // Mount the file system
        sdPS.MountFileSystem();
            // Assume one storage device is available, access it through
        // Micro Framework and display available files and folders:
        Debug.Print("Getting files and folders:");
        if (VolumeInfo.GetVolumes()[0].IsFormatted)
        {
            string rootDirectory =
            VolumeInfo.GetVolumes()[0].RootDirectory;
            string[] files = Directory.GetFiles(rootDirectory);
            string[] folders = Directory.GetDirectories(rootDirectory);
            Debug.Print("Files available on " + rootDirectory + ":");
            for (int i = 0; i < files.Length; i++)
            Debug.Print(files[i]);
            Debug.Print("Folders available on " + rootDirectory + ":");
            for (int i = 0; i < folders.Length; i++)
            Debug.Print(folders[i]);
        }
        else
        {
         Debug.Print("Storage is not formatted. Format on PC with FAT32/FAT16 first.");
        }
        // Unmount
        sdPS.UnmountFileSystem();
        }
    }
}


It seems to work fine and lists the directories I had previously created accurately. Then I tried to run the next example to write a simple text file:


using System.Threading;
using System.Text;
using Microsoft.SPOT;
using System.IO;
using Microsoft.SPOT.IO;
using GHIElectronics.NETMF.IO;
namespace MFConsoleApplication1
{
    public class Program
    {
        static void Main()
        {
            // ... check if SD is inserted

        // SD Card is inserted
        // Create a new storage device
        PersistentStorage sdPS = new PersistentStorage("SD");
        // Mount the file system
        sdPS.MountFileSystem();
        // Assume one storage device is available,
        // access it through NETMF
        string rootDirectory = VolumeInfo.GetVolumes()[0].RootDirectory;
        FileStream FileHandle = new FileStream(rootDirectory + @ "\hello.txt", FileMode.Create);
        byte[] data =
        Encoding.UTF8.GetBytes("This string will go in the file!");
        // write the data and close the file
        FileHandle.Write(data, 0, data.Length);
        FileHandle.Close();
        // if we need to unmount
        sdPS.UnmountFileSystem();
        // ...
        Thread.Sleep(Timeout.Infinite);
        }
    }
}

When I get to the line “FileHandle.Write(data, 0, data.Length);” I get the following exception:

#### Exception System.IO.IOException - CLR_E_FILE_IO (1) ####
#### Message: 
#### Microsoft.SPOT.IO.NativeFileStream::Write [IP: 0000] ####
#### System.IO.FileStream::Write [IP: 002a] ####
#### MFConsoleApplication1.Program::Main [IP: 0035] ####

A first chance exception of type ‘System.IO.IOException’ occurred in Microsoft.SPOT.IO.dll
An unhandled exception of type ‘System.IO.IOException’ occurred in Microsoft.SPOT.IO.dll

I’ve tried everything I can think of. I am connected to both USB and an external power supply. Any ideas?

The second SD related problem I’m having is with the mass storage feature. I’d like to be able to connect my device to a PC and have it mount as a drive so I can copy script files to it. I wired a small switch to the mode pads that allows me to toggle between USB and COM debug mode. Using the following example, I have my device in USB mode. I run it in VS to deploy it and it throws an exception (since it is in USB mode). I then flick the switch to COM mode and reset the Domino. I’d then expect if everything worked for Windows to recognize the device but nothing happens. I’m not sure where to go from here.


using System;
using System.IO;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using Microsoft.SPOT.IO;
 
using GHIElectronics.NETMF.USBClient;
using GHIElectronics.NETMF.IO;
using GHIElectronics.NETMF.Hardware;
 
namespace USBClient_Example
{
    public class Program
    {
        public static void Main()
        {
            // Check debug interface
            if (Configuration.DebugInterface.GetCurrent() == 
                Configuration.DebugInterface.Port.USB1)
                throw new InvalidOperationException("Current debug interface is USB. ");
 
            // Start MS
            USBC_MassStorage ms = USBClientController.StandardDevices.StartMassStorage();
 
            // Assume SD card is connected
            PersistentStorage sd;
            try
            {
                sd = new PersistentStorage("SD");
            }
            catch
            {
                throw new Exception("SD card not detected");
            }
            ms.AttachLun(0, sd, " ", " ");
 
 
            // enable host access 
            ms.EnableLun(0);
 
            Thread.Sleep(Timeout.Infinite);
        }
    }
}

I know that’s a lot but any help for either problem would be appreciated. Thanks,

Dean


#2

on your first issue, there are firstly a number of threads on Kingston cards being “special”. Perhaps they draw more power than others, not sure, but i don’t think that’s the cause of your exception.

The exception you’re seeing is *I suspect) caused by this code block:

       FileHandle.Close();
        // if we need to unmount
        sdPS.UnmountFileSystem();

where the file close and the unmount happen too quick to let the write/close complete.

add some pauses


 thread.sleep(500);
     FileHandle.Close();
        // if we need to unmount
 thread.sleep(500);
         sdPS.UnmountFileSystem();
     

and see if that helps?

On the second one, first thing i’d suggest checking is that Windows recognises the change in device, and therefore shows you a different device in Device Manager. Did you install the drivers in Windows correctly??


#3

Can add a 10uF on pararllel at C10 location? This might help stabilize the power source to the card since some kingstone cards withdraw relatively high power.


#4

Brett,

I had seen stuff about Kingston cards causing people problems, but I thought if an external power supply was used it was OK. I didn’t realize as Joe has pointed out that locally the voltage drops on the SD card reader. I may just try to pick up a new card to see if that helps.

On your comment about the exception, I see the exception when I am stepping through the code with the debugger right at the FileHandle.Write command, not when the file is being closed.

On your comment about the drivers for mass storage, I haven’t installed any drivers yet because I was expecting Windows to ask for drivers once it recognized new hardware. Basically I’m not seeing Windows react at all.

Thanks for your responses so far,
Dean


#5

I picked up a new 2GB PNY SD card and it doesn’t seem to make a difference. I get the same exception when I try to use the example code to write a file.


#6

Should I still try to install a capacitor even though the new card didn’t make a difference? The only thing I have that is 10uF is electrolytic. How should I install it (polarity of existing cap)?

Do you have any other examples of writing something simple to the SD card that I could try? I can’t imagine that is the problem because I am simply copying and pasting the examples out of the manual.

Can anyone think of something stupid that I am not doing? Is it possible that my Domino is faulty and cannot write files?

Dean


#7

I can write to the SD card now after some weird behavior. I was able to write to the new card once and then when I looked at the contents everything that was previous on the card was gone as if it got corrupted somehow. I reformatted it using the SDCard.org formatter instead of Windows with 8k blocks and now it seems to be working reliably from what I can tell. Hopefully it stays that way.