TM STM32Fxxx HAL Libraries  v1.0.0
Libraries for STM32Fxxx (F0, F4 and F7 series) devices based on HAL drivers from ST from Tilen Majerle

FATFS implementation for STM32Fxxx devices - More...


 Library defines.
 Library typedefs.
 Library Functions.

Detailed Description

FATFS implementation for STM32Fxxx devices -

FatFs implementation for STM32F4xx devices

This library uses Chan's Fatfs implementation.

This library is only for communication. To work with files, you have to look at Chan's FatFs manual, link below:

You can work with SPI or SDIO protocol to interface SDCARD.

SDCARD pinouts

Library works with SPI or SDIO mode. Also, when in SDIO mode, you can set to 1- or 4-bit mode. By default, SDIO with 4-bit mode is used, so you will look into right column on table below.


  / 1 2 3 4 5 6 7 8 |  NR   |SDIO INTERFACE                               |SPI INTERFACE
 /                  |       |NAME     STM32Fxxx     DESCRIPTION           |NAME   STM32Fxxx   DESCRIPTION 
/ 9                 |       |         4-BIT  1-BIT                        | 
|                   |       |                                             | 
|                   |   1   |CD/DAT3  PC11   -      Connector data line 3 |CS     PB5         Chip select for SPI 
|                   |   2   |CMD      PD2    PD2    Command/Response line |MOSI   PA7         Data input for SPI 
|                   |   3   |VSS1     GND    GND    GND                   |VSS1   GND         GND 
|   SD CARD Pinout  |   4   |VDD      3.3V   3.3V   3.3V Power supply     |VDD    3.3V        3.3V Power supply 
|                   |   5   |CLK      PC12   PC12   Clock                 |SCK    PA5         Clock for SPI 
|                   |   6   |VSS2     GND    GND    GND                   |VSS2   GND         GND 
|                   |   7   |DAT0     PC8    PC8    Connector data line 0 |MISO   PA6         Data output for SPI 
|                   |   8   |DAT1     PC9    -      Connector data line 1 |-      -           - 
|___________________|   9   |DAT2     PC10   -      Connector data line 2 |-      -           - 
SDIO Communication

By default, SDIO with 4-bit communication is used. If you want to use SDIO 1-bit communication, set defines below in your defines.h file:

//Set SDIO with 1-bit communication
#define FATFS_SDIO_4BIT 0

For SDIO communication, you will need at least these files:

- tm_stm32_fatfs.h
- tm_stm32_fatfs.c
- fatfs/diskio.h
- fatfs/diskio.c
- fatfs/ff.h
- fatfs/ff.c
- fatfs/ffconf.h
- fatfs/integer.h
- fatfs/option/syscall.c
- fatfs/option/unicode.c
- fatfs/drivers/fatfs_sd_sdio.h
- fatfs/drivers/fatfs_sd_sdio.c
SPI Communication

Or, if you want to use SPI communication, you have to add lines below in your defines.h file

//Enable SPI communication, disable SDIO
#define FATFS_USE_SDIO 0

Files, needed for SPI:

- tm_stm32_fatfs.h
- tm_stm32_fatfs.c
- fatfs/diskio.h
- fatfs/diskio.c
- fatfs/ff.h
- fatfs/ff.c
- fatfs/ffconf.h
- fatfs/integer.h
- fatfs/option/syscall.c
- fatfs/option/unicode.c
- fatfs/drivers/fatfs_sd.h
- fatfs/drivers/fatfs_sd.c
Overwriting default pinout

SDIO interface pins are fixed, and can not be changed. If you want to change SPI pins, you have to set these defines in your defines.h file:

//Set your SPI, for corresponding pins look at TM SPI library
#define FATFS_SPI SPI1
//Set your CS pin for SPI
Write protect and Card detect pins

Library has support for Card detect and Write protect pins.

It is the same for any communication used and is disabled by default.

CD      PB6         Card detect pin. Pin low when card detected
CD      PB7         Card write protect pin. Pin low when card write is enabled

