1.FAT32文件系统引入
为什么需要文件系统
浏览器如何从服务器获取网页?
文件系统的作用是什么
磁盘原理
硬盘容量 = 盘面数 × 柱面数 × 扇区数 × 512字节
SD卡存储结构
SD卡操作流程
read
/* @brief Reads Sector(s)
* @param lun : not used
* @param *buff: Data buffer to store read data
* @param sector: Sector address (LBA)
* @param count: Number of sectors to read (1..128)
* @retval DRESULT: Operation result
*/
DRESULT SD_read(BYTE lun, BYTE *buff, DWORD sector, UINT count);
wirte
/* @brief Writes Sector(s)
* @param lun : not used
* @param *buff: Data to be written
* @param sector: Sector address (LBA)
* @param count: Number of sectors to write (1..128)
* @retval DRESULT: Operation result
*/
DRESULT SD_write(BYTE lun, const BYTE *buff, DWORD sector, UINT count);
Fat32文件分配表
文件系统如何管理磁盘
Window下文件系统
/*
FAT12/FAT16/FAT32
exFAT
NTFS
linux下文件系统
/*
ext2/ext3
NFS
jffs2
yaffs
磁盘管理工具
FAT32存储结构
2.Fat32文件存储原理
Fat32怎么管理文件
簇---- 文件存储的最小单元
文件信息
100KB空间如何管理文件
分配方案
目录分配方案
当文件需要删除/增加
Fat32 FAT表
添加FAT表
FAT表格式
FAT表解决文件删除/添加问题
Fat32存储原理
FAT表取值
FAT 目录项
短文件名目录项
长文件名目录项
3.FatFS介绍及STM32集成
FatFS介绍
官方网站
http://elm-chan.org/fsw/ff/00index_e.html
应用接口
介质访问接口
相关资源
STM32Cube集成FatFS
FatFs 中间件模块架构
FatFS配置
- 打开FatFS
- 使能磁盘为SD卡
- 配置中文编码
- 配置命名空间为HEAP
- 增大C库堆空间
FatFS示例代码
uint8_t u8chr[] = "hello";
uint32_t u32Wbytes;
/* USER CODE END Variables */
void MX_FATFS_Init(void)
{
/*## FatFS: Link the SD driver ###########################*/
retSD = FATFS_LinkDriver(&SD_Driver, SDPath);
/* USER CODE BEGIN Init */
if(f_mount(&SDFatFS,SDPath,1) == FR_OK)
{
if(f_open(&SDFile,(const char*)"fatfs.txt",FA_CREATE_ALWAYS|FA_WRITE) == FR_OK)
{
if(f_write(&SDFile,u8chr,sizeof(u8chr),&u32Wbytes) == FR_OK)
{
f_close(&SDFile);
}
}
}
/* additional user code for init */
/* USER CODE END Init */
}
4.FatFS应用编程上
FatFS提供的应用接口
API学习方法
学习步骤
项目当中用到的API
f_mkfs //物理磁盘的格式化 |
---|
f_mount |
f_open |
f_read |
f_write |
f_size |
f_close |
API分析
f_mkfs
FRESULT f_mkfs (
const TCHAR* path, /* [IN] Logical drive number */
BYTE opt, /* [IN] Format options */
DWORD au, /* [IN] Size of the allocation unit */
void* work, /* [-] Working buffer */
UINT len /* [IN] Size of working buffer */
);
f_mount
FRESULT f_mount (
FATFS* fs, /* [IN] File system object */
const TCHAR* path, /* [IN] Logical drive number */
BYTE opt /* [IN] Initialization option */
);
f_open
FRESULT f_open (
FIL* fp, /* [OUT] Pointer to the file object structure */
const TCHAR* path, /* [IN] File name */
BYTE mode /* [IN] Mode flags */
);
f_close
FRESULT f_close (
FIL* fp /* [IN] Pointer to the file object */
);
f_read
FRESULT f_read (
FIL* fp, /* [IN] File object */
void* buff, /* [OUT] Buffer to store read data */
UINT btr, /* [IN] Number of bytes to read */
UINT* br /* [OUT] Number of bytes read */
);
f_write
FRESULT f_write (
FIL* fp, /* [IN] Pointer to the file object structure */
const void* buff, /* [IN] Pointer to the data to be written */
UINT btw, /* [IN] Number of bytes to write */
UINT* bw /* [OUT] Pointer to the variable to return number of bytes written */
);
f_size
FSIZE_t f_size (
FIL* fp /* [IN] File object */
);
5.FatFS应用编程下
实际文件系统应用案例分析
//远程终端单元( Remote Terminal Unit,RTU)
怎么存储历史数据呢
历史数据的目的是什么
CSV格式文件
历史数据存储功能实现
6.FatFS底层实现
通用底层驱动API
驱动包含哪些文件
ff_gen_drv.c
//FatFS 提供的通用驱动文件的实现
/**
* @brief Disk IO Driver structure definition
*/
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;
/**
* @brief Global Disk IO Drivers structure definition
*/
typedef struct
{
uint8_t is_initialized[_VOLUMES];//磁盘是否初始化
const Diskio_drvTypeDef *drv[_VOLUMES];//磁盘的驱动
uint8_t lun[_VOLUMES];//磁盘的编号
volatile uint8_t nbr;
}Disk_drvTypeDef;
sd_diskio.c
//针对SD底层驱动实现,封装成为通用的底层驱动API
//如果使能freeRTOS,在read和Write里面,会用到操作系统的消息队列
bsp_driver_sd.c
//HAL库的二次封装,把所有基于SD卡的操作都在bsp_driver_sd实现
驱动装载
/**
* @brief Links a compatible diskio driver/lun id and increments the number of active
* linked drivers.
* @note The number of linked drivers (volumes) is up to 10 due to FatFs limits.
* @param drv: pointer to the disk IO Driver structure
* @param path: pointer to the logical drive path
* @param lun : only used for USB Key Disk to add multi-lun management
else the parameter must be equal to 0
* @retval Returns 0 in case of success, otherwise 1.
*/
uint8_t FATFS_LinkDriverEx(const Diskio_drvTypeDef *drv, char *path, uint8_t lun)
{
uint8_t ret = 1;
uint8_t DiskNum = 0;
//判断是否超出了fatfs最大的卷数量
if(disk.nbr < _VOLUMES)
{
//未初始化
disk.is_initialized[disk.nbr] = 0;
//把驱动进行链接
disk.drv[disk.nbr] = drv;
disk.lun[disk.nbr] = lun;
DiskNum = disk.nbr++;
path[0] = DiskNum + '0'; //"1:/"
path[1] = ':';
path[2] = '/';
path[3] = 0;
ret = 0;
}
return ret;
}