Version 4.1.4

This is an updated version of the SDFileSystem library module supplied with the compiler. It is now at Version 4.1.4. The module now supports multiple simultaneous files, upto a maximum of 127. The option #option SD_MAX_FILES is used to set the maximum number of files required and should be set as low as possible to reduce RAM use. Note also that the option #option SD_SUPPORT_MULTIPLE_FILES must be set to True for the module to support multiple files. It is set to False by default to maintain backwards compatibility. When set to true, many of the commands in the library require an extra parameter, pFileNumber, passing as the first parameter to indicate which file is being opened, written to, closed etc. In addition, commands such as MkDir and Dir can be used even when files are open, so long as multiple file support is active.

Version 4.1.4 also adds the following commands:

  • CloseAll
  • SaveAll
  • FreeFile
  • NewRecord, SetRecord, GetRecord, RecordLength and NumberOfRecords (moved from Database module; added optional parameter pRecordLength to OpenFileRW to set length of one record when using file for random access; n.b. first create file with NewFile, then close, followed by opening with OpenFileRW, before using these commands)
  • Truncate
  • CopyFile
  • MoveFileCut, MoveFilePaste
  • VolumeLabel, VolumeID

Also new in Version 4.1.4 is the option to use the second MSSP port for PICs that have this facility - use #option SD_MSSP = 2.

See the full version history below for more details.

Version 4.1.4 follows the release of Version 3.1.1. The main additions in Version 3 were support for SDHC high capacity SD cards, as well as support for the FAT32 format. These two combined allowed the use of much higher capacity cards than previously. New commands were also added, namely FATType, SerialNumber, ReadNBytes, Format, DestroyFormat (use with care), FileCount and DirCount. In addition, a new option, #option SD_REINIT_SPI, was added.

Note that the Init command for initialising the card has been changed slightly since Version 3.1.1 and this will necesitate a change to existing user code. The command now returns a byte, not a boolean. The return variable gives more information in case of a failure (see err codes in comments above command in module for details). Note also that when using the hardware MSSP SPI option using #option SD_SPI = MSSP, the SPI bus speed is no longer passed to the Init command as a parameter, but is set using an option, e.g. #option SD_SPI_SPEED = spiOscDiv4. As for all options, this must be placed before the Include "SDFileSystem.bas" statement in the user code.

Steven Wright


Licenses for integration of SD/MMC cards into products may be required from Microsoft, IBM, the SD Card Association (for SD Cards) and the Multimedia Association (for MMC Cards).

Microsoft has been granted patents covering the File Allocation Table (FAT) file system. These patents pertain to the integration of long file names with legacy DOS file names in the same data structure. This library does not support long file names, but there may be issues if the Format or QuickFormat command are used.

It is up to the user of this module to ascertain for themselves if licenses are required from any or all of these or other organizations for the integration of the media or this software into their products. In the event licenses are required from any of these or other organization, it is up to the user of this software to acquire, at their own cost, such licenses directly from the license holders.

Version History

