这个文件系统运行的时候需要的资源少,我的芯片只有2k RAM用这个文件系统刚好。
fatfs/tiny:么有格式化磁盘的功能。
用到这5个文件:
配置按照tff.h中的说明来就可以了
只需要把我们自己的 读写函数应用到 diskio.c中就可以了,移植非常简单,用不到的就直接返回0就ok
还有一个获取实时时钟的函数,不用的话就返回0就可以了
DSTATUS disk_initialize (
BYTE drv /* Physical drive nmuber (0…) */
)
{
DSTATUS stat;
int result;
do{
result = SD_Init();
}while(0);
return 0;
}
DSTATUS disk_status (
BYTE drv /* Physical drive nmuber (0…) */
)
{
DSTATUS stat;
int result;
return 0;
}
/-----------------------------------------------------------------------/
/* Read Sector(s) */
DRESULT disk_read (
BYTE drv, /* Physical drive nmuber (0…) */
BYTE buff, / Data buffer to store read data /
DWORD sector, / Sector address (LBA) /
BYTE count / Number of sectors to read (1…255) */
)
{
DRESULT res;
int result;
do{
result = sd_read(buff,sector,count);
if(result !=0x00)
{
//SD_Init();
}
}while(0);
return result;
}
/-----------------------------------------------------------------------/
/* Write Sector(s) */
#if _READONLY == 0
DRESULT disk_write (
BYTE drv, /* Physical drive nmuber (0…) */
const BYTE buff, / Data to be written /
DWORD sector, / Sector address (LBA) /
BYTE count / Number of sectors to write (1…255) */
)
{
DRESULT res;
u8 result;
do{
result = sd_write((u8*)buff,sector,count);
if(result !=0x00)
{
//SD_Init();
}
}while(0);
return result;
}
#endif /* _READONLY */
/-----------------------------------------------------------------------/
/* Miscellaneous Functions */
DRESULT disk_ioctl (
BYTE drv, /* Physical drive nmuber (0…) /
BYTE ctrl, / Control code */
void buff / Buffer to send/receive control data */
)
{
DRESULT res;
int result;
这个函数是在格式化磁盘的时候用到,我们不需要用,直接返回0就ok
return 0;
}
然后就可以用这些函数接口来使用文件系统了
先测试一下:
FATFS fs;
FIL fsrc;
printf(“SD init …\r\n”);
if(f_mount(0, &fs))
{
printf(“load fatfs failed \r\n”);
return 0;
}
printf(“load fatfs success \r\n”);
res = f_open(&fsrc, "log.txt", FA_WRITE);
printf("open code:%d\r\n",res);
if(res ==0x00)
{
res = f_close(&fsrc);
if(res)
{
printf("保存文件失败\r\n");
}
else
{
printf("文件已存在,无需创建新文件,size,%u\r\n",fsrc.fsize);
}
}
else
{
printf("文件不存在\r\n");
res = f_open(&fsrc, "log.txt", FA_CREATE_NEW);
if(res)
{
printf("文件创建失败,退出!\r\n");
}
else
{
res = f_close(&fsrc);
printf("文件创建成功\r\n");
}
}