stm32 Fats 檔案系統移植、應用
移植紅牛開發板的 檔案系統 到rtos 工程中。
F_open 形參:
Flags Meaning
FA_READ Specifies read access to the object. Data can be read from the file.
FA_WRITE Specifies write access to the object. Data can be written to the file. Combine with FA_READ for read-write access.
FA_OPEN_EXISTING Opens the file. The function fails if the file is not existing. (Default)
FA_CREATE_NEW Creates a new file. The function fails with FR_EXIST if the file is existing.
FA_CREATE_ALWAYS Creates a new file. If the file is existing, it will be truncated and overwritten.
FA_OPEN_ALWAYS Opens the file if it is existing. If not, a new file will be created.
FA_OPEN_APPEND Same as FA_OPEN_ALWAYS except the read/write pointer is set end of the file.
POSIX FatFs
“r” FA_READ
“r+” FA_READ | FA_WRITE
“w” FA_CREATE_ALWAYS | FA_WRITE
“w+” FA_CREATE_ALWAYS | FA_WRITE | FA_READ
“a” FA_OPEN_APPEND | FA_WRITE
“a+” FA_OPEN_APPEND | FA_WRITE | FA_READ
“x”*1 FA_CREATE_NEW | FA_WRITE
“x+”*1 FA_CREATE_NEW | FA_WRITE | FA_READ
fopen相關引數:
“r” 以只讀方式開啟檔案,該檔案必須存在。
“r+” 以可讀寫方式開啟檔案,該檔案必須存在。
“w” 開啟只寫檔案,若檔案存在則檔案長度清為0,即該檔案內容會消失。若檔案不存在則建立該檔案。
“w+” 開啟可讀寫檔案,若檔案存在則檔案長度清為零,即該檔案內容會消失。若檔案不存在則建立該檔案。
“a” 以附加的方式開啟只寫檔案。若檔案不存在,則會建立該檔案,如果檔案存在,寫入的資料會被加到檔案尾,即檔案原先的內容會被保留。(EOF符保留)
”a+“ 以附加方式開啟可讀寫的檔案。若檔案不存在,則會建立該檔案,如果檔案存在,寫入的資料會被加到檔案尾後,即檔案原先的內容會被保留。 (原來的EOF符不保留)
SEEK_SET 從距檔案開頭offset 位移量為新的讀寫位置.
SEEK_CUR 以目前的讀寫位置往後增加offset個位移量.
2)SEEK_END 將讀寫位置指向檔案尾後再增加offset 個位移量. 當whence 值為SEEK_CUR 或SEEK_END 時, 引數offset 允許負值的出現.
附加說明:fseek()不像lseek()會返回讀寫位置, 因此必須使用ftell()來取得目前讀寫的位置.
Ftell() 用於得到檔案位置指標當前位置相對於檔案首的偏移位元組數。
fseek(fp, 0L,SEEK_END);
len =ftell(fp); 首先將檔案的當前位置移到檔案的末尾,然後呼叫函式ftell()獲得當前位置相
例子:建立1個新檔案,往檔案中寫人資料, 再讀取資料
注意讀取資料時,f_open(&fd, “file.bin”, FA_READ ); 從頭開始的
FATFS fatfs;
FIL fd;
FRESULT result;
unsigned int len ;
unsigned char readbuffer[50];
void lxl_prac_fileoperation()
{
result = f_mount(&fatfs, "", 0); //載入檔案系統
if (result != FR_OK)
{
printf(" fmount file err \r\n");
}
else if(result == FR_OK)
{
printf("fmount suncessful \r\n");
}
result = f_open(&fd, "file.bin", FA_CREATE_NEW);
result = f_write(&fd,"hello fatfs", sizeof("hello fatfs"), &len);
f_close(&fd);
result = f_open(&fd, "file.bin", FA_READ );//注 FA_READ 指向檔案的 0位置
if(result!=FR_OK)
{
printf("f_open file.bin err \r\n");
}
result = f_read(&fd,readbuffer,sizeof("hello fatfs"),&len);
readbuffer[len] ='\0';
printf("%s \r\n",readbuffer);
f_close(&fd);
f_mount(NULL, "", 0); //載入檔案系統
}
f_lseek 應用:
res = f_open(fp, "file.dat", FA_READ|FA_WRITE);
if (res) ...
/* Move to offset of 5000 from top of the file */
res = f_lseek(fp, 5000);
/* Move to end of the file to append data */
res = f_lseek(fp, f_size(fp));
/* Forward 3000 bytes */
res = f_lseek(fp, f_tell(fp) + 3000);
/* Rewind 2000 bytes (take care on wraparound) */
res = f_lseek(fp, f_tell(fp) - 2000);
// 在原來檔案後,填件內容
void lxl_prac_fileoperation2()
{
result = f_mount(&fatfs, "", 0); //載入檔案系統
if (result != FR_OK)
{
printf(" fmount file err \r\n");
}
else if(result == FR_OK)
{
printf("fmount suncessful \r\n");
}
result = f_open(&fd, "file.bin", FA_CREATE_ALWAYS | FA_WRITE | FA_READ) ; // 根據前面的表,知道相當於 w+ (w 破壞原檔案,重新寫,w+ 不破壞原檔案,但檔案指標在頭部,借用 f_lseek(fp, f_size(fp)) 移到函式末尾)
if(result != FR_OK)
{
printf("failed to open file mode is FA_READ|FA_WRITE| FA_OPEN_ALWAYS!\r\n");
}
len= f_tell(&fd);
printf("size is %d \r\n",len);
f_lseek(&fd,20); // 移到檔案第20個位元組處
f_write(&fd,"i will add data in fatfs", sizeof("i will add data in fatfs"), &len);//在檔案末尾+20 位元組處 新增內容
f_close(&fd);
result = f_open(&fd, "file.bin", FA_READ); // 移到檔案的首位置
if(result != FR_OK)
{
printf("failed to open file!\r\n");
}
result = f_read(&fd,readbuffer,50,&len);
readbuffer[49] ='\0';
printf("%s \r\n",readbuffer);
f_close(&fd);
f_mount(NULL, "", 0); // 解除安裝檔案系統
}
檔案系統練習:
這個函式 是輸入posix 介面值,獲得 fatfs 的open 型別:
int fopenString( char * temp)
{
int val =0;
if(strncmp((char *)temp,"r+",2) ==0)
{
val =FA_READ | FA_WRITE ;
return val ;
}
else if(strncmp((char *)temp,"r",1) ==0)
{
val =FA_READ ;
return val ;
}
if (strncmp((char *)temp,"w+",2) ==0)
{
val =FA_CREATE_ALWAYS | FA_WRITE | FA_READ ;
return val ;
}
else if(strncmp((char *)temp,"w",1) ==0)
{
val =FA_CREATE_ALWAYS | FA_WRITE;
return val ;
}
else
{
return -1;
}
}
#define SD_SECTOR_SIZE (512)
#define BUFF_SIZE (4096)
FATFS fs0;
FIL f0;
FRESULT fr;
DIR dir0;
FILINFO f_info;
void fileoperation()
{
result = f_mount(&fatfs, "", 0); //載入檔案系統
if (result != FR_OK)
{
printf(" fmount file err \r\n");
}
else if(result == FR_OK)
{
printf("fmount suncessful \r\n");
}
result = f_open(&fd,"file.bin",fopenString("w"));// 將檔案內容清零
f_puts("fopen(w)",&fd);
f_close(&fd);
result = f_open(&fd, "file.bin", fopenString("r+") ); 開啟檔案,指標在檔案頭,準備寫(檔案必須存在)
unsigned char tempstring[20]="file operation\r\n";
f_puts((const TCHAR*)tempstring,&fd); // 前面的內容將被覆蓋
// /f_lseek(&fd,f_size(&fd)); // 移到檔案末尾 ,如果檔案不想被覆蓋,加上這句話
//len =f_tell(&fd); // 獲取當前指標位置
f_puts((const TCHAR*)tempstring,&fd);
f_close(&fd);
result = f_open(&fd, "file.bin", fopenString("r")); // 移到檔案的首位置,讀檔案
if(result != FR_OK)
{
printf("failed to open file!\r\n");
}
result = f_read(&fd,readbuffer,100,&len);
readbuffer[99] ='\0';
printf("%s \r\n",readbuffer);
f_close(&fd);
f_mount(NULL, "", 0); // 解除安裝檔案系統
}
執行結果:
fmount suncessful
file operation
file operation
如果去掉 f_lseek(&fd,f_size(&fd)); 註釋,執行結果:
fmount suncessful
fopen(w)file operation
file operation