Porting of Fatfs file system based on stm32f407 to SD card driver

Source: Internet
Author: User

porting of Fatfs file system based on stm32f407 to SD card driver

Daniellee_ustb

2014-2-26

Recently in doing SD card driver, previously transplanted efsl, feel that the use of people are not many, now transplant a fatfs, also keep up with the team.

The first step is to ensure that the SD card driver function is correct, including SD card initialization, SD sector reading and writing.

The second step, download Fatfs source code, called Ff9b.zip, extract in the SRC folder can get diskio.c, FF.C, and \option\ Cc936.c These three source files, create the Fatfs folder in the stm32f407 project file, add the source code and the corresponding header file, and add the folder location to the system include path to find its header file at compile time.

The third step begins the migration. DISKIO.C This file defines the interface functions between the Fatfs file system and the hardware memory for file system invocation, including Disk_initialize, Disk_status, Disk_read, Disk_write, Disk_ Five functions of the IOCTL. The FF.C contains common functions for FATFS file operations, including the operation of files and folders. CC936.C is a simplified Chinese character coding file.

First, configure the file system in Ffconf.h. Change #define_code_page 932 to 936 to support Simplified Chinese, the original default support for Japanese, do not know why, it is written by the Japanese. Do not study first. Compile it and find the following error:

#if!_USE_LFN | | _code_page! = 936

#error This file was not needed incurrent configuration. Remove from the project.

#endif

Where the macro definition _USE_LFN indicates whether to use a long file name, modify to 1, use static zone BSS storage, and the longest file name is 255 characters. Recompile, found that there are many functions not defined, including such as Ata_disk_initialize, Ata_disk_status, Ata_disk_read, ata_disk_write, etc., because we use the media is an SD card, delete other ATA, USB and MMC-related functions leave only sd_disk_initialize, Sd_disk_status, Sd_disk_read, Sd_disk_write, Sd_disk_ioctl, and Get_fattime functions.

These functions are implemented gradually in the following steps.

Disk_initialize, corresponding to the underlying function is sd_init.

Simply let it return 0 in Disk_status, indicating success.

Disk_read can see that the input has four parameter values, namely the device type PDRV, the data buffer pointer buff, the sector address sector, and the number of sectors count. This shows that Disk_read can read more than one sector at a time, adding in the function body

for (cnt=0; cnt<count; cnt++) {

Res =sd_readsingleblock (sector+cnt, buff+cnt*512);

if (res! = RES_OK) return res_error;

}

Similarly, this is also handled in Disk_write. Finally, the state RES_OK is returned directly from the Disk_ioctl. Other minor errors are compiled and passed.

To start the test, write a test case for creating a file in the SD card:

To create the file system and file objects:

FATFS FS;

FIL file;

①result = F_mount (FS_SD, &fs), mount the file system, indicating that there is fs_sd in the system and the file system

②result = F_opendir (&dirinf, "/"), opens the root directory, and in this function calls the chk_mounted, initializes the SD card.

③result = F_open (&file, "HelloYou.txt", Fa_create_always | Fa_write); Create a text file under the root directory

④result = F_write (&file,text,strlen (text), &BW); Write the test document, in order to be able to test write multiple blocks is not correct, directly find a large section of the Dragon Eight wrote in, you can see the correct document in the SD card, haha, test success.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.