Like I said before, these 2 pins are disabled by default. If you want to use it, you have to add 2 lines in your defines.h file:

//Enable Card detect pin
//Enable Write protect pin

WP and CD pins are now enabled with default configuration.

Add lines below to your defines.h file only if you want to overwrite default pin settings:

//Default CD pin
//Default WP pin
Timing function for files

FatFs uses function get_fattime() for time, to set timestamp for files, when they were edited or created.

By default, function returns 0, but if you want to create your own function, you have to set defines in defines.h file:

//Use custom get_fattime() function

And then somewhere in your project, add function like below:

//Use custom get_fattime function
//Implement RTC get time here if you need it
DWORD get_fattime (void) {
//Get your time from RTC or something like that
return ((DWORD)(2014 - 1980) << 25) // Year 2014
| ((DWORD)7 << 21) // Month 7
| ((DWORD)10 << 16) // Mday 10
| ((DWORD)16 << 11) // Hour 16
| ((DWORD)0 << 5) // Min 0
| ((DWORD)0 >> 1); // Sec 0
Use FATFS with USB MSC Host library.

You can use this library also with TM_USBH_MSC Library if you want to ready USB Flash keys when needed.

First, files you will need for implementation based on FATFS are:

- tm_stm32_fatfs.h
- tm_stm32_fatfs.c
- fatfs/diskio.h
- fatfs/diskio.c
- fatfs/ff.h
- fatfs/ff.c
- fatfs/ffconf.h
- fatfs/integer.h
- fatfs/option/syscall.c
- fatfs/option/unicode.c
- fatfs/drivers/fatfs_usb.h
- fatfs/drivers/fatfs_usb.c
- Entire USB MSC Host stack and TM USB library
Operate with USB MSC Host libraries.

Because my USB MSC Host library supports both USB modes at the same time, I've made 2 different names for logical drivers. So, when you wanna mount/read/write/etc, you have 2 possible drive names:

//Mount USB Flash memory connected on USB FS port
f_mount(&fatfs_FS, "USBFS:", 1);
//Mount USB Flash memory connected on USB HS port
f_mount(&fatfs_HS, "USBHS:", 1);

Like SDCARD has "SD:" name, here are 2 different names, which allows you flexibility in your code. This also means, that you can use SDCARD and 2 USB flash drives at the same time without any problems, just specifying drive name when performing read and write operations.

Search for files

I've added support for easy search function for files/folders on your FATFS related device. It works with any device (SDCARD, USB, etc), because you specify starting path for search which is specified by FATFS.

There are 2 functions for search, one is TM_FATFS_Search which you should call when you want to do a search operations in your FATFS structure

Second is TM_FATFS_SearchCallback which is called anytime file/folder is found on system so user can do his job with file.

int user_func(void) {
//Create working buffer
char working_buffer[200];
TM_FATFS_Search_t FindStructure;
//mount first
if ((res = TM_FATFS_Search("SD:", working_buffer, sizeof(working_buffer), &FindStructure)) == FR_OK) {
//Search was OK
} else if (res == FR_NOT_ENOUGH_CORE) {
//Not enough buffer memory for full search operation
uint8_t TM_FATFS_SearchCallback(char* path, uint8_t is_file, TM_FATFS_Search_t* FindStructure) {
//Check for file/folder
if (is_file) {
printf("File: %s", path);
} else {
printf("Folder: %s", path);
//Allow next search
return 1;

Check documentation for these 2 functions for more info.

 Version 1.1
  - April 24, 2016
  - Added support for FATFS R0.12
  - Added support for SPI DMA when using fatfs in SPI mode on STM32F4xx and STM32F7xx devices

 Version 1.0
  - First release
 - STM32Fxxx HAL
 - defines.h
 - attributes.h
 - TM SPI           (only when SPI)
 - TM SPI DMA       (only when SPI on STM32F4xx and STM32F7xx)
 - TM DELAY         (only when SPI)
 - FatFS by Chan    (R0.12a)