Overview

Tiny File System can be used directly from any .NET Micro Framework or Gadgeteer project. In the case of a Gadgeteer project, you also have the added option of using the Flash File System module driver which is a custom module driver that can be used in from the Gadgeteer designer in Visual Studio.

The File System module driver is currently supports the GHI FLASH module

Using Tiny File System from .NET Micro Framework

The following code requires Microsoft.SPOT.Hardware.

Instantiate the file system

To instantiate the file system we need to pass in a block driver. The block driver provides the raw access to the storage medium that will be used by the file system. In this case we are using the MX25l3206BlockDriver which is compatible with the FLASH memory module from GHI. The module also has a LED indicator which can be used to indicate read/write activity on the device, the pin location of the LED is passed to the block driver. Since this driver uses SPI to communicate with the flash memory device, we also need to configure the SPI interface for the memory module.

In this example we are using a Gadgeteer board with the flash module connected to socket 9. Note you will need to select a appropriate 'S' socket for the Gadgeteer mainboard that you are using. If you are using a non-Gadgeteer .NETMF board then you can configure the SPI interface directly specifying the relevant SPI pins.
// Configure the SPI interface on socket 9
var spiSocket = Socket.GetSocket(9, true, null, null);
var spiModule = spiSocket.SPIModule;
var spiConfig = new SPI.Configuration(spiSocket.CpuPins[6], false, 0, 0, false, true, 12000, spiModule);
var spi = new SPI(spiConfig);

// Instantiate the block driver
var driver = new MX25l3206BlockDriver(spi, spiSocket.CpuPins[5], 4);

// Instantiate the file system passing the block driver for the underlying storage medium
var tfs = new TinyFileSystem(driver);


Now that you have an instance of the file system you can start working with it. The rest of the code will check of the device has been formatted, if not it will format the device which also mounts the device, or if it has been formatted it will be mounted directly. Once formatted or mounted you can proceed to use the file system to interact with the device.

if (tfs.CheckIfFormatted())
{
     tfs.Mount();
}
else
{
     tfs.Format();
}

// Create a file and write to it using a StreamWriter
using (var fs = tfs.Create("settings.dat"))
using (var wr = new StreamWriter(fs))
{ 
     wr.WriteLine("<settings>");
     wr.WriteLine("</settings>");
}

// Check if a file exists and if it does, 
// open it and read from it using a StreamReader
if (tfs.Exists("settings.dat"))
{
     using (var fs = tfs.Open("settings.dat", FileMode.Open))
     using (var rdr = new StreamReader(fs))
     {
       string line;
       while ((line = rdr.ReadLine()) != null)
       {
         Debug.Print(line);
       }
     }
}

Last edited Oct 29, 2013 at 10:25 PM by taylorza, version 3