1.5wuling_zhuanjietou/SYSTEM/fileSys.c

379 lines
10 KiB
C
Raw Normal View History

2024-10-17 01:06:51 +00:00
#include <string.h>
#include <stdio.h>
#include "fileSys.h"
#include "sd.h"
//#define DEPRINT printf
#define DEPRINT printf
/*
FA_OPEN_EXISTING <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD>ܡ<EFBFBD>(Ĭ<EFBFBD><EFBFBD>)
FA_OPEN_ALWAYS <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>򿪣<EFBFBD><EFBFBD><EFBFBD><EFBFBD>򣬴<EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD>
FA_CREATE_NEW <EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><EFBFBD>Ѵ<EFBFBD><EFBFBD>ڣ<EFBFBD><EFBFBD>򴴽<EFBFBD>ʧ<EFBFBD>ܡ<EFBFBD>
FA_CREATE_ALWAYS <EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><EFBFBD>Ѵ<EFBFBD><EFBFBD>ڣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ضϲ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǡ<EFBFBD>
*/
/**********************************************************************************************************
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD> :
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD> : <EFBFBD>ļ<EFBFBD>ϵͳ<EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD>
***********************************************************************************************************/
FRESULT res33; // FatFs function common result code
int filesys_init(void)
{
FATFS fs; // Work area (file system object) for logical drive
FIL fsrc; // file objects
FRESULT res; // FatFs function common result code
UINT br = 10; // File R/W count
BYTE work[10];
res = f_mount(&fs,"0:",1); //<2F><><EFBFBD><EFBFBD>SD<53><44>
Delay(100);
res = f_open(&fsrc,"0:/Demo.TXT",FA_CREATE_NEW | FA_READ | FA_WRITE ); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>ı<EFBFBD><C4B1>ĵ<EFBFBD><C4B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
Delay(100);
if (FR_NO_FILESYSTEM == res)
{
res = f_mkfs("0:/", 1, 0); //<2F><><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>ϵͳ
DEPRINT("fileSys: create Demo.TXT %u\n", res);
}
else if ( res == FR_OK )
{
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><D0B4><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD> */
memset(work, 0, sizeof(work));
memmove(work, "init", 4);
res = f_write(&fsrc, work, 4, &br);
f_sync(&fsrc); //ͬf_close<73><65><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>
DEPRINT("fileSys: open and write Demo.TXT %u\n", res);
res = f_close(&fsrc); /*<2A>ر<EFBFBD><D8B1>ļ<EFBFBD> */
}
else if ( res == FR_EXIST )
{
DEPRINT("fileSys: Demo.TXT exist!\n");
}
res = f_mount(0, "0:", 1);
return 0;
}
/**********************************************************************************************************
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD> :
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD> : <EFBFBD>Ƴ<EFBFBD>ָ<EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>
***********************************************************************************************************/
uint8_t fileSys__remove (char* path)
{
FATFS fs; // Work area (file system object) for logical drive
FRESULT res;
res = res;
DEPRINT("removed file: %s\n", path);
res = f_mount(&fs, "0:/", 1); //<2F><><EFBFBD><EFBFBD><EBB9A4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
DEPRINT("f_mount: res %u\n", res);
res = f_unlink((char*)path);
DEPRINT("f_unlink: res %u\n", res);
res = f_mount(0, "0:/", 1); //ע<><D7A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
DEPRINT("f_unmount: res %u\n", res);
return 0;
}
/**********************************************************************************************************
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD> :
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD> : <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
***********************************************************************************************************/
uint8_t fileSys__mkfs(void)
{
FATFS fs; // Work area (file system object) for logical drive
FRESULT res; // FatFs function common result code
res = f_mount(&fs, "0:/", 1);
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϴ<EFBFBD><CFB4><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>ļ<EFBFBD>ϵͳ<CFB5><CDB3><EFBFBD><EFBFBD>С<EFBFBD><D0A1>Ԫ512K
res = f_mkfs("0:/", 0, 512); /* Create an FAT volume on the logical drive 0. 2nd argument is ignored. */
res = f_mount(0, "0:/", 1);
return res;
}
/**********************************************************************************************************
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD> : fileSys__freeSpace
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD> : <EFBFBD><EFBFBD><EFBFBD>ÿ<EFBFBD><EFBFBD>пռ<EFBFBD>
***********************************************************************************************************/
uint8_t fileSys__freeSpace(char* fileName, uint32_t* totalkB, uint32_t* freekB)
{
uint8_t retValue = 0;
FRESULT res;
FATFS fs;
FATFS* p__fs = &fs;
DWORD fre_clust, fre_sect, tot_sect;
res = f_mount(&fs, "0:/", 1);
Delay(100);
/* Get volume information and free clusters of drive 1 */
res = f_getfree(fileName, &fre_clust, &p__fs);
if (res)
{
retValue = OPS__FAIL;
goto getfree__result;
}
/* Get total sectors and free sectors */
tot_sect = (p__fs->n_fatent - 2) * p__fs->csize;
fre_sect = fre_clust * p__fs->csize;
//x.
*totalkB = tot_sect * SF__DF__SECTOR__SIZE / 1024; //ת<><D7AA><EFBFBD><EFBFBD>KB
*freekB = fre_sect * SF__DF__SECTOR__SIZE / 1024; //ת<><D7AA><EFBFBD><EFBFBD>KB
/* Print free space in unit of KB (assuming 512 bytes/sector) */
getfree__result:
res = f_mount(0, "0:/", 1);
return retValue;
}
/**********************************************************************************************************
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD> :
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD> : <EFBFBD><EFBFBD><EFBFBD>ӻ<EFBFBD>׷<EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>
***********************************************************************************************************/
uint8_t fileSys__append(char* fileName, uint8_t apdType, uint8_t* data, uint16_t dataLength)
{
FATFS fs; // Work area (file system object) for logical drive
FIL fsrc; // file objects
FRESULT res; // FatFs function common result code
UINT br; // File R/W count
uint8_t retValue = FERROR__SUCC;
res = f_mount(&fs, "0:/", 1);
/*<2A>ڸո<DAB8>ע<EFBFBD><D7A2><EFBFBD>Ĺ<EFBFBD><C4B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><D0B4><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>򴴽<EFBFBD><F2B4B4BD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA>д */
if (FILE__APD == apdType) //<2F><><EFBFBD><EFBFBD>
{
//res = f_open(&fsrc, fileName, FA_OPEN_ALWAYS | FA_WRITE);
res = f_open(&fsrc, fileName, FA_OPEN_EXISTING | FA_WRITE);
res = f_lseek(&fsrc, f_size(&fsrc)); //<2F><>λ<EFBFBD><CEBB><EFBFBD>ļ<EFBFBD>ĩβ
}
else
{
res = f_open(&fsrc , fileName , FA_CREATE_ALWAYS | FA_WRITE);
res = f_lseek(&fsrc, 0); //<2F><>λ<EFBFBD><CEBB><EFBFBD><EFBFBD>ʼ
res = f_truncate(&fsrc); //<2F><><EFBFBD><EFBFBD><EFBFBD>ض<EFBFBD><D8B6>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD>/дָ<D0B4><D6B8>
}
if (res != FR_OK) //
{
retValue |= FERROR__OPEN;
DEPRINT("file: open error.\n");
}
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><D0B4><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD> */
res = f_write(&fsrc, data, dataLength, &br); // д<><D0B4><EFBFBD><EFBFBD>
if (res != FR_OK)
{
retValue |= FERROR__WRITE;
DEPRINT("file: write error.\n");
}
f_sync(&fsrc); //ͬf_close<73><65><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>
DEPRINT("should write %u, actually %u\n", dataLength, br);
res = f_close(&fsrc); // <20>ر<EFBFBD><D8B1>ļ<EFBFBD>
if (res != FR_OK)
{
retValue |= FERROR__CLOSE;
DEPRINT("file: close error.\n");
}
res = f_mount(0, "0:/", 1);
return retValue;
}
/**********************************************************************************************************
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD> :
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD> : <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>Ŀ¼<EFBFBD>ļ<EFBFBD>
***********************************************************************************************************/
uint8_t fileSys__read(char* fileName, uint8_t* data, uint16_t* dataLength, uint32_t cacheSize)
{
FATFS fs;
FIL fsrc; // file objects
FRESULT res; // FatFs function common result code
UINT br; // File R/W count
BYTE work[10];
uint8_t retValue = OPS__RW__SUCC;
uint16_t tmpReadDataLength = 0;
res = f_mount(&fs, "0:/", 1);
res = f_open(&fsrc, fileName, FA_OPEN_EXISTING | FA_READ); /* <20><>ֻ<EFBFBD><D6BB><EFBFBD>ķ<EFBFBD>ʽ<EFBFBD>򿪸ոմ<D5B8><D5B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD> */
if(res == FR_OK)
{
uint32_t fileSize = f_size(&fsrc); //<2F>ļ<EFBFBD><C4BC><EFBFBD>С
DEPRINT("fileread: file size: %u\n", fileSize);
// <20>жϻ<D0B6><CFBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if(cacheSize < fileSize)
return OPS__RW__OVERSIZE;
while(1)
{
memset(work, 0, sizeof(work));
res = f_read( &fsrc, work, sizeof(work), &br ); /* <20><><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݶ<EFBFBD><DDB6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
//memmove(data + tmpReadDataLength, work, br);
memcpy(data + tmpReadDataLength, work, br);
tmpReadDataLength += br;
if (res || br == 0)
break; /* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ߵ<EFBFBD><DFB5><EFBFBD><EFBFBD>ļ<EFBFBD>β */
}
}
else
{
retValue = OPS__RW__NOEXIST;
}
res = f_close(&fsrc); /* <20>رմ򿪵<D5B4><F2BFAAB5>ļ<EFBFBD> */
res = f_mount(0, "0:/", 1);
*dataLength = tmpReadDataLength;
return retValue;
}
/**********************************************************************************************************
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD> :
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD> : <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
char* path ·<EFBFBD><EFBFBD>
uint8_t type <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
***********************************************************************************************************/
uint8_t fileSys__traversalOps (char* path, uint8_t type, uint32_t tarIdx, uint8_t* paraData, uint16_t* paraDataLength)
{
uint32_t tmpIdx = 0;
uint8_t retValue = OPS__FAIL;
uint16_t tmpFileNameLength = 0;
uint8_t fileName[50];
FIL fsrc;
FATFS fs;
FRESULT res;
FILINFO fno;
DIR dir;
char *fn; /* This function is assuming non-Unicode cfg. */
#if _USE_LFN
static char lfn[_MAX_LFN + 1];
fno.lfname = lfn;
fno.lfsize = sizeof lfn;
#endif
if (type == FILESYS__DELETEALL)
retValue = OPS__RM__SUCC;
else if (type == FILESYS__SEARCHANY)
retValue = OPS__DIR__ERROR;
res = f_mount(&fs, "0:/", 1); // <20><><EFBFBD><EFBFBD><EBB9A4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
res = f_opendir(&dir, (const TCHAR*)path); // Open the directory <20><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>Ѵ<EFBFBD><D1B4>ڵ<EFBFBD>Ŀ¼<C4BF><C2BC><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD>ĵ<EFBFBD><C4B5>ô<EFBFBD><C3B4><EFBFBD>һ<EFBFBD><D2BB>Ŀ¼<C4BF><C2BC><EFBFBD><EFBFBD>
if (res == FR_OK) //<2F><><EFBFBD>ء<EFBFBD><D8A1>򿪳ɹ<F2BFAAB3>
{
while(1)
{
res = f_readdir(&dir, &fno); /* Read a directory item */
if(res != FR_OK || fno.fname[0] == 0) //<2F><><EFBFBD><EFBFBD><EFBFBD>˻<EFBFBD><CBBB><EFBFBD>ĩβ<C4A9><CEB2>
{
break; /* Break on error or end of dir */
}
if(fno.fname[0] == '.')
continue; /* Ignore dot entry */
#if _USE_LFN
fn = *fno.lfname ? fno.lfname : fno.fname;
#else
fn = fno.fname;
#endif
if(fno.fattrib & AM_DIR)
{
// /* It is a directory */
// sprintf(&path[i], "Dir: /%s", fn);
// res = scan_files(path);
// if (res != FR_OK)
// break;
// path[i] = 0;
}
else
{
if(FILESYS__SEARCHANY == type) // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>ļ<EFBFBD>
{
if(tmpIdx == tarIdx)
{
uint16_t tmpFileNameLength = 0;
memcpy(paraData + tmpFileNameLength, path, strlen(path));
tmpFileNameLength += strlen(path);
memcpy(paraData + tmpFileNameLength, "/", strlen("/")); //<2F><><EFBFBD><EFBFBD>Ŀ¼
tmpFileNameLength += strlen("/");
memcpy(paraData + tmpFileNameLength, fn, strlen(fn));
tmpFileNameLength += strlen(fn);
*paraDataLength = tmpFileNameLength;
paraData[tmpFileNameLength] = 0;
retValue = OPS__DIR__SUCC;
}
tmpIdx += 1;
}
else if (FILESYS__DELETEALL == type) //ɾ<><C9BE>Ŀ¼<C4BF><C2BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
{
memset(fileName, 0, sizeof(fileName));
tmpFileNameLength = 0;
memcpy(fileName + tmpFileNameLength, path, strlen(path));
tmpFileNameLength += strlen(path);
memcpy(fileName + tmpFileNameLength, "/", strlen("/"));
tmpFileNameLength += strlen("/");
memcpy(fileName + tmpFileNameLength, fn, strlen(fn));
tmpFileNameLength += strlen(fn);
fileName[tmpFileNameLength] = 0;
res = f_open(&fsrc, (char*)fileName, FA_OPEN_EXISTING | FA_WRITE);
res = f_close(&fsrc);
res = f_unlink((char*)fileName); //<2F>Ƴ<EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if (res != FR_OK)
{
retValue = OPS__RM__ERROR;
}
DEPRINT("f_unlink file: %s res %u\n", fileName, res);
}
}
} //while(1)
}
res = f_mount(0, "0:/", 1); //ע<><D7A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
return retValue;
}