The data area of MMC and SD memory cards is accessed through the use of a 512 byte sector buffer. All data read and write operations work through the reading and writing a 512 byte block of sector data. Therefore to modify a single byte, a complete sector of data must be read to local ram, modified and then the complete sector written back to the card.
Other flash memory devices, such as flash memory IC’s also typically use the same system whereby a complete block of data must be erased to reset all of the bytes in that block back to 0xFF ready for writing again, as many flash memory technologies work on the principal of turning individual bits from high bits to low, not low to high.
This 512 byte buffer is an issue when it comes to designing a driver to provide fast read and write access. The reason is that as a programmer you want to be able to access individual bytes of a file without worrying about sectors, but you don’t want the driver continuously reading and writing 512 bytes of data every time you modify a byte, resulting in painfully slow access. This driver overcomes these problems by only reading and writing when an operation needs to access a byte that is contained in a different sector on the card. Whilst this requires some instances of quite complex driver code, this complexity is worthwhile due to the massive speed improvements this approach provides.
If you want to gain an understanding of exactly how the driver works then this manual contains a thorough description of the layout of FAT based MMC / SD cards. Once you understand this each of the driver functions are relatively easy to understand. However you don’t need to do this and if you just want to read and write FAT16 or FAT32 MMC or SD cards then you can skip these in-depth parts of the manual.
Finally you should also note that different MMC / SD memory cards can take different amounts of time to complete internal operations, such as preparing to read or writing a new sector of data. If your application is very time sensitive you may need to consider using some processor RAM memory to act as some sort of FIFO buffer for read and write operations. For example say you are designing a MP3 player that needs to send MP3 file data to a MP3 decoder IC within a certain response time when it requests it. You may find that a slow MMC or SD card might not be able to provide the next byte of data fast enough when it moves from one sector to the next, resulting in your MP3 decoder IC temporarily running out of data. By using some form of circular FIFO RAM buffer in your application you could read data from the MMC or SD card as one process, always trying to fill the data buffer so its full, and read data from the buffer to send to the MP3 decoder IC when it requests it as a separate interrupt based process.


