当前位置:   article > 正文

STM32之HAL开发——CubeMX串行Flash文件系统源码讲解

STM32之HAL开发——CubeMX串行Flash文件系统源码讲解

前提

本次源码讲解是根据FatFS文件系统使用的流程进行

注册设备函数

uint8_t FATFS_LinkDriver(Diskio_drvTypeDef *drv, char *path)
{
  return FATFS_LinkDriverEx(drv, path, 0);
}
---------------------------------------------------------------------------------------------------
typedef struct
{
  DSTATUS (*disk_initialize) (BYTE);                     /*!< Initialize Disk Drive                     */
  DSTATUS (*disk_status)     (BYTE);                     /*!< Get Disk Status                           */
  DRESULT (*disk_read)       (BYTE, BYTE*, DWORD, UINT);       /*!< Read Sector(s)                            */
#if _USE_WRITE == 1 
  DRESULT (*disk_write)      (BYTE, const BYTE*, DWORD, UINT); /*!< Write Sector(s) when _USE_WRITE = 0       */
#endif /* _USE_WRITE == 1 */
#if _USE_IOCTL == 1  
  DRESULT (*disk_ioctl)      (BYTE, BYTE, void*);              /*!< I/O control operation when _USE_IOCTL = 1 */
#endif /* _USE_IOCTL == 1 */

}Diskio_drvTypeDef;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

在使用FatFS文件系统前需要对使用的设备进行注册,需要传入俩个参数一个是Diskio_drvTypeDef,一个是文件路径参数可以是这样的:char SPIFLASHPath[4]。而其中的Diskio_drvTypeDef是一个结构体,里面定义了三个函数指针,需要在创建结构体时声明对应的函数。

设备挂载函数

FRESULT f_mount (
	FATFS* fs,			/* Pointer to the file system object (NULL:unmount)*/
	const TCHAR* path,	/* Logical drive number to be mounted/unmounted */
	BYTE opt			/* 0:Do not mount (delayed mount), 1:Mount immediately */
)
  • 1
  • 2
  • 3
  • 4
  • 5

设备注册成功后,需要在串行FLASH挂载文件系统,文件系统挂载时会对串行FLASH初始化。如果要取消挂载, 则在第一个参数传入NULL即可

文件系统格式化函数

FRESULT f_mkfs (
	const TCHAR* path,	/* Logical drive number */
	BYTE sfd,			/* Partitioning rule 0:FDISK, 1:SFD */
	UINT au				/* Size of allocation unit in unit of byte or sector */
)

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

创建文件系统前,需要对文件系统进行格式化,第一个传入路径,第二三个参数传入0,0即可。

au:簇大小,即每个簇所包含的扇区数。在创建文件系统时,需要指定簇大小。簇大小会影响文件系统的性能和空间利用率。一般情况下,簇大小应该根据磁盘容量进行选择。例如,对于1GB以下的磁盘,可以选择4KB的簇大小;对于1GB到32GB的磁盘,可以选择8KB的簇大小;对于32GB以上的磁盘,可以选择16KB的簇大小。如果au为0,则会自动选择簇大小。

简单使用函数

打开/创建文件
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 */
)
  • 1
  • 2
  • 3
  • 4
  • 5

示例:可以配合sprintf实现路径拼接,进而打开或者创建文件

sprintf(tempfilepath,"%s%s",SPIFLASHPath,"FatFs读写测试文件.txt"); //拼接出带逻辑驱动器名的完整路径名
f_res = f_open(&file, tempfilepath,FA_CREATE_ALWAYS | FA_WRITE );
  • 1
  • 2
写文件
FRESULT f_write (
	FIL* fp,			/* Pointer to the file object */
	const void *buff,	/* Pointer to the data to be written */
	UINT btw,			/* Number of bytes to write */
	UINT* bw			/* Pointer to number of bytes written */
)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

示例:传入一个char数组作为要写入的数据,fnum作为函数返回写入成功的字节数

  f_res=f_write(&file,WriteBuffer,sizeof(WriteBuffer),&fnum);
  • 1
读文件
FRESULT f_read (
	FIL* fp, 		/* Pointer to the file object */
	void* buff,		/* Pointer to data buffer */
	UINT btr,		/* Number of bytes to read */
	UINT* br		/* Pointer to number of bytes read */
)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

示例:传入一个空char数组作为缓冲区用于接收数据,fnum作为函数返回读出成功的字节数

      f_res = f_read(&file, ReadBuffer, sizeof(ReadBuffer), &fnum); 
  • 1
关闭文件
FRESULT f_close (
	FIL *fp		/* Pointer to the file object to be closed */
)
  • 1
  • 2
  • 3
注销设备

在注销设备前,需要先取消挂载的文件路径。

uint8_t FATFS_UnLinkDriver(char *path)
{ 
  return FATFS_UnLinkDriverEx(path, 0);
}
  • 1
  • 2
  • 3
  • 4
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/463965
推荐阅读
相关标签
  

闽ICP备14008679号