Demo:
Library structure:
- SD commands, in sd_spi.c. It is lies on my STM32 SPI library, and implements all of the supported SD SPI commands of all SD's (SDSC, SDHC, SDXC). For every command I heave a separate function. You can read and write raw data on sd card using cmd17ReadSingleBlock() / cmd24WriteBlock() pair after init (sdCardInit()). Also support standard CRC check from crc.c file.
- FAT file system inner functions are in sd_exfat.c. Local init function (mbrDescriptorCheck()) detects the type of file system, reads it MBR and descriptor. Then you can surf file folders with parseFileEntry(uint32_t dirCluster,...)/parseNextFileEntry(). All the data of the detected file is in the file structure (name, size, address, etc).
- Upper level functions (fopen(), fread()), sd.c file. Almost standard file open and read functions with sd speciality. You can open only one file with sdfopen(*name) function. It can find the file in folder with it full name (no 8.3 format restrictions) and it also support russian names, when compiling on linux. This implemented by using custom string compare function, which compares UCS FAT LFN names with UTF8 names of library user. Because of humble resources of the microcontroller, I do not implemented classic fread function. There are situative appropriate custom functions instead:
- fread512(*data) - simply reads next block of the file of 512bytes size. Aligns to the nearest one if the pointer is in middle of block.
- sdfread(*data, size) - reads an array of a certain size, but not bigger than 512 bytes.
- freadNextByte(), fread16bit(), fread32bit() - reads the next unsigned char, unsigned short or unsigned int variable from file.
- fskipNextBytes(n) - sets the pointer of a file in ptr+n position, for skipping unuserfull data.
- sdfgetpos() - only one function which works like classic fgetpos() function from stdlib.
Mikhail Belkin