STM32學(xué)習(xí)筆記之fatfs文件系統(tǒng)接口函數(shù)使用
FatFS文件系統(tǒng)包含了文件
ff.h :文件系統(tǒng)實(shí)現(xiàn)頭文件,定義有文件系統(tǒng)所需的數(shù)據(jù)結(jié)構(gòu)
diskio.h :底層驅(qū)動頭文件,就一些狀態(tài)宏的定義和底層驅(qū)動函數(shù)的申明
integer.h:僅實(shí)現(xiàn)數(shù)據(jù)類型重定義,增加系統(tǒng)的可移植性
ffconf.h :文件系統(tǒng)配置
ff.c :文件系統(tǒng)實(shí)現(xiàn)。
diskio.c 底層驅(qū)動
FatFs 提供下面的函數(shù)API:f_mount - 注冊/注銷一個工作區(qū)域(Work Area)
f_open - 打開/創(chuàng)建一個文件f_close - 關(guān)閉一個文件
f_read - 讀文件f_write - 寫文件
f_lseek - 移動文件讀/寫指針
f_truncate - 截斷文件
f_sync - 沖洗緩沖數(shù)據(jù) Flush Cached Data
f_opendir - 打開一個目錄
f_readdir - 讀取目錄條目
f_getfree - 獲取空閑簇 Get Free Clusters
f_stat - 獲取文件狀態(tài)
f_mkdir - 創(chuàng)建一個目錄
f_unlink - 刪除一個文件或目錄
f_chmod - 改變屬性(Attribute)
f_utime - 改變時間戳(Timestamp)
f_rename -重命名/移動一個文件或文件夾
f_mkfs - 在驅(qū)動器上創(chuàng)建一個文件系統(tǒng)
f_forward - 直接轉(zhuǎn)移文件數(shù)據(jù)到一個數(shù)據(jù)流 Forward file data to the stream directly
f_gets - 讀一個字符串
f_putc - 寫一個字符
f_puts - 寫一個字符串
f_printf - 寫一個格式化的字符磁盤I/O接口
f_tell - 獲取當(dāng)前讀/寫指針
f_eof - 測試一個文件是否到達(dá)文件末尾
f_size - 獲取一個文件大小
f_error - 測試一個文件是否出錯
因?yàn)镕atFs模塊完全與磁盤I/O層分開,因此需要下面的函數(shù)來實(shí)現(xiàn)底層物理磁盤的讀寫與獲取當(dāng)前時間。底層磁盤I/O模塊并不是FatFs的一部分,并且必須由用戶提供。
disk_initialize - Initialize disk drive 初始化磁盤驅(qū)動器
disk_status - Get disk status 獲取磁盤狀態(tài)
disk_read - Read sector(s) 讀扇區(qū)
disk_write - Write sector(s) 寫扇區(qū)
disk_ioctl - Control device dependent features 設(shè)備相關(guān)的控制特性
get_fattime - Get current time 獲取當(dāng)前時間
結(jié)合我之前寫的一篇博客SPI操作SD卡驅(qū)動,完成自定義的diskio.c文件如下:
#include"nrf_gpio.h"
#include"nrf_drv_spi.h"
#include"nrf_drv_common.h"
#include"nrf_assert.h"
#include"app_util_platform.h"
#include"bsp.h"
#include"app_trace.h"
#include"string.h"
#include"drv_sd_api.h"
#include"diskio.h"
/*StatusofDiskFunctions*/
DSTATUSdisk_initialize(BYTEdrv)
{
uint8_tres=0;
res=SD_Card_Initialize();//正確返回1:sd2:SDHC
if(res!=1)returnSTA_NOINIT;
elsereturn0;
}
DSTATUSdisk_status(
BYTEdrv/*Physicaldrivenmuber(0..)*/
)
{
return0;
}
DRESULTdisk_read(
BYTEdrv,/*Physicaldrivenmuber(0..)*/
BYTE*buff,/*Databuffertostorereaddata*/
DWORDsector,/*Sectoraddress(LBA)*/
BYTEcount/*Numberofsectorstoread(1..255)*/
)
{
uint8_tres=0;
if(!count)returnRES_PARERR;
if(count==1)//1個sector的讀操作
{
res=SD_readSingleBlock(sector,buff);
}
else//多個sector的讀操作
{
res=SD_ReadMultiBlock(sector,buff,count);
}
if(res==0x00)returnRES_OK;
elsereturnRES_ERROR;
}
#if_READONLY==0
DRESULTdisk_write(
BYTEdrv,/*Physicaldrivenmuber(0..)*/
constBYTE*buff,/*Datatobewritten*/
DWORDsector,/*Sectoraddress(LBA)*/
BYTEcount/*Numberofsectorstowrite(1..255)*/
)
{
uint8_tres=0;
if(count==1)
{
res=SD_writeSingleBlock(sector,buff);
}
else
{
res=SD_WriteMultiBlock(sector,buff,count);
}
//返回值轉(zhuǎn)換
if(res==0)
{
returnRES_OK;
}
else
{
returnRES_ERROR;
}
}
#endif/*_READONLY*/
DRESULTdisk_ioctl(
BYTEdrv,/*Physicaldrivenmuber(0..)*/
BYTEctrl,/*Controlcode*/
void*buff/*Buffertosend/receivecontroldata*/
)
{
DRESULTres;
if(drv)
{
returnRES_PARERR;//僅支持單磁盤操作,否則返回參數(shù)錯誤
}
//FATFS目前版本僅需處理CTRL_SYNC,GET_SECTOR_COUNT,GET_BLOCK_SIZ三個命令
switch(ctrl)
{
caseCTRL_SYNC:
/*
SD_CS_ENABLE();
if(SD_WaitReady()==0)
{
res=RES_OK;
}
else
{
res=RES_ERROR;
}
SD_CS_DISABLE();
*/