一. 前言
前面我们实现了SD卡的读写,现在就可以来移植文件系统了,我们来移植使用的比较多的FATFS。
二. 准备
下载源码
http://elm-chan.org/fsw/ff/00index_e.html
将源码添加到自己的工程
只保留如下源码
三. 移植接口
需要在diskio中实现以下接口
DSTATUS disk_initialize (BYTE pdrv);
DSTATUS disk_status (BYTE pdrv);
DRESULT disk_read (BYTE pdrv, BYTE* buff, LBA_t sector, UINT count);
DRESULT disk_write (BYTE pdrv, const BYTE* buff, LBA_t sector, UINT count);
DRESULT disk_ioctl (BYTE pdrv, BYTE cmd, void* buff);
diskio.c中
包含我们的头文件
#include sd_itf.h
sd_itf中进一步封装对应的接口
int sd_itf_deinit(void)
{
return sd_deinit(&s_sd_dev);
}
int sd_itf_read_sblock(uint8_t* buffer, uint32_t block_addr)
{
return sd_read_sblock(&s_sd_dev,buffer,block_addr);
}
int sd_itf_write_sblock(uint8_t* buffer, uint32_t block_addr)
{
return sd_write_sblock(&s_sd_dev,buffer,block_addr);
}
int sd_itf_read_mblock(uint8_t* buffer, uint32_t block_addr, uint32_t count)
{
return sd_read_mblock(&s_sd_dev,buffer,block_addr,count);
}
int sd_itf_write_mblock(uint8_t* buffer, uint32_t block_addr, uint32_t count)
{
return sd_write_mblock(&s_sd_dev,buffer,block_addr,count);
}
int sd_itf_get_sectorcount(uint32_t* count)
{
*count = (uint32_t)(s_sd_dev.cap / 512);
return 0;
}
int sd_itf_init(void);
int sd_itf_read_sblock(uint8_t* buffer, uint32_t block_addr);
int sd_itf_write_sblock(uint8_t* buffer, uint32_t block_addr);
int sd_itf_read_mblock(uint8_t* buffer, uint32_t block_addr, uint32_t count);
int sd_itf_write_mblock(uint8_t* buffer, uint32_t block_addr, uint32_t count);
int sd_itf_deinit(void);
int sd_itf_get_sectorcount(uint32_t* count);
disk_status
返回当前状态,这里直接返回OK
DSTATUS disk_status (
BYTE pdrv /* Physical drive nmuber to identify the drive */
)
{
DSTATUS stat;
switch (pdrv) {
case DEV_MMC :
stat = RES_OK;
// translate the reslut code here
return stat;
}
return STA_NOINIT;
}
disk_initialize
底层我们单独初始化,这里直接返回OK
DSTATUS disk_initialize (
BYTE pdrv /* Physical drive nmuber to identify the drive */
)
{
DSTATUS stat;
switch (pdrv) {
case DEV_MMC :
stat = RES_OK;
// translate the reslut code here
return stat;
}
return STA_NOINIT;
}
disk_read
读块
DRESULT disk_read (
BYTE pdrv, /* Physical drive nmuber to identify the drive */
BYTE *buff, /* Data buffer to store read data */
LBA_t sector, /* Start sector in LBA */
UINT count /* Number of sectors to read */
)
{
DRESULT res;
int r=0;
switch (pdrv) {
case DEV_MMC :
// translate the arguments here
if (count == 1)
{
if(0 == (r = sd_itf_read_sblock((uint8_t*)buff, sector)))
{
res = RES_OK;
}
else
{
res = RES_ERROR;
}
}
else if(count > 1)
{
if(0 == (r=sd_itf_read_mblock((uint8_t*)buff, sector, count)))
{
res = RES_OK;
}
else
{
res = RES_ERROR;
}
}
else
{
res = RES_PARERR;
}
// translate the reslut code here
DISKIO_LOG(("rd sector:%d,count:%d res:%d\r\n",sector,count,r));
return res;
}
return RES_PARERR;
}
disk_write
写块
DRESULT disk_write (
BYTE pdrv, /* Physical drive nmuber to identify the drive */
const BYTE *buff, /* Data to be written */
LBA_t sector, /* Start sector in LBA */
UINT count /* Number of sectors to write */
)
{
DRESULT res;
int r=0;
switch (pdrv) {
case DEV_MMC :
// translate the arguments here
if (count == 1)
{
if(0 == (r=sd_itf_write_sblock((uint8_t*)buff, sector)))
{
res = RES_OK;
}
else
{
res = RES_ERROR;
}
}
else if(count > 1)
{
if(0 == (r = sd_itf_write_mblock((uint8_t*)buff, sector, count)))
{
res = RES_OK;
}
else
{
res = RES_ERROR;
}
}
else
{
res = RES_PARERR;
}
// translate the reslut code here
DISKIO_LOG(("wr sector:%d,count:%d res:%d\r\n",sector,count,r));
return res;
}
return RES_PARERR;
}
disk_ioctl
格式化时需要GET_SECTOR_COUNT获取容量大小。
DRESULT disk_ioctl (
BYTE pdrv, /* Physical drive nmuber (0..) */
BYTE cmd, /* Control code */
void *buff /* Buffer to send/receive control data */
)
{
DRESULT res;
DISKIO_LOG(("ioctl pdrv:%d,cmd:%d\r\n",pdrv,cmd));
switch (pdrv) {
case DEV_MMC :
switch(cmd)
{
case GET_SECTOR_COUNT:
sd_itf_get_sectorcount(buff);
break;
case CTRL_SYNC:
break;
}
res = RES_OK;
// Process of the command for the MMC/SD card
return res;
}
return RES_PARERR;
}
配置
ffconf.h中
配置支持格式化
#define FF_USE_MKFS 1
以下参数需要按需配置,目录”x:”的数字x即对应驱动号, 下面宏配置为最大可能出现的x。
默认一个配置为1,则目录使用0:开头
#define FF_VOLUMES 1
对应diskio.c中的参数pdrv就为0。
即对应
#define DEV_MMC 0
时间接口
如果FF_FS_NORTC为0则需要实现get_fattime获取时间戳,我这里配置为1,不使用时间信息。
四. 测试
测试代码如下,mount失败则自动格式化,创建文件写,然后读。
#include "ff.h"
int r;
static FATFS fs; /* Filesystem object */
static FIL fil; /* File object */
FRESULT res; /* API result code */
UINT bw; /* Bytes written */
UINT br;
static BYTE work[FF_MAX_SS]; /* Work area (larger is better for processing time) */
uint8_t read_buffer[16];
uint8_t write_buffer[16];
int mountflag = 0;
if(0 == (r = sd_itf_init()))
{
if(FR_OK != (res = f_mount (&fs, "0:", 1)))
{
printf("mount err %d, mkfs\r\n",res);
res = f_mkfs("0:",0,work,sizeof(work));
if(res == 0)
{
printf("mkfs ok\r\n");
if(FR_OK == f_mount (&fs, "0:", 1))
{
printf("mount ok\r\n");
mountflag = 1;
}
else
{
printf("mount err\r\n");
}
}
else
{
printf("mkfs err %d\r\n",res);
}
}
else
{
mountflag = 1;
printf("mount ok\r\n");
}
if(mountflag)
{
for(uint32_t i=0; i<sizeof(write_buffer); i++)
{
write_buffer[i] = i;
}
res = f_open(&fil, "test.bin", FA_CREATE_NEW | FA_WRITE);
if (res == 0)
{
printf("open for write ok\r\n");
/* Write a message */
res = f_write(&fil, write_buffer, sizeof(write_buffer), &bw);
if ((bw != sizeof(write_buffer)) || (res != 0))
{
printf("write err %d %d\r\n",bw,res);
}
else
{
printf("write ok\r\n");
}
/* Close the file */
res = f_close(&fil);
if(res != 0)
{
printf("close err %d\r\n",res);
}
}
else
{
printf("open for write err %d\r\n",res);
}
res = f_open(&fil, "test.bin", FA_READ);
if (res == 0)
{
printf("open for read ok\r\n");
/* read a message */
f_read(&fil, read_buffer, sizeof(read_buffer), &br);
if(res != 0)
{
printf("read err %d\r\n",res);
}
/* Close the file */
res = f_close(&fil);
if(res != 0)
{
printf("close err %d\r\n",res);
}
for(uint32_t i=0; i<br; i++)
{
printf("%02x,",read_buffer[i]);
}
}
else
{
printf("open for read err %d\r\n",res);
}
f_unmount("0:");
}
}
else
{
printf("sd init err:%d\r\n",r);
}
打印如下
五. 总结
移植fatfs非常简单,只需要实现块读写,以及获取容量的接口即可。
注意目录名x:中x和驱动号的对应。
注意FATFS ,FIL变量比较大,最好不要在函数中使用局部变量,如果要使用局部变量,注意栈的溢出。