HAL Library 20- FATFS for STM32Fxxx

FATFS library (HAL LIB 20) is a “generic” library for all FAT related implementations, such as SDCARD, USB FLASH, SPI FLASH and also SDRAM can be used with proper FAT initialization.

My FATFS library currently supports only SDCARD communication with SDIO (STM32F4xx) or SDMMC (STM32F7xx) or SPI on both families. There is no big difference between them and you can treat them as the same peripheral with only different name.

In case you are interested for FATFS based on STD libraries for F4xx series, check here.

Library

Read more about new HAL libraries

Features

  • Interface to SDCARD using FATFS by Chan, version 0.11
    • Use SDIO for communication
    • Use SPI for communication
  • Support for CARD DETECT pin
  • Interface with USB MSC HOST
  • Interface with SDRAM (Coming)

Dependencies

  • HAL
  • TM
    • STM32Fxxx HAL
    • defines.h
    • FATFS
    • TM SPI when SPI is used as SDCARD interface
    • TM DELAY when SPI is used as SDCARD interface
    • TM GPIO

FATFS

First a little bit about FATFS library and its configuration. I’ve reconfigure some of default settings for FATFS, which can be set in ffconf.h file for fatfs. Main changes are these:

  1. Dynamic allocation is used when new file should be created. You need at least 512 bytes of free memory in HEAP region for malloc to allocate data, otherwise, f_open function will return FR_NOT_ENOUGH_CORE result. You can force FATFS to use static buffer for all files (not thread safe) or to use stack for files, but you have to increase stack to allow that or you will have stack overflow. Settings can be changed in ffconf.f file from FATFS library.
  2. Instead of numbers for logical drivers (which will come in future with USB and other settings), I’ve configured FATFS in settings for nice logical drives name. So, instead of mount SDCARD with
    1. f_mount(&fatfs, “0:”, 1), you can do it now with
    2. f_mount(&fatfs, “SD:“, 1), which increases transparency between multiple FATFS related low level devices. Other implementations are listed below.
    3. For USB MSC: f_mount(&fatfs, “USBHS:“, 1); for USB HOST in HS mode.
    4. For USB MSC: f_mount(&fatfs, “USBFS:“, 1); for USB HOST in FS mode.
    5. For SDRAM: f_mount(&fatfs, “SDRAM:”, 1); for SDRAM implementation. This option does not work yet.
    6. Check example for more info how to use this aliases.

SDCARD

SDCARD pinout

SDCARD pinout

Library supports 2 modes of communication with SDCARD. First, which is faster and better is SDIO/SDMMC, or second, slower communication with SPI. You can see default pinouts in table below for SDCARD.

NR SDIO Interface SPI Interface
Name STM32Fxxx Description Name STM32Fxxx Description
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
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

There is also CARD detect pin on SDCARD connector if you use it. By default, this feature is disabled in library, but can easily be enabled. In case, you want CARD DETECT pin in your project, you can open defines.h file and define your settings for CARD DETECT pin. Default CD pin is PB6 when pin is active, but can easily be changed.

SDIO/SDMMC implementation

Default SDCARD communication is SDIO/SDMMC and is automatically enabled if settings are not overwritten. For getting SDIO/SDMMC properly to work, you have to add these files in your project.

Default mode for SDIO/SDMMC is 4-bit mode, but in case you want to save some GPIO pins, you can use 1-bit also. For configuration, open defines.h file and add/edit settings you need.

SPI implementation

If you need (for come case) SPI implementation instead of SDIO (I don’t prefer SPI when SDIO can be used), you have to manually enable SPI for FATFS. To do this, open defines.h file and add following lines.

To get SPI into working state, you will have to add these files into project.

USB MSC HOST

USB MSC Host is also supported with FATFS. The 2 things are there you need to know in order to use FATFS with MSC host:

  1. You need to add low_level driver, from fatfs/drivers folder, driver is fatfs_usb.c
  2. Check examples for USB MSC Host on Github to see how to use it when you have HS mode and when you have FS mode.

Note: if you won’t include low level driver file, you will not get any compile errors, because I have defined functions for low-level driver with __weak parameter. You will just always get errors from FATFS related functions.

To run USB MSC Host, you will also need my USB library with HOST stack.

SDRAM

Not in working state yet. Coming soon!

SPI FLASH

Not in working state yet. Coming soon!

Functions and enumerations

Example

Example below is basic and shows how to write data to SDCARD. Example was tested with STM32F7-Discovery board and STM32F429-Discovery board and I got expected result!

In this example, default configuration is used, so:

  • SDIO/SDMMC interface
  • Interface has 4-bit mode
  • Card detect pin is disabled

Project is available on my Github account, download all libraries below.

tilz0R

Owner of this site. Application engineer, currently employed by STMicroelectronics. Exploring latest technologies and owner of different libraries posted on Github.

You may also like...

Read before commenting!

Before you make a new comment, make sure you agree with things listed below:

  • - Read post to make sure if it is already posted what you are asking for,
  • - Make sure you have the latest version of libraries used in your project,
  • - Make a clean and grammatically correct written message,
  • - Report as many details as possible, including what have you done so far,
  • - Do NOT post any code here. Use Pastebin,
  • - Do NOT post any error codes here. Use Pastebin,
  • - Specify STM32Fxxx family and used Discovery/EVAL/Nucleo or custom made board,
  • - Make sure your clock is set correct for PLL,
  • - If you are using my HAL drivers, please check this post how to start.
Comment will be deleted on breaking these rules without notification!