How to interface an SD card with FAT file system using SPI

SD card

The Secure Digital (SD) Card is a non-volatile memory card format developed by the SD Card Association for use in portable devices. It is based on flash memory technology and widely used in digital cameras, cell phones, e-book readers, tablet computers, notebook computers, media players, GPS receivers, and video game consoles. Ever since its adoption in the year 2000, the format has proven very popular and is considered the de-facto industry standard.

The SD card types from introduction until the present day are outlined in Table 1.1 and Table 1.2 .

Table 1.1. SD Card Size


Table 1.2. Card Parameters

2The speed indications in Table 1.2 are orientational for most devices, their exact speed being specified by their belonging to one or other SD speed class.

Most SD cards are sold pre-formatted with a FAT file system on top of an MBR partition scheme as per the above table. The popularity of this file system allows the card to be accessed on virtually any host device with an SD reader.

SD cards are plain block devices and do not in any way imply any specific partition layout or file system thus partition schemes other than MBR partitioning and the FAT file systems can be used. Under Unixlike operating systems such as Linux or FreeBSD, SD cards can be formatted using, for example, the UFS, EXT3 or the ReiserFS file systems; under Mac OS X, SD cards can be partitioned as GUID devices and formatted with the HFS+ file system. Under Windows and some Unix systems, SD cards can be formatted using the NTFS and on later versions exFAT file system. However most consumer products will expect MBR partitioning and FAT16/FAT32 filesystem.

SD Card Access with the EFM32

The present application note deals with the implementation of the SPI-based access mode to read data from / write data to a SDSC (standard SD) card using an EFM32 microcontroller.

Figure 3.1 shows the SD card pinout and Table 3.1 (p. 4) the pin connections for both
SD and SPI modes.

Figure 3.1. SD Card Pinout


In Table 3.1 the type column can have the following:

• I – input
• O – output
• S – supply/ground
• C – control line
• RSV – reserved, not used

Table 3.1. SD Card Pinout Description


Apart from the above matching contacts, SD card connectors may have 2 additional contacts, that signal the presence of an SD card in the socket and whether the card is write protected respectively.

FAT File System

The File Allocation Table (FAT) is a computer file system architecture widely used on many computer systems and most memory cards, because of its relative simplicity. The FAT file system is quite straightforward technically and supported by virtually all existing operating systems for personal computers, which makes it a useful format for flash memory cards and a convenient way to share data between operating systems.

  •  FAT16

The FAT16 format, was introduced in 1987 with the expansion of historical 16-bit cluster addresses to 32 bits. In 1988 this improvement became generally available through MS-DOS 4.0 and OS/2 1.1. The limit on partition size was dictated by the 8-bit signed count of sectors per cluster, which had a maximum power-of-two value of 64. With the standard hard disk sector size of 512 bytes, this gives a maximum of 32 KB clusters, thereby fixing the “definitive” limit for the FAT16 partition size at 2 GB. On magnetooptical media, which can have 1KB or 2 KB sectors instead, this size limit is proportionally larger.

  •  FAT32

Historically, the FAT32 was introduced with Windows 95 OSR2. Cluster values are represented by 32-bit numbers, of which 28 bits are used to hold the cluster number, for a maximum of approximately 268 million clusters. This allows for drive sizes of up to 8 TB with 32 KB clusters, but the boot sector uses a 32-bit field for the sector count, limiting volume size to 2 TB on a hard disk with 512 byte sectors. The maximum possible size for a file on a FAT32 volume is 4 GB, a limit which can be well exceed by computer software today, yet deemed “reasonable” for the usual file types used in SD card (and especially embedded). Files larger than 4 GB require another formatting type such as NTFS.

The above results in NTFS being the most widespread file system format for computer systems, but FAT32 still being considered as the first choice in the applications typical to the use of solid state storage media.

  • The FATFS Library

The FatFs Generic FAT File System Module is a fully free FAT implementation in the form of a library and application interface module, destined to small embedded systems.

The FatFs is written in compliance with ANSI C and completely separated from the disk I/O layer, therefore it is independent of hardware architecture. It can be incorporated into low cost microcontrollers, from 8-bit to 32-bit and various architecture types. The diagram in Figure 4.1 shows how application interfaces the several data storage types using the FATFS library.

Figure 4.1. FATFS library interface diagram


The main features of this library are as follows :

• Windows compatible FAT file system, which is platform independent and easy to port
• Supports FAT12, FAT16 and FAT32 with 2 partitioning rules: FDISK and Super-floppy
• Very small footprint for code and work area
• Has a separate buffer for FAT structure and each file, suitable for fast multiple file access
• Various configuration options:
• Multiple volumes (physical drives and partitions)
• Multiple ANSI/OEM code pages including DBCS
• Long file name support in ANSI/OEM or Unicode
• RTOS support
• Multiple sector size support
• Read-only, minimized API, I/O buffer

The following is the list of functions in the Application Interface FatFs module (i.e. what is exactly implemented to access the SD card):

• f_mount – Register/Unregister a work area
• f_open – Open/Creat a file
• f_close – Close a file
• f_read – Read file
• f_write – Write file
• f_lseek – Move read/write pointer, Expand file size
• f_truncate – Truncate file size
• f_sync – Flush cached data
• f_opendir – Open a directory
• f_readdir – Read a directory item
• f_getfree – Get free clusters
• f_stat – Get file status
• f_mkdir – Create a directory
• f_ulink – Remove a file or directory
• f_chmod – Change attribute
• f_utime – Change timestamp
• f_rename – Rename/Move a file or directory
• f_mkfs – Create a file system on the drive
• f_forward – Forward file data
• f_chdir – Change current directory
• f_chdrive – Change current drive
• f_getcwd – Retrieve the current directory
• f_gets – Read a string
• f_putc – Write a character
• f_puts – Write a string
• f_printf – Write a formatted string

  • The Disk I/O Interface

Since the FatFs module is completely separated from the disk I/O layer, it requires at least the following functions to access the physical media:

• disk_initialize – Initialize disk drive
• disk_status – Get disk status
• disk_read – Read sector(s)
• disk_write – Write sector(s)
• disk_ioctl – Control device dependent features
• get_fattime – Get current time

The low level disk I/O module is not a part of FatFs module so that it must be provided by user – in the case of our application, these are the SPI bus communication routines for the EFM32 (sample drivers are available in the original FatFs resources).

This post is written based on the application note ‘FAT on SD Card.’ Download the full version HERE and read more about the EFM32 software example of how to support FAT file system on the SD card.  

4 thoughts on “How to interface an SD card with FAT file system using SPI

    • I assume you want to use a 5V mcu to talk to a 3.3V SD-card?
      A voltage translation circuit is needed. It would probably work with resistive dividers for the 5V -> 3V direction, but 3V -> 5V direction would probably need transistor/level shifter (some 5V devices might be OK with 3.3V as high-level even if it is < 0.7*Vdd which is typical minimum high level voltage for mcu's.)

      If you do an internet search for "5V SD-card" or "SD-card level shifter" you find many example circuits.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s