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 - http://stm32f4-discovery.com/2015/08/hal-library-20-fatfs-for-stm32fxxx/. More...

Modules

 TM_FATFS_Macros
 Library defines.
 
 TM_FATFS_Typedefs
 Library typedefs.
 
 TM_FATFS_Functions
 Library Functions.
 

Detailed Description

FATFS implementation for STM32Fxxx devices - http://stm32f4-discovery.com/2015/08/hal-library-20-fatfs-for-stm32fxxx/.

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: http://elm-chan.org/fsw/ff/00index_e.html

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.

SD CARD PINS

   _________________
  / 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
#define FATFS_SPI_PINSPACK TM_SPI_PinsPack_1
//Set your CS pin for SPI
#define FATFS_CS_PORT GPIOB
#define FATFS_CS_PIN GPIO_PIN_5
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.

NAME    STM32Fxxx   DESCRIPTION
    
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
#define FATFS_USE_DETECT_PIN 1
//Enable Write protect pin
#define FATFS_USE_WRITEPROTECT_PIN 1

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
#define FATFS_DETECT_PORT GPIOB
#define FATFS_DETECT_PIN GPIO_PIN_6
//Default WP pin
#define FATFS_WRITEPROTECT_PORT GPIOB
#define FATFS_WRITEPROTECT_PIN GPIO_PIN_7
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
#define FATFS_CUSTOM_FATTIME 1

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];
FRESULT res;
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
}
//unmount
}
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.

Changelog
 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
Dependencies
 - STM32Fxxx HAL
 - defines.h
 - attributes.h
 - TM DMA
 - TM SPI           (only when SPI)
 - TM SPI DMA       (only when SPI on STM32F4xx and STM32F7xx)
 - TM DELAY         (only when SPI)
 - TM GPIO
 - FatFS by Chan    (R0.12a)