Đang Online : 2
Hôm nay : 6
Hôm qua : 186
Tổng truy cập : 286260
Sat 23/04/2016 | 11:39 GMT+7
by tilz0R · August 3, 2015
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.
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:
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.
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.
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 is also supported with FATFS. The 2 things are there you need to know in order to use FATFS with MSC host:
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.
Not in working state yet. Coming soon!
Not in working state yet. Coming soon!
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
/**
* Keil project for SDCARD reader using SDIO example
*
* Before you start, select your target, on the right of the "Load" button
*
* @author Tilen Majerle
* @email tilen@majerle.eu
* @website http://stm32f4-discovery.com
* @ide Keil uVision 5
* @conf PLL parameters are set in "Options for Target" -> "C/C++" -> "Defines"
* @packs STM32F4xx/STM32F7xx Keil packs are requred with HAL driver support
* @stdperiph STM32F4xx/STM32F7xx HAL drivers required
*/
/* Include core modules */
#include "stm32fxxx_hal.h"
/* Include my libraries here */
#include "defines.h"
#include "tm_stm32_disco.h"
#include "tm_stm32_delay.h"
#include "tm_stm32_fatfs.h"
#include "tm_stm32_lcd.h"
#include "stdio.h"
/* Fatfs structure */
FATFS FS;
FIL fil;
FRESULT fres;
/* Size structure for FATFS */
TM_FATFS_Size_t CardSize;
/* Buffer variable */
char buffer[100];
int main(void) {
/* Init system clock for maximum system speed */
TM_RCC_InitSystem();
/* Init HAL layer */
HAL_Init();
/* Init leds */
TM_DISCO_LedInit();
/* Try to mount card */
if (f_mount(&FS, "SD:", 1) == FR_OK) {
/* Try to open file */
if ((fres = f_open(&fil, "SD:first_file.txt", FA_OPEN_ALWAYS | FA_READ | FA_WRITE)) == FR_OK) {
/* Read SDCARD size */
TM_FATFS_GetDriveSize("SD:", &CardSize);
/* Format string */
sprintf(buffer, "Total card size: %u kBytes\n", CardSize.Total);
/* Write total card size to file */
f_puts(buffer, &fil);
/* Format string for free card size */
sprintf(buffer, "Free card size: %u kBytes\n", CardSize.Free);
/* Write free card size to file */
f_puts(buffer, &fil);
/* Close file */
f_close(&fil);
/* Turn led ON */
TM_DISCO_LedOn(LED_ALL);
}
/* Unmount SDCARD */
f_mount(NULL, "SD:", 1);
}
/* Do nothing */
while (1) {
}
}
|