This project facilitates the reading and writing of SD flash memory cards
using FAT16 or FAT32 file systems, over the SPI protocol, and it is designed
with embedded systems in mind. It includes an SD driver that uses the SPI
interface and a FAT driver that is controlled by the host microcontroller.
If you whish to know about the low level communication between the microcontroller and the SD card, I have a two part series tutorial here https://www.programming-electronics-diy.xyz/2022/07/sd-card-tutorial-interfacing-sd-card.html.
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 this.
|
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
- CRC: not yet
-
Multiple files and folders instances: Yes
-
Ability to create folders and directories
-
Delete files: No (only file truncation is implemented)
-
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
- Functions and their usage
- Volume Management
- Directory Access
- File Access
- 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.