SD卡之FATFS库移植

方法一:官网库移植
1) 官网下载Fatfs库
FATFS官网
在这里插入图片描述
2) 将源文件添加到工程
该工程需要实现某存储器的读写等底层驱动,比如SD卡或者FLASH亦或者磁盘等存储介质
在这里插入图片描述

  ff.c           //FatFs module.
  ffconf.h       //Configuration file of FatFs module.
  ff.h           //Common include file for FatFs and application module.
  diskio.h       //Common include file for FatFs and disk I/O module.
  diskio.c       //An example of glue function to attach existing disk I/O module to FatFs.
  ffunicode.c    //Optional Unicode utility functions.
  ffsystem.c     //An example of optional O/S related functions.

3) 修改FATFS相关函数接口
主要包括diskio.c中的:
① disk_status()//获取设备状态

DSTATUS disk_status (
	BYTE pdrv		/* Physical drive nmuber to identify the drive */
)
{
	DSTATUS stat;
	switch (pdrv) 
	{
		case DEV_SD :
			if(SD_GetCID(ciddata))	//调用SD卡的获取设备状态函数接口	
				stat = RES_ERROR;
			else
				stat = RES_OK;
			return stat;
	}
	return STA_NOINIT;/* Drive not initialized */
}

② disk_initialize()//初始化

/*@pdrv Physical drive nmuber to identify the drive */
DSTATUS disk_initialize (BYTE pdrv)
{
	DSTATUS stat;
	switch (pdrv) {
	case DEV_SD :
		SD_Init();//初始化SD卡
		if(SD_GetCID(ciddata))	//重新获取一下SD卡的ID,看是否初始化成功	
			stat = RES_ERROR;
		else
			stat = RES_OK;
		return stat;
	}
	return STA_NOINIT;/* Drive not initialized */
}

③ 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;
	switch (pdrv) {
	case DEV_SD :
		res = SD_ReadDisk(buff, sector, count);
		if(res != 0) //读失败
		{
			SD_Init();//重新初始化SD卡
			res = SD_ReadDisk(buff, sector, count);//重新读
		}
		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;
	switch (pdrv) {
	case DEV_SD :
		res = SD_WriteDisk((BYTE *)buff, sector, count);
		if( res != 0) //写失败
		{
			SD_Iint();//重新初始化SD卡
			res = SD_WriteDisk((BYTE *)buff, sector, count);//重新写	
		}
		return res;
	}
	return RES_PARERR;
}

⑤ disk_ioctl()//设备属性接口

DRESULT disk_ioctl (
	BYTE pdrv,		/* Physical drive nmuber (0..) */
	BYTE cmd,		/* Control code */
	void *buff		/* Buffer to send/receive control data */
)
{
	DRESULT res;
	if(pdrv == DEV_SD)
	{
		switch (cmd) 
		{
			case CTRL_SYNC:
				res = RES_OK; 
				break;	 
			case GET_SECTOR_SIZE:
				*(DWORD*)buff = 512; //每个扇区的大小为512字节
				res = RES_OK;
				break;	 
			case GET_BLOCK_SIZE:
				*(WORD*)buff = 8;
				res = RES_OK;
				break;	 
			case GET_SECTOR_COUNT:
				*(DWORD*)buff = SD_GetSectorCount();//调用获取SD卡扇区个数的函数接口
				res = RES_OK;
				break;
			default:
				res = RES_PARERR;
				break;
		}
	}
	if(res)
		return RES_PARERR;
	return res;
}

4) 修改ffconf.h中的宏定义来使能相应的函数接口

#define FF_FS_READONLY	0 /*This option switches read-only configuration. (0:Read/Write or 1:Read-only)*/
#define FF_USE_MKFS		1/* This option switches f_mkfs() function. (0:Disable or 1:Enable) */
//f_mkfs()  The f_mkfs function creates an FAT/exFAT volume on the logical drive.格式化,一般挂载失败可以尝试调用格式化一下磁盘在再重新挂载
#define FF_CODE_PAGE	936 /*支持文件名为中文格式*/
#define FF_USE_LFN		0
#define FF_MAX_LFN		255
/* The FF_USE_LFN switches the support for LFN (long file name).
/   0: Disable LFN. FF_MAX_LFN has no effect.
/   1: Enable LFN with static  working buffer on the BSS. Always NOT thread-safe.注意开启的话,会增大代码占用空间
/   2: Enable LFN with dynamic working buffer on the STACK. 一般使用OS时设置
*/
#define FF_VOLUMES		1 /* Number of volumes (logical drives) to be used. (1-10) */
#define FF_MIN_SS		512
#define FF_MAX_SS		512
/*
/ 配置所支持的扇区大小范围。 (512, 1024, 2048或4096),一般大容量存储介质都配置为512,像磁盘,MMC等,对于flash一般需要将最大值设置为4096
*/
#define FF_FS_NORTC		0 /*0 使能时间戳,1 关闭时间戳,需要我们自己实现get_fattime(),可以开启RTC获得具体的时间*/

