FATFS, fatfsstm32

Source: Internet
Author: User

FATFS, fatfsstm32

(1) What is a file management system?

A: data is stored in the disk as files on the PC. The data is generally in the ASCII or binary format. To put it simply, it is the code used to manage files on a disk!

For example, we can write data on the SD card for more scientific management.

Http://elm-chan.org/fsw/ff/00index_e.html. The following is the source code

(2) What functions do we mainly use during transplantation?

A: Device Control Interface (hardware Interface function)

  • Disk_status-Get device status: select the operated module. SD card is used here.
  • Disk_initialize-Initialize device initialization Function
  • Disk_read-Read sector (s) Read
  • Disk_write-Write sector (s) Write
  • Disk_ioctl-Control device dependent features
  • Get_fattime-Get current time

(3) routine:

1. We use version 0.09.

2. CC936.c Chinese font library

① Create a work Interval

/* Register work area for each volume (Always succeeds regardless of disk status) */f_mount (0, & fs); // (file management system) registers a work interval, the workspace name is 0.

So what is the name range of the Work interval?

A:

FRESULT f_mount (BYTE vol,/* Logical drive number to be mounted/unmounted */FATFS *fs/* Pointer to new file system object (NULL for unmount)*/){FATFS *rfs;if (vol >= _VOLUMES)/* Check if the drive number is valid */return FR_INVALID_DRIVE;

Vol is the first parameter (name) of f_mount. When its value is greater than or equal to _ VOLUMES, an error is returned.

#define _VOLUMES1  //can define 9/* Number of volumes (logical drives) to be used. */

 

Parameters (official website)
Drive
Logical drive number (0-9) to register/unregister the work area.
FileSystemObject
Pointer to the work area (file system object) to be registered.

 

 

② Create a file in the work Interval

/* function disk_initialize() has been called in f_open *//* Create new file on the drive 0 */res = f_open(&fnew, "0:newfile.txt", FA_CREATE_ALWAYS | FA_WRITE );

OPEN attributes:

1. How to open it?

FA_CREATE_ALWAYS always opens the file in the form of creating a new one.

FA_WRITE: This file can only be written

2. Check the open function.

FRESULT f_open (
FIL *FileObject,/* Pointer to the blank file object structure */file Pointer to link the opened file to this Pointer
Const TCHAR *FileName,/* Pointer to the file neme */file name
BYTEModeFlags/* Mode flags */attribute
);

Attribute:

Value Description
FA_READ Specifies read access to the object. Data can be read from the file. CombineFA_WRITEFor read-write access.
FA_WRITE Specifies write access to the object. Data can be written to the file. CombineFA_READFor read-write access.
FA_OPEN_EXISTING Opens the file. The function fails if the file is not existing. (Default)
FA_OPEN_ALWAYS Opens the file if it is existing. If not, a new file is created. To append data to the file, use f_lseek function after file open in this method.
FA_CREATE_NEW Creates a new file. The function failsFR_EXISTIf the file is existing.
FA_CREATE_ALWAYS Creates a new file. If the file is existing, it is truncated and overwritten.

You can use: FA_CREATE_NEW for the first time, and then use: FA_OPEN_EXISTING (already exists ).

Or: always open, always close

When using: FA_CREATE_ALWAYS, we need to set # define _ FS_READONLY 0/* 0: Read/Write or 1: Read only */to open

3. For the Return value (Return Values): We only test: FR_ OK (whether it is successful)

③ Write data to the file

If (res = FR_ OK) {res = f_write (& fnew, textFileBuffer, sizeof (textFileBuffer), & bw); f_close (& fnew); after writing, close the file}

④ Turn it off and open it again

Res = f_open (& fnew, "0: newfile.txt", FA_OPEN_EXISTING | FA_READ); // FA_READ: The definition is readable.

⑤ Read data

res = f_read(&fnew, buffer, sizeof(buffer), &br); 

⑥ Close after printing

printf("\r\n %s ", buffer);/* Close open files */f_close(&fnew);

7. register the "0" Work interval just opened in the file system

/* Unregister work area prior to discard it */f_mount(0, NULL);

(4) association between the file management system and the SD card!

1. Application: the upper-layer functions used in our main function.

2. The middle part is the file system module. When the file system needs to manipulate the underlying SD card, it also needs Low level disk I/O. This part of the driver needs to be written by ourselves.

/*-------------------------- SD Init ----------------------------- */  Status = SD_Init();if (Status!=SD_OK ){return STA_NOINIT;}else{return RES_OK;}}

And

