TM STM32F4xx Libraries  v1.0.0
Libraries for STM32F4xx devices from Tilen Majerle

FATFS implementation for STM32F4xx devices - http://stm32f4-discovery.com/2014/07/library-21-read-sd-card-fatfs-stm32f4xx-devices/ - http://stm32f4-discovery.com/2014/08/library-29-usb-msc-host-usb-flash-drive-stm32f4xx-devices. More...

Modules

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

Detailed Description

FATFS implementation for STM32F4xx devices - http://stm32f4-discovery.com/2014/07/library-21-read-sd-card-fatfs-stm32f4xx-devices/ - http://stm32f4-discovery.com/2014/08/library-29-usb-msc-host-usb-flash-drive-stm32f4xx-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: 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       STM32F4XX       DESCRIPTION             |NAME   STM32F4XX   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_stm32f4_fatfs.h
- tm_stm32f4_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_stm32f4_fatfs.h
- tm_stm32f4_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 Write protect and Card detect pins. This two pins are by default on pins below.

They are the same for any communication used, and are disabled by default.

NAME    STM32F4XX   DESCRIPTION
    
WP      PB7         Write protect pin. Pin low when write is enabled
CD      PB6         Card detect pin. Pin low when card detected

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_USE_DETECT_PIN_PORT          GPIOB
#define FATFS_USE_DETECT_PIN_PIN           GPIO_PIN_6

//Default WP pin            
#define FATFS_USE_WRITEPROTECT_PIN_PORT    GPIOB
#define FATFS_USE_WRITEPROTECT_PIN_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
}

Version v1.2

Added support for USB MSC HOST interface with FatFS library. More about, how to configure USB MSC HOST to work properly, you should take a look at my library tm_stm32f4_usb_msc_host.h. There is also default pinout for USB and other stuff.

I will explain here, how to properly set FatFS library to work with USB.

Files, needed for USB communication and FatFS:

- tm_stm32f4_fatfs.h
- tm_stm32f4_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

After you have your files included in project, open project's defines.h file and add line below:

//Enable USB in FatFS library
#define FATFS_USE_USB      1

This will enable USB functions to work with FatFS module. Also, when you make define 2 lines above, SD card settings become inactive.

If you want to use SD card and USB storage at the same time for some reason, you have to enable SD card functionality back. Add lines below in your defines.h file:

//Enable SDIO communication for SD card
#define FATFS_USE_SDIO      1

//Enable SPI communication for SD card
#define FATFS_USE_SDIO      0

And also, you should know, that USB has 1: partition and SD card has 0: partition. So when you mount SD card, you should use:

//Mount SD card
f_mount(&sd_fs, "0:", 1);

//Mount USB storage
f_mount(&usb_fs, "1:", 1);

This allows you to copy data from one SD card to USB and back too and use of 2 different physical drives at the same time.

FatFS with SDRAM

As of version 1.7, FATFS can now be used with SDRAM memory on STM32F429-Discovery or STM324x9-EVAL board.

To enable this feature, you will have to open defines.h file and add define:

//Enable SDRAM
#define FATFS_USE_SDRAM   1
Note
After that, again, SDCARD will be disabled by default, if you want to use it too (with SDCARD together) you have to do the same as you have to do when you use USB with FATFS, otherwise, if you will try to read/write from SDCARD, you will get errors.

Files, you will need for running SDRAM driver are:

- tm_stm32f4_fatfs.h
- tm_stm32f4_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_sdram.h
- fatfs/drivers/fatfs_sdram.c
- tm_stm32f4_sdram.h
- tm_stm32f4_sdram.c
Note
SDRAM is not a memory which holds data when power is off. For that reason, you will have to use f_mkfs() function to create file system object on your SDRAM drive. This will have to be done when you initialize your SDRAM or when you first time try to mount from SDRAM. Otherwise, you will not be able to interface with FATFS based functionality like SDCARD can do.
FatFS with SPI FLASH

This option is added to implementation, but low level drivers are not implemented yet.

Note
If you will try to read from SPIFLASH using f_mount() function, you will get errors.
New names for drivers

Before version 1.7, SDCARD and USB drivers were supported only with this library.

If you want to use them both at a time, you always have to keep in mind to use different numbers for drivers, for example (notice number in the second parameter):

//Mount SDCARD
f_mount(&fsSD, "0:", 1);
//Mount USB 
f_mount(&fsUSB, "1:", 1);

As of version 1.7, you can now use the same as before, or like that:

//Mount SDCARD
f_mount(&fsSD, "SD:", 1);
//Mount USB 
f_mount(&fsUSB, "USB:", 1);

If you want to open file, fox example on USB, you can do like this:

//Open file on USB key
//Mount first
f_open(&fil, "USB:my_file.txt", FA...);
All supported names

Below is a list of all supported strings for volume numbers:

Example for opening files:

//Open file on SDRAM
f_open(&fil, "SDRAM:my_file_sdram.txt", FA...);
//Open file on SDCARD
f_open(&fil, "SD:my_file_sd.txt", FA...);
//and more files
Example of using all 4 possible drivers in your project

To use all available data sources (physical drivers) for FATFS, you will have to open defines.h file, and add these defines:

//Enable all possible FATFS sources implemented in library

//Enable USB
#define FATFS_USE_USB       1
//Enable SDRAM
#define FATFS_USE_SDRAM     1
//Enable SPI flash
#define FATFS_USE_SPIFLASH  1 //Not implemented yet

//SDCARD is enabled by default, but when you enable anything else too, SDCARD is disabled
//If you want to enable it too, you have to tell that!
#define FATFS_USE_SDIO      1 //Set to 0 if you want to use SDCARD with SPI
Changelog
 Version 1.7
  - April 30, 2015
  - Added support for SDRAM as FATFS drive
  - Prepared files (not implemented yet) for SPI flash driver with FATFS
  
 Version 1.6
  - March 11, 2015
  - Added support for my new GPIO library

 Version 1.5
  - February 17, 2015
  - FatFs R0.11 supported
  - Fixed some problems when using SDIO

 Version 1.4
  - December 29, 2014
  - Support for truncate file from beginning

 Version 1.3
  - December 06, 2014
  - FatFs R0.10C supported

 Version 1.2
  - August 28, 2014
  - Added support for USB

 Version 1.0
  - First release
Dependencies
 - STM32F4xx
 - STM32F4xx RCC
 - STM32F4xx GPIO
 - STM32Fx44 SPI    (only when SPI)
 - STM32F4xx DMA    (only when SDIO)
 - STM32Fx44 SDIO   (only when SDIO)
 - MISC             (only when SDIO)
 - defines.h
 - TM SPI           (only when SPI)
 - TM DELAY         (only when SPI)
 - TM GPIO
 - TM SDRAM         (only when SDRAM)
 - FatFS by Chan