具体的宏定义查看官网具体配置介绍说明
以上就完成了移植与修改
接下来就可以调用相应的API函数来实现文件的创建,读写等操作。

//设备挂载
FRESULT f_mount (
  FATFS*       fs,    /* [IN] Filesystem object */
  const TCHAR* path,  /* [IN] Logical drive number */
  BYTE         opt    /* [IN] Initialization option */
);
//设备卸载
FRESULT f_unmount (
  const TCHAR* path   /* [IN] Logical drive number */
);
//格式化设备
FRESULT f_mkfs (
  const TCHAR* path,   /* [IN] Logical drive number */
  const MKFS_PARM* opt,/* [IN] Format options */
  void* work,          /* [-]  Working buffer */
  UINT len             /* [IN] Size of working buffer */
);
// 打开或者创建一个文件  
FRESULT f_open (
	FIL* fp,			/* Pointer to the blank file object */
	const TCHAR* path,	/* Pointer to the file name */
	BYTE mode			/* Access mode and file open mode flags */
)
//mode
#define	FA_READ				0x01 //	指定对对象的读取权限。可以从文件中读取数据。
#define	FA_WRITE			0x02 // 指定对对象的写访问权。数据可以写入文件。
#define	FA_OPEN_EXISTING	0x00 // 打开文件。如果文件不存在,该函数将失败
#define	FA_CREATE_NEW		0x04 // 创建一个新文件。如果文件存在,该函数将失败并显示 FR_EXIST 。
#define	FA_CREATE_ALWAYS	0x08 // 创建一个新文件。如果文件存在,它将被截断并覆盖。
#define	FA_OPEN_ALWAYS		0x10 // 如果文件存在,则打开该文件。如果不是,则创建一个新文件。
#define	FA_OPEN_APPEND		0x30 // 存在则打开、不存在则创建,指针指向文件尾
//举例
f_open(fp,"att_log.txt",FA_WRITE|FA_OPEN_APPEND);
//格化式写入
/*-----------------------------------------------------------------------*/
/* Put a Formatted String to the File                                    */
/*-----------------------------------------------------------------------*/
int f_printf (
	FIL* fp,			/* Pointer to the file object */
	const TCHAR* fmt,	/* Pointer to the format string */
	...					/* Optional arguments... */
)
//例如
f_printf(fp,"%d,%d\n",buff[0],buff[1]);
//关闭文件
FRESULT f_close (
	FIL *fp		/* Pointer to the file object to be closed */
)
//打开/创建文件、写完之后要关闭,否则最终的数据不会写到SD卡中
f_close(fp);
//写数据到文件里
FRESULT f_write (
	FIL* fp,			/* Pointer to the file object */
	const void* buff,	/* Pointer to the data to be written 要写进文件的数据buff*/
	UINT btw,			/* Number of bytes to write 要写的数据个数*/
	UINT* bw			/* Pointer to number of bytes written 写进文件的实际数据个数*/
)
//从文件里读
FRESULT f_read (
	FIL* fp, 	/* Pointer to the file object */
	void* buff,	/* Pointer to data buffer 要存放数据的buff*/
	UINT btr,	/* Number of bytes to read 指定要读的数据长度*/
	UINT* br	/* Pointer to number of bytes read 实际读出的数据长度*/
)
/刷新(同步)文件
FRESULT f_sync (
	FIL* fp		/* Pointer to the file object */
)
//也就是不关闭文件可以继续写,但是可以保证前面写的数据保存到SD卡中,
//可以降低因断电等带来的数据丢失分析,f_close调用的也是f_sync.
//但是f_close关闭文件后要想继续写需要重新打开.

具体的解释与示例参考官网http://elm-chan.org/fsw/ff/00index_e.html
基本上每个函数都有官方用法的示例参考。

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值