D3-嵌入式文件系统

1.FAT32文件系统引入

为什么需要文件系统

浏览器如何从服务器获取网页?

image-20200327223213533

文件系统的作用是什么

image-20200327223850303

磁盘原理

img

硬盘容量 = 盘面数 × 柱面数 × 扇区数 × 512字节

image-20200327224907782

SD卡存储结构

image-20200327231256119

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

磁盘管理工具

img

img

image-20200331184509087

FAT32存储结构

在这里插入图片描述

2.Fat32文件存储原理

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

Fat32怎么管理文件

簇---- 文件存储的最小单元

image-20200331104732792

文件信息

image-20200331103706237

100KB空间如何管理文件

分配方案

image-20200331103828525

目录分配方案

image-20200331103927988

当文件需要删除/增加

image-20200331104108367

Fat32 FAT表

添加FAT表

image-20200331104309696

FAT表格式

image-20200331104353978

FAT表解决文件删除/添加问题

image-20200331104500239

Fat32存储原理

image-20200331104709939

FAT表取值

image-20200331104813049

FAT 目录项

短文件名目录项

image-20200331105102358

长文件名目录项

image-20200331105321679

3.FatFS介绍及STM32集成

FatFS介绍

官方网站

http://elm-chan.org/fsw/ff/00index_e.html

image-20200331231943911

应用接口

image-20200331232318158

介质访问接口

image-20200331232433069

相关资源

image-20200331232845329

STM32Cube集成FatFS

FatFs 中间件模块架构

image-20200331233620078

FatFS配置

  1. 打开FatFS
  2. 使能磁盘为SD卡
  3. 配置中文编码
  4. 配置命名空间为HEAP

1563182116733-1585669197094

  1. 增大C库堆空间

1563182334485

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提供的应用接口

image-20200402102203722

API学习方法

学习步骤

Created with Raphaël 2.3.0 函数原型分析 典型代码案例阅读 应用编程

项目当中用到的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)

image-20200402150157168

怎么存储历史数据呢

历史数据的目的是什么

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

CSV格式文件

image-20200402150931599

历史数据存储功能实现

Created with Raphaël 2.3.0 创建文件 格式化文件流 文件写入

6.FatFS底层实现

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

通用底层驱动API

image-20200402161205729

驱动包含哪些文件

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实现

驱动装载

Created with Raphaël 2.3.0 MX_FATFS_Init FATFS_LinkDriver FATFS_LinkDriverEx
/**
  * @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;
}
  • 20
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值