File system size
The reason why it is not 6M is that there are directory and file information allocation tables, which will also occupy space.
RTC is required for returning real-time time
Equipment information acquisition
Call the functions of the file system layer from the file system application layer.
f_getfree: get free space
Path, pointer to remaining sector address, pointer to file system (handle)
2. Calculate the total number of sectors and the number of empty sectors
(1) Calculate total sectors
n_fatent: number of clusters + 2;
1 cluster = n sectors;
csize = n;
tot_sect = number of clusters * cisze = number of sectors;
fre_clust (obtained by f_getfree);
fre_ sect = fre_ Clust (number of spare clusters) * csize = number of sectors of spare space;
(1) Print information (4096 bytes / sector)
Print tot with printf_ sect 4,fre_ SET4 unit is kb (the size of a sector is 4kb, and the number of sectors * 4 is the size)
printf("\n*************** Equipment information acquisition ***************\r\n"); /* Get device information and empty cluster size */ res_flash = f_getfree("1:", &fre_clust, &pfs); /* Calculate the total number of sectors and the number of empty sectors */ tot_sect = (pfs->n_fatent - 2) * pfs->csize; fre_sect = fre_clust * pfs->csize; /* Print information (4096 bytes / sector) */ printf(">Total equipment space:%10lu KB. \n>Free space: %10lu KB. \n", tot_sect *4, fre_sect *4);
File location and format write function test
1.f_printf: format write file
2. Reading method
f_read
The readbuff is set to 512KB. If it is not large enough, it can be read in several times, and can be read through the for loop
When number of bytes to read= Number of bytes read indicates the end of the file
f_ Close (& handle)
printf("\n******** File location and format write function test ********\r\n"); res_flash = f_open(&fnew, "1:FatFs Read and write test files.txt", FA_OPEN_ALWAYS|FA_WRITE|FA_READ ); if ( res_flash == FR_OK ) { /* File location */ res_flash = f_lseek(&fnew,f_size(&fnew)); if (res_flash == FR_OK) { /* The parameter format is similar to the printf function */ f_printf(&fnew,"\n Add a new line to the original file\n"); f_printf(&fnew,">Total equipment space:%10lu KB. \n>Free space: %10lu KB. \n", tot_sect *4, fre_sect *4); /* Locate the file to the start of the file */ res_flash = f_lseek(&fnew,0); /* Read all contents of the file to the cache */ res_flash = f_read(&fnew,readbuffer,f_size(&fnew),&fnum); if(res_flash == FR_OK) { printf(">Document content:\n%s\n",readbuffer); } } f_close(&fnew);
Function test of directory creation and renaming
1. Open the directory
f_opendir: open folder
2. If opening fails, create a directory
f_mkdir: create folder (directory)
3. If the directory already exists, close it
f_closedir: close folder
f_unlink: delete a file
4. Rename and move files
f_rename
printf("\n********** Function test of directory creation and renaming **********\r\n"); /* Try opening the directory */ res_flash=f_opendir(&dir,"1:TestDir"); if(res_flash!=FR_OK) { /* Failed to open directory, create directory */ res_flash=f_mkdir("1:TestDir"); } else { /* If the directory already exists, close it */ res_flash=f_closedir(&dir); /* Delete file */ f_unlink("1:TestDir/testdir.txt"); } if(res_flash==FR_OK) { /* Rename and move files */ res_flash=f_rename("1:FatFs Read and write test files.txt","1:TestDir/testdir.txt"); } } else { printf("!! Failed to open file:%d\n",res_flash); printf("!! You may need to run it again“ FatFs Transplantation and reading and writing test "project\n"); } return res_flash;
File information acquisition
(1) Path
(2) Information structure pointer FILINFOs
f_stat: fill in information structure members
Get information by reading information structure members
Confirm the file type by whether the fattrib is 1
static FRESULT file_check(void) { /* Get file information */ res_flash=f_stat("1:TestDir/testdir.txt",&fno); if(res_flash==FR_OK) { printf(""testdir.txt"File information:\n"); printf(">file size: %ld(byte)\n", fno.fsize); printf(">time stamp: %u/%02u/%02u, %02u:%02u\n", (fno.fdate >> 9) + 1980, fno.fdate >> 5 & 15, fno.fdate & 31,fno.ftime >> 11, fno.ftime >> 5 & 63); printf(">attribute: %c%c%c%c%c\n\n", (fno.fattrib & AM_DIR) ? 'D' : '-', // Is a directory (fno.fattrib & AM_RDO) ? 'R' : '-', // Read only file (fno.fattrib & AM_HID) ? 'H' : '-', // Hide file (fno.fattrib & AM_SYS) ? 'S' : '-', // System file (fno.fattrib & AM_ARC) ? 'A' : '-'); // Archives } return res_flash; }
File scanning
Because recursion is used, the stack space needs to be large
Store long file name
Idea: select whether to keep the directory open until all files are scanned by checking whether it is a file
1. Create an array
2. Calculate directory length
This structure contains directory information
Call f_readdir: read directory information
If it is a directory, it is recursive and path synthesis is performed. If it is not a directory, generate a file name
static FRESULT scan_files (char* path) { FRESULT res; //Some variables that are modified in the recursive process do not use global variables FILINFO fno; DIR dir; int i; char *fn; // file name #if _USE_LFN /* Long file name support */ /* Simplified Chinese needs 2 bytes to save a "word"*/ static char lfn[_MAX_LFN*2 + 1]; fno.lfname = lfn; fno.lfsize = sizeof(lfn); #endif //Open Directory res = f_opendir(&dir, path); if (res == FR_OK) { i = strlen(path); for (;;) { //Read the contents of the directory, and then read the next file automatically res = f_readdir(&dir, &fno); //If it is empty, it means that all items have been read and the system will jump out if (res != FR_OK || fno.fname[0] == 0) break; #if _USE_LFN fn = *fno.lfname ? fno.lfname : fno.fname; #else fn = fno.fname; #endif //The dot indicates the current directory. Skip if (*fn == '.') continue; //Directory, recursive read if (fno.fattrib & AM_DIR) { //Composite full directory name sprintf(&path[i], "/%s", fn); //Recursive traversal res = scan_files(path); path[i] = 0; //Open failed, jump out of loop if (res != FR_OK) break; } else { printf("%s/%s\r\n", path, fn); //Output file name /* You can extract the file path of a specific format here */ }//else } //for } return res; }