This is a library for reading and writing of SD flash memory cards using FAT16 or FAT32 file systems, over the SPI protocol, and is designed with embedded systems in mind. It includes an SD card driver that uses the SPI interface and a FAT driver that is controlled by the host microcontroller.
There are two main standards for flash memory cards: MMC (MultiMediaCards) and SD (Secure Digital), SD being the most used today. SD was developed by the SD Association (SDA) as an improvement over MMCs. These cards have basically a flash memory array and a microcontroller inside that controls erasing, reading, writing, error controls, and wear leveling of the flash array. The data is transferred between the memory card and the host controller as data blocks in units of 512 bytes.
The recommended file systems for memory cards are FAT12/16/32 or exFAT. Maximum volume sizes for each one are: 256MB, 4GB, 16TB* for FAT32 and 128PB for exFAT.
*Windows will refuse to format cards over 32GB using FAT32, offering exFAT
and NTFS as an option but there are workarounds and third-party software
that can do that.
FAT16 | FAT32 | |||
---|---|---|---|---|
Flash | RAM | Flash | RAM | |
Read | 6.1k | 605 | 6.6k | 607 |
Write | 7.8k | 607 | 8.6k | 609 |
Read/Write | 7.9k | 607 | 8.7k | 609 |
Some rounded up values of the code footprint depending on the functions
used: for reading, writing or both.
RAM size is expressed in bytes and it includes 512 bytes for the
read/write buffer.
Features
- Communication protocol: SPI
- Supported memory cards: SD cards (MMC's are not implemented)
- Includes SD driver: Yes
- Supported file systems: FAT16 and FAT32
- Support for LFN (Long File Names): Yes (up to 255 characters)
- Formatting utility: No
- Multiple files and folders instances: Yes
- Ability to create folders and directories
- Delete files: Yes, with recursive removal of files and sub-directories
- FSInfo (FAT32): not implemented in order to reduce the code size. The only situations when this could be a downside is when querying for free space, creating files/folders or expand them since this is when it is necessary to search for a free cluster. If the card is mostly empty, even if it has a large capacity, the search for a free cluster will be very quick. As the card is filled it will take longer (few seconds).
Contents
- SD Card Pins
- Schematic Interface
- Return Values
- File Object Structures
- File names
- The Buffer, the Writing and the Flush
- Code Examples
- Library Configuration
-
Library Usage
- Volume Management
- Card Detect
- Card Initialization
- Volume Free Space
- Volume Capacity
- Read Volume Label and Serial Number
- Directory Access
- File Access
- Make File
- Delete File or Folder by Index
- Delete File or Folder by Name
- Open File
- Open File by Index
- Set File Pointer
- Seek File End
- Get File Pointer
- Write Float Number
- Write Number
- Write String
- Write File
- Truncate File
- Flush Data
- Read File
- End of File Check
- Error Flag Check
- Clear Error Flag
- Get Filename
- Get Index
- Get File Size
- Get Write Date and Time
- Get File Attributes
- Setting File Date and Time
- Download SD Card Library
SD Card Pinout
Both MMC/SD standards have their own proprietary protocols but they also support SPI which can be selected during card initialization. Since microcontrollers have SPI integrated hardware, this is the most used interface for memory cards.