Close
0%
0%

SD card SPI STM32 library

Up to 2GB FAT16 SD card file reading (FAT32/exFAT read/write earlier).

Public Chat
Similar projects worth following
0 followers
Yet another implementation, based on actual version of official SD standards. In order to keep up to the documentation entirely, I needed a bigger amount of files than in arduino SDFS. So, the resulting library structure seems to be more complicated, but at fact, it is easier to review every little level of protocol separately.
Macros and commands for all types of cards (up to SDXC) are ready. But, unfortunately, FAT32 (SDHC), exFAT (SDHC) so far in TODO list. On the other side I already have a amazing demo with 160x128 jpeg pictures slideshow, readed from 2GB flash.

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.

  • SD JPEG demo

    Mikhail Belkin23 minutes ago 0 comments

    About library usage and demo. For using SD library only, download sd_fat folder and stm32 folder from registerHeap repository, use makefile in sd folder for project building. For building a demo, add st7735 folder, and build the project from makefile in this folder.

    This demo is written for st7735 160x128 display, and can open only native resolution (160x128) jpeg files. If you wold try it demo, you can add your files on SD card and open them through showJpeg(char *fileName) function in main of st7735 folder.

View project log

Enjoy this project?

Share

Discussions

Does this project spark your interest?

Become a member to follow this project and never miss any updates