/*-----------------------------------------------------------------------*//* Return Disk Status                                                    */DSTATUS disk_status (BYTE drv/* Physical drive nmuber (0..) */){return RES_OK;}

Read/write of the underlying Disk

/* Ctor * // * Read Sector (s) */DRESULT disk_read (BYTE drv,/* Physical drive nmuber (0 ..) */BYTE * buff,/* Data buffer to store read data */DWORD sector,/* Sector address (LBA) */BYTE count/* Number of sectors to read (1 .. 255) */) {if (count> 1) // multiple disks {SD_ReadMultiBlocks (buff, sector * BLOCK_SIZE, BLOCK_SIZE, count);/* Check if the Trans Fer is finished */SD_WaitReadOperation (); // cyclically query whether the dma transmission is over/* Wait until end of DMA transfer */while (SD_GetStatus ()! = SD_TRANSFER_ OK);} else // a single disk {SD_ReadBlock (buff, sector * BLOCK_SIZE, BLOCK_SIZE);/* Check if the Transfer is finished */SD_WaitReadOperation (); // cyclically query whether the dma transmission is over/* Wait until end of DMA transfer */while (SD_GetStatus ()! = SD_TRANSFER_ OK);} return RES_ OK;}/* writable * // * Write Sector (s) */# if _ READONLY = 0 DRESULT disk_write (BYTE drv, /* Physical drive nmuber (0 ..) */const BYTE * buff,/* Data to be written */DWORD sector,/* Sector address (LBA) */BYTE count/* Number of sectors to write (1 .. 255) */) {if (count> 1) {SD_WriteMultiBlocks (uint8_t *) buff, sector * BLOCK_SIZE, BLOCK_SIZE, count);/* Check if the Transfer is finished */SD_WaitWriteOperation (); // wait until the dma transmission ends while (SD_GetStatus ()! = SD_TRANSFER_ OK); // wait until the transmission from sdio to SD card ends} else {SD_WriteBlock (uint8_t *) buff, sector * BLOCK_SIZE, BLOCK_SIZE ); /* Check if the Transfer is finished */SD_WaitWriteOperation (); // wait until the dma Transfer ends while (SD_GetStatus ()! = SD_TRANSFER_ OK); // wait until the transmission from sdio to the SD card ends} return RES_ OK;} # endif/* _ READONLY */

I/O control is empty (OK is returned directly)

/*-----------------------------------------------------------------------*//* Miscellaneous Functions                                               */DRESULT disk_ioctl (BYTE drv,/* Physical drive nmuber (0..) */BYTE ctrl,/* Control code */void *buff/* Buffer to send/receive control data */){return RES_OK;}

 

For beginners of embedded systems, I have a lot of comments on the mistakes in this article!


How to create and batch name files in the FatFS File System of STM32?

I will give you a sample...

Char pch [40];
Short inum = 0, BMP res;
Fil bmp fsrc;
Do
{
Sprintf (char *) pch, "0: ScreenShort/ss_1_d.bmp", inum ++ );
If (inum> 500) return;
BMP res = f_open (& bmp fsrc, (char *) pch, FA_CREATE_NEW | FA_WRITE );
} While (BMP res! = FR_ OK );
BMP screen_short (0, 0, 400,240, & bmp fsrc );
F_close (& bmp fsrc );
I am using a part of the Code. The function is to save the current memory card. The memory card already has a part, so the current name should not be repeated, I used a while to create it all the time, until the creation is successful. The image name contains a variable, such as ss_0.bmp, and ss_1.bmp.
The hypothetical memory card has already been used by ss_0.bmp and ss_1.bmp. The previous name is ss_2.bmp. You are simpler than me. I hope I can give you a prompt.

How to create and batch name files in the FatFS File System of STM32?

I will give you a sample...

Char pch [40];
Short inum = 0, BMP res;
Fil bmp fsrc;
Do
{
Sprintf (char *) pch, "0: ScreenShort/ss_1_d.bmp", inum ++ );
If (inum> 500) return;
BMP res = f_open (& bmp fsrc, (char *) pch, FA_CREATE_NEW | FA_WRITE );
} While (BMP res! = FR_ OK );
BMP screen_short (0, 0, 400,240, & bmp fsrc );
F_close (& bmp fsrc );
I am using a part of the Code. The function is to save the current memory card. The memory card already has a part, so the current name should not be repeated, I used a while to create it all the time, until the creation is successful. The image name contains a variable, such as ss_0.bmp, and ss_1.bmp.
The hypothetical memory card has already been used by ss_0.bmp and ss_1.bmp. The previous name is ss_2.bmp. You are simpler than me. I hope I can give you a prompt.

Related Article

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.