First of all, this is the first time I have opened a blog on CSDN. This is also my first blog post. If there are any shortcomings, please feel free to correct me . I am currently a sophomore student. The reason why I chose to use a blog here is that I was inspired by an embedded expert. If I share my learning with my friends, I believe I can learn more.
Without further ado, let’s get to the point.
First of all, FATFS is a completely free and open source FAT file system module, designed specifically for small embedded systems, written in standard C language, with strong independence, and can be easily ported to 8-bit, 16-bit, and the 32-bit ARM series STM32 that I am using. That is, FATFS is a scalable file system. This is particularly important.
The hierarchical structure diagram of the FATFS module is as follows. Here, some information from Zhengdian Atom is referenced. Thanks to Atom
The bottom layer interface, including the storage media read/write interface (disk I/O) and the real-time clock that provides file creation and modification time, requires us to write porting code according to the platform and storage media. The
middle layer FATFS module implements the FAT file read/write protocol. The FATFS module provides ff.c and ff.h. Unless necessary, users generally do not need to modify them. When using, just include the header file directly. The
top layer is the application layer. Users do not need to pay attention to the complex FAT protocol and internal structure of FATFS. They only need to call a series of application API interface functions provided by the FATFS module to users.
So, let me introduce my understanding of the commonly used API interface functions of the FATFS file system. (The following is what I wrote directly in WPS, so I copied it directly to understand it)
/* FRESULT f_open function mode all open mode description
-------------------------------------------------- ----------------------------------
FA_READ | Read mode, (read and write modes can be effective at the same time)
FA_WRITE | Write mode, (read and write modes can be effective at the same time)
FA_OPEN_EXISTING | Default opening method
FA_OPEN_ALWAYS | Open the file and create a new file if it does not exist;
| In this way, you can use f_lseek to append data to the file
|
FA_CREATE_NEW | Create a new file. If the file already exists, the creation fails.
FA_CREATE_ALWAYS | Create a new file. If the file already exists, overwrite the old one.
|
-------------------------------------------------- ----------------------------------
*/
/*------------------------------------------------ ----------------------------------
/①The following test f_write writes data through the program, that is, a new txt document is created through the program and there is data
res=f_open (&fil,"0:/write.txt", FA_CREATE_ALWAYS|FA_WRITE); //Create a new file and write operation
f_write (&fil, "This is a new file, the data is just written in!", 48, &bww); //The prerequisite is to open the file in the form of writing
f_close(&fil);//Close the file, must appear in pair with f_open function, same as below
res=f_open (&fil,"0:/write.txt", FA_READ);
f_read (&fil, buf,48,&bww);
f_close(&fil); //Whether you open or create a new file, remember to close it
LCD_ShowString(10,210,280,24,24,(u8 *)buf);//
①The following test writes data through the program, that is, a new txt document is created through the program and there is data
-------------------------------------------------- ---------------------------------- */
/*------------------------------------------------ ----------------------------------
②The following test FRESULT f_lseek() moves the file pointer. This function can move the current pointer position when reading or writing an open file, that is, it can add or subtract some unnecessary data.
res=f_open (&fil,"0:/write.txt", FA_WRITE);
res = f_lseek (&fil , 25); ////The pointer points to the 25th byte
res = f_write (&fil , "40" ,2 , &bw); //2 means the written data is 2 bytes
// res = f_lseek (&fil , fil.fptr + 10); ////The pointer moves forward 10 bytes
res = f_lseek (&fil , 28); ////The pointer points to the 28th byte
res = f_write (&fil , "forward" ,8 , &bw);
// res = f_lseek (&fil , fil.fptr - 20); ////The pointer moves backward 20 bytes
res = f_lseek (&fil , 37); ////The pointer points to the 37th byte
res = f_write (&fil, "backward", 9, &bw); //Write 9 bytes of data
res = f_lseek (&fil , fil.fsize); ////The pointer points to the end of the file
res = f_write (&fil , "end" ,3 , &bw);
res = f_close ( &fil );
-------------------------------------------------- ---------------------------------- */
/*------------------------------------------------ ----------------------------------
③The following test FRESULT f_truncate() truncates the file. This function can truncate the file at the current pointer or extend the file length.
res = f_open (&fil ,"0:/write.txt", FA_WRITE); //Open file and write operation
res = f_lseek (&fil , 60); ////The pointer points to the 60th byte
res = f_truncate (&fil ); ////Truncate the file here, all data after 60 bytes will disappear, that is, truncate the file
res = f_sync ( &fil ); ////Close the file
-------------------------------------------------- ------------------------------- */
/*------------------------------------------------ ----------------------------------
④ Refresh cache information FRESULT f_sync (FIL* FileObject ) function, * FileObject == pointer to the file object structure
For example: res = f_sync (&fil);
Function description: This function is compatible with f_close. The difference between it and f_close is whether the current file is valid after execution.
After calling this function, the current file is still readable, writable and searchable.
Usage: When the file is in write mode for a long time, such as data logging, call this function regularly, or call it immediately after writing data.
This function can reduce data loss caused by unexpected situations such as power outages, which is a bit like the regular background save in WORD.
-------------------------------------------------- ---------------------------------- */
/*------------------------------------------------ ----------------------------------
⑤Create a new folder FRESULT f_mkdir (const TCHAR* DirName) function, *DirName == pointer to the name of the folder to be created
Function Description: Create a new folder,
Note: The file name should comply with the fatfs standard and cannot contain illegal characters.
The length of the file name cannot be greater than 8, otherwise the new creation will fail.
Example: f_mkdir("new"); //Create a new folder named new
-------------------------------------------------- ------- */
/*------------------------------------------------ ----------------------------------
⑥ Delete files and folders FRESULT f_unlink() function, *FileName: pointer to the name of the file or folder
Function Description: This function can delete a file or folder
Notes for use:
When deleting a folder: 1. It cannot be the current folder
2. It cannot be a non-empty folder
When deleting a file: 1. The file cannot be opened
2. Cannot be a read-only file
For example: f_unlink("new"); //Delete the folder "new"
f_unlink("TEXT/write.txt"); //Delete the "write" txt text in the "TEXT" folder
When deleting files, you must pay attention to the following points: all the tails must end with png.txt
-------------------------------------------------- ------- */
/*------------------------------------------------ ----------------------------------
⑦Rename\move file or folder FRESULT f_rename (const TCHAR* OldName, const TCHAR* NewName)
Function Description: This function can move or rename a file or folder
Parameter description: *OldName: pointer to the old file name
*NewName : pointer to the new file name
Notes for use:
1. This function can rename a file or folder, regardless of whether the folder is empty.
2. This function can move files or folders, regardless of whether the folder is empty.
For example: res = f_rename("Test.txt","Test1.txt"); //Rename the test.txt file,
res = f_rename("Test 1.txt","PAINT/Test 2.txt");
//Move the test1.txt file to the folder PAINT and rename it to test2.txt
-------------------------------------------------- ------- */
/*------------------------------------------------ ----------------------------------
⑧Get file information FRESULT f_stat(const TCHAR* FileName,FILINFO* FileInfo)
Function description: This function can obtain the latest modification time, attributes and other information of the file. The obtained information is stored in the fileninfo structure
Parameter description: *FileName: pointer to the file name
*FileInfo: Pointer to the structure that stores file information. The type must be FileInfo
Notes for use:
1. If the target is a folder, the size is 0.
2. This function is invalid for the root directory.
3. Both time and date are two bytes, and the storage format is as follows:
a) Date:
i. bit15…bit9: The decimal number after the year calculation should be added with 1980
ii. bit8 … bit5: month
iii. bit4 … bit0: day
b) Time:
i. bit15…bit11:
ii. bit10…bit5: points
iii. bit4 … bit0 : The decimal number calculated for seconds should be *2
Example:
i. Date: 0000001 0001 00001, which means January 1, 1981
ii. Time: 00001 000001 00001, which means 1:01:02.
Example: res = f_stat("TEXT/write.txt", &filinfo); //Read the information of the newname.txt file in the folder directory
if( res )
printf("newname.txt err : %d\r\n", res); //Failed to read the file information successfully
else
{
printf("newname.txt size : %lu\r\n",filinfo.fsize);//Read the length of the file, that is, how many bytes it occupies
printf("fdate : %d\r\n",filinfo.fdate); //Read the file's most recent modification date and convert it into binary.
printf("ftime : %d\r\n",filinfo.ftime); //Read the last modification time of the file and convert it into binary
printf("fattrib : %d\r\n",filinfo.fattrib);//Display the properties of the file, that is, what file
//#define AM_RDO 0x01 //Read-only file
//#define AM_HID 0x02 //Hidden file
//#define AM_SYS 0x04 //System file
//#define AM_VOL 0x08 //Volume label file
//#define AM_LFN 0x0F //
//#define AM_DIR 0x10 //Program directory
//#define AM_ARC 0x20 //Archive file
//#define AM_MASK 0x3F //
}
-------------------------------------------------- ------- */
/*------------------------------------------------ ----------------------------------
⑨Change file attributes: FRESULT f_chmod (const TCHAR* FileName,BYTE Attribute,BYTE AttributeMask)
Function Description:
1. This function can modify the attributes of a file or folder
2. The properties that can be modified are only one or a combination of the following. Other properties are invalid.
AM_RDO //Read-only file
AM_ARC //Archive file
AM_SYS //System files
AM_HID //Hidden files
Parameter Description:
a) *Filename: pointer to the name of the file or folder
b) Attribute: The attribute to be set, that is, what the file or folder attribute needs to be changed to
c) AttributeMask: The attributes that need to be changed (including the attributes to be set and the attributes to be cleared), that is, the original attributes and the attributes that need to be changed
Instructions:
a) Attribute must be a subset of AttributeMask
b) The function processes the attribute set in AttributeMask. If the attribute is contained in Attribute, it is set, otherwise it is cleared.
Example: Write to the file TEXT/.txt, set the HID and SYS attributes, and cancel the ARC and RDO attributes
res = f_chmod("TEXT/write.txt", AM_HID | AM_SYS, AM_ARC | AM_RDO | AM_HID |AM_SYS);
if( res )
printf("err :%d\r\n", res);
else
{
res = f_stat("TEXT/write.txt", &filinfo);
printf("fattrib : %d\r\n",filinfo.fattrib);//
}
-------------------------------------------------- ------- */
/*------------------------------------------------ ----------------------------------
⑩Change file timestamp FRESULT f_utime (TCHAR* FileName, FILINFO* TimeDate)
Function Description:
1. This function can change the last modification time of a file
Parameter Description:
a) Filename: pointer to the file
b) Timedate: pointer to the file information structure
Usage: In this function, we can write a regular date and time, and then this function integrates the data according to the date storage format (see above) and calls f_utime.
FRESULT set_timestamp (char *obj,int year, int month, int mday, int hour, int min, int sec);//
{
FILINFO fno;
fno.fdate = (WORD)(((year - 1980) * 512U) | month * 32U | mday);
fno.ftime = (WORD)(hour * 2048U | min * 32U | sec / 2U);
return f_utime(obj, &fno);
res = set_timestamp("123.txt",2001,06,05,02,03,34);//Modify 123.txt time
printf("%d\r\n",res);
This example fails because the prototype of the function FRESULT set_timestamp(); is not found .
-------------------------------------------------- -------
Well, the above is my understanding of the commonly used API functions of the FATFS file system. I hope it can help some friends quickly understand
Previous article:Study notes on SD card FATFS file system based on STM32
Next article:STM32CubeMX based on SD card FATFS file system test
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- CGD and Qorvo to jointly revolutionize motor control solutions
- CGD and Qorvo to jointly revolutionize motor control solutions
- Keysight Technologies FieldFox handheld analyzer with VDI spread spectrum module to achieve millimeter wave analysis function
- Infineon's PASCO2V15 XENSIV PAS CO2 5V Sensor Now Available at Mouser for Accurate CO2 Level Measurement
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- A new chapter in Great Wall Motors R&D: solid-state battery technology leads the future
- Naxin Micro provides full-scenario GaN driver IC solutions
- Interpreting Huawei’s new solid-state battery patent, will it challenge CATL in 2030?
- Are pure electric/plug-in hybrid vehicles going crazy? A Chinese company has launched the world's first -40℃ dischargeable hybrid battery that is not afraid of cold
- Problems with STM32CubeMX generating serial port 1 interrupt initialization code
- What is unity gain frequency?
- TI 2803x DSP chip LIN communication baud rate adaptation
- Are UWB devices the future trend?
- Please tell me what's going on.
- I'm a novice and I need help on how to install Grace on CCS10. Thanks in advance.
- DCA1000EVM User Guide
- When designing Cadence PCB, after setting the large cross cursor, the cursor cannot be seen. When setting it to a small cursor, it is normal. What is the reason?
- [Ended] Live broadcast of "wearing" and "chip": Infineon's nanny-level solution for wearable devices
- 【GD32L233C-START Review】1. Getting to know GD32L233C-START