A full list of additions and fixes since Version 1 is given below.

  • 1.0.2 13/04/07 - SPI routines changed to allow for silicon errors for some PICS - thanks to Tom Estes
  • 1.0.3 11/09/07 - InitSequence altered to recognise inserted card more quickly - SeekResponse for Cmd 1 reduced To $FF
  • 1.0.4 22/09/07 - SPI altered to use SSPIF rather than SSPStatus - thanks to SteveB on forum -
  • 1.0.5 23/09/07 - SSPSTAT.7 (SMP) changed to 0 - sample at middle
  • 1.0.6 21/10/07 - Calculation of last cluster corrected - effects writing of files when disk full, as well as DiskSizeKB and FreeSpaceKB - previous version overestimated size and free space by one cluster for some cards
  • 1.0.7 23/10/07 - Correction to AppendFile and CloseFile to avoid errors when appending to files of zero length and closing them
  • 2.0.0 24/10/07 - Added OpenFileRW and ability to read and write at file pointer, when file opened with NewFile, OpenFileRW and AppendFile - can use new command FSeek to move file pointer and FilePtr to locate position of file pointer
  • 2.0.1 27/10/07 - DiskMounted altered to return False only if disk not present or Init not previously called - a read/write error no longer causes a False return
  • 3.0.0 26/01/08 - Redone init code to include support for SDHC high capacity cards and added MemoryType to return type of memory card (MMC, SD or SDHC); included code to slow SPI bus to max speed of 400kHz during card initialisation
  • 3.0.1 01/02/08 - Added FATType to return card FAT type (FAT16 or FAT32)
  • 3.0.2 02/02/08 - Added SerialNumber to get the card serial number from the CID register
  • 3.0.3 17/02/08 - Added support for FAT32 - allows use of much larger card capacities
  • 3.0.4 21/02/08 - Added ReadNBytes
  • 3.0.5 22/02/08 - Init altered when using hardware MSSP - SPI speed now set using #option SD_SPI_SPEED and not passed as parameter to Init
  • 3.0.6 24/02/08 - Added Format to re-construct full FAT16/32 card structure and file system - FAT16 or FAT32 chosen automatically depending on card size
  • 3.0.7 24/02/08 - Init return type altered to provide more info
  • 3.0.8 26/02/08 - Added DestroyFormat - USE WITH CARE!
  • 3.0.9 27/02/08 - Added FileCount and DirCount to return number of files or directories in the current subdirectory
  • 3.1.0 28/02/08 - Correction to ChDir routine so that doesn't incorrectly handle "." and ".." entries when passed as a string parameter
  • 3.1.1 29/02/08 - Added #option SD_REINIT_SPI to allow for reinitialising MSSP SPI port before each use, to allow port to be reconfigured for other devices on SPI bus between calls to SD commands
  • 4.0.0 01/03/08 - FindFreeCluster altered to also scan for free clusters at start of FAT to reuse those subsequently made available after file first created
  • 4.0.1 09/03/08 - Added support for multiple simultaneous files with #option SD_SUPPORT_MULTIPLE_FILES - all relevant commands support the initial parameter pFileNumber to direct to specific file (upto 127 files possible, dependent on memory availability)
  • 4.0.2 09/03/08 - Added CloseAll, SaveAll and FreeFile when using multiple file support
  • 4.0.3 10/03/08 - Added optional parameter pRecordLength to OpenFileRW to set length of one record when using file for random access, as well as new functions NewRecord, SetRecord, GetRecord, RecordLength and NumberOfRecords (n.b. first create file with NewFile, then close, followed by opening with OpenFileRW)
  • 4.0.4 13/03/08 - Correction to RmDir to avoid errDirNotEmpty
  • 4.0.5 14/03/08 - Correction to prevent wrong position when using FSeek for position on sector boundary
  • 4.0.6 15/03/08 - Added Truncate to truncate a file from the current file pointer position
  • 4.0.7 20/03/08 - Correction to error in WriteSector that caused some sectors to remain unwritten
  • 4.0.8 20/03/08 - Added CopyFile to copy a source file to a destination file (user must ensure that source file is not open before copying)
  • 4.0.9 21/03/08 - Added support for moving files using MoveFileCut and MoveFilePaste
  • 4.1.0 24/03/08 - Added date & time as optional parameters to SaveFile
  • 4.1.1 28/03/08 - Correction to prevent DiskMounted blocking code if called before Init
  • 4.1.2 04/04/08 - Added overloaded version of QuickFormat to allow setting a Volume Label string (max 11 characters), and an optional parameter to Format to do the same
  • 4.1.3 05/04/08 - Added VolumeLabel and VolumeID commands
  • 4.1.4 08/11/08 - Added #option SD_MSSP to enable use of second MSSP port for PICs with 2 MSSP ports - use #option SD_MSSP = 2


It is suggested that this library is placed in the UserLibrary directory.

Download SDFileSystem Version 4.1.4 »

Download SDFileSystem Version 3.1.1 »

It should also be noted that the revisions are still at the BETA stage. If any problems are found, please raise them via the forum.

Sample Code

Help File

Download SDFileSystem Help File (PDF) »


I would be grateful if you were to post a reply on the Swordfish forum (feedback) if you try the latest version of the module so that I can get a feel for its uptake.