Professional Documents
Culture Documents
on STM32F4
BY TILZ0R JULY 10, 2014
Finally I got it working properly. Reading SDcard was very awesome
for me for first time, so I decided to make a library for STM32F4xx
devices and post it to my website. Its basically just communication
interface between STM32F4 and FatFS library by Chan. It
supports SPI or SDIO communication. Library also supports USB
communication, but for that purpose you need USB stack too. USB is
available here.
Below Im showing how to setup everything with STM32F4 to get SD
card working with SPI or SDIO communication.
FatFS Library
If you are interested for FATFS using HAL drivers, check here.
Features
Fatfs by Chan supported, version R0.11
Support for SDIO or SPI interface
SDIO works with 1- or 4-bit mode
Support for Card detect pin
Can be disabled
Support for Write protect pin
Can be disabled
Custom fat time
Enables you to implement your own time using RTC
Updates
Version 1.2 August 29, 2014
Supported for USB devices
Version 1.3 December 06, 2014
FatFs version R0.10C now implemented
Version 1.4 December 29, 2014
Added support for truncate file from beginning
Version 1.5 February 17, 2015
FatFs version R0.11 now implemented
Fixed problem which appers sometime using SDIO
Dependencies
CMSIS
STM32F4xx
STM32F4xx RCC
STM32F4xx GPIO
STM32F4xx SPI (only when SPI)
STM32F4xx DMA (only when SDIO)
STM32F4xx SDIO (only when SDIO)
MISC (only when SDIO)
TM
TM SPI (only when SPI)
Download
TM DELAY (only when SPI)
TM STM32F4 Delay Library 27840 downloads 10.12 KB
Download
TM GPIO
Download
defines.h
Download
attributes.h
Pinout
SDCard pinout
STM32F4x
x
4- 1- Nam STM32F4x
NR Name bit bit Description e x Description
Card detect and write protect pins are not part of SD card, but part
of SDcard connector. They are listed below, and are same for both
communications:
By default, both pins are disabled in library. If you want to use them,
you have to enable them in defines.h file
1
2
/* Use detect pin */
3
#define FATFS_USE_DETECT_PIN 1
4
/* Use writeprotect pin */
5
#define FATFS_USE_WRITEPROTECT_PIN 1
6
7
/* If you want to overwrite default CD pin, then change this settings */
8
#define FATFS_USE_DETECT_PIN_PORT GPIOB
9
#define FATFS_USE_DETECT_PIN_PIN GPIO_PIN_6
1
0
/* If you want to overwrite default WP pin, then change this settings */
1
#define FATFS_USE_WRITEPROTECT_PIN_PORT GPIOB
1
#define FATFS_USE_WRITEPROTECT_PIN_PIN GPIO_PIN_7
1
2
SDIO Communication
STM32F4xx has internal SDIO peripheral to work with SD cards. Also,
SDIO communication is faster than SPI, but if you dont need speed
in your project, you can use SPI aswell. With default settings, SDIO
4-bit mode communication is used. It means, that you need all four
data lines. SDIO also supports 1-bit mode, where you need only
Data0, CMD and CLK pins. To activate 1-bit mode, add this to your
defines.h file:
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
Fatfs
To work with files, you have to use fatfs library. In order to get it
work properly, you should check its manual here. My library is used
only for behind the scenes settings, to get communication with SD
card properly.
Custom get_fattime() function
Fatfs supports get_fattime() function, which returns time in integer
format. By default get_fattime() returns time 0. That means
1.1.1980 00:00:00 time. If you have any RTC library or you want to
set your own time, then you should set define in defines.h file:
1
//Use custom get_fattime function
2
//Implement RTC get time here if you need it
3
DWORD get_fattime (void) {
4
return ((DWORD)(2014 - 1980) << 25) // Year 2014
5
| ((DWORD)7 << 21) // Month 7
6
| ((DWORD)10 << 16) // Mday 10
7
| ((DWORD)16 << 11) // Hour 16
8
| ((DWORD)0 << 5) // Min 0
9
| ((DWORD)0 >> 1); // Sec 0
1
}
0
My libraries for RTC
Internal on STM32F4xx
DS1307 I2C for STM32F4xx
Changelogs
Fixed error for SPI communication
TM_FATFS_DriveSize
Get full drive size and free space on your disk
TM_FATFS_TruncateBeginning
Truncate file from beginning
1 /**
2 * Get SD card drive size
3 *
4 * Parameters:
5 * - uint32_t* total:
6 * Pointer to variable to store total size of card
7 * - uint32_t* free:
8 * Pointer to variable to store free space on card
9 *
1 * Returns FRESULT struct members. If data are valid, FR_OK is returned.
0 */
1 FRESULT TM_FATFS_DriveSize(uint32_t* total, uint32_t* free);
1
1 /**
2 * Get SD card drive size
1 *
3 * Parameters:
1 * - uint32_t* total:
4 * Pointer to variable to store total size of card
1 * - uint32_t* free:
5 * Pointer to variable to store free space on card
1 *
6 * Returns FRESULT struct members. If data are valid, FR_OK is returned.
1 */
7 FRESULT TM_FATFS_USBDriveSize(uint32_t* total, uint32_t* free);
1
8 /**
1 * Truncate beginning of file
9 *
2 * Parameters:
0 * - FIL* fil:
2 * Pointer to already opened file
1 * - uint32_t index:
2 * Number of characters that will be truncated from beginning.
2
2
3
2
4
2
5
2
6
2
7
2
8
2
9
3
0
3
* If index is more than file size, everything will be truncated, but file will not be
1
deleted
3
*
2
* Example
3
* - You have a file, it's content is: "abcdefghijklmnoprstuvwxyz",
3
* - You want to truncate first 5 bytes,
3
* - Call TM_FATFS_TruncateBeginning(&opened_file, 5);
4
* - You will get new file data: "fghijklmnoprstuvwxyz"
3
*
5
* Returns FRESULT struct members. If everything ok, FR_OK is returned.
3
*/
6
FRESULT TM_FATFS_TruncateBeginning(FIL* fil, uint32_t index);
3
7
3
8
3
9
4
0
4
1
4
2
4
3
4
4
4
5
Example
I have test this example with SPI and SDIO interface. In both cases I
got the same result. I used 2 and 8GB cards in FAT16 format. In
example below, default settings are in use, so:
SDIO interface
Card detect OFF
Write protect OFF
Led status
No leds on: It has not been mounted ok
Check if pinout is correct, also make sure FAT16 is
selected
Only GREEN led on: mounted ok and opened ok, but data
could not be written
Only RED led on: mounted OK, but could not open file
Maybe you have enabled Write protect pin. Disable it
for this example
Both ON: everything OK
Download
25317 downloads551.30 KB