fatfs/tiny文件系统的移植

这个文件系统运行的时候需要的资源少,我的芯片只有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");
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值