Showing posts with label FAT. Show all posts
Showing posts with label FAT. Show all posts

Thursday, July 7, 2022

SD Memory Card Library for AVR Microcontrollers - SD and FAT Driver

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

  1. SD Card Pins
  2. Schematic Interface
  3. Return Values
  4. File Object Structures
  5. File names
  6. The Buffer, the Writing and the Flush
  7. Code Examples
  8. Library Configuration
  9. Library Usage
    1. Volume Management
      1. Card Detect
      2. Card Initialization
      3. Volume Free Space
      4. Volume Capacity
      5. Read Volume Label and Serial Number
    2. Directory Access
      1. Make Directory
      2. Open Directory
      3. Open a Directory by Index
      4. Go Back
      5. Find by Index
      6. Find Next
      7. Count Items
    3. File Access
      1. Make File
      2. Delete File or Folder by Index
      3. Delete File or Folder by Name
      4. Open File
      5. Open File by Index
      6. Set File Pointer
      7. Seek File End
      8. Get File Pointer
      9. Write Float Number
      10. Write Number
      11. Write String
      12. Write File
      13. Truncate File
      14. Flush Data
      15. Read File
      16. End of File Check
      17. Error Flag Check
      18. Clear Error Flag
      19. Get Filename
      20. Get Index
      21. Get File Size
      22. Get Write Date and Time
      23. Get File Attributes
      24. Setting File Date and Time
  10. 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.