你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

【经验分享】STM32 FatFs文件系统移植

[复制链接]
STMCU小助手 发布时间:2022-4-7 18:02
一、把FatFs源码拷贝到工程目录,此此移植文件系统是基于之前SPI_FLASH的工程下进行。

源码地址:Petit FAT File System Module。

FXOBFCJ3K9L[~4{9NOSOKW3.png

二、在keil打开工程文件,将源码添加到工程中。

SF[`BVY4XT4]PF(BKZ15RWV.png

三、添加FatFs源码的头文件。

TIA[82}~T28`T_Y5`2]GXY7.png

四、尝试构建程序,解决错误。

        主要错误为函数未定义却使用了。可以进行删除,或者实现即可。

五、实现FatFs底层驱动的接口。

a.disk_status  获取设备状态的接口。示例代码如下:

  1. DSTATUS disk_status (
  2.         BYTE pdrv                /* Physical drive nmuber to identify the drive */
  3. )
  4. {
  5.         DSTATUS stat=STA_NOINIT;
  6.         switch (pdrv) {
  7.         case DEV_FLASH :
  8.                 {
  9.                     if(SPI_FLASH_ReadID()==sFLASH_ID)
  10.                       stat&=~STA_NOINIT;
  11.                     else
  12.                       stat=STA_NOINIT;
  13.                 }
  14.                 return stat;        
  15.         }
  16.         return STA_NOINIT;
  17. }
复制代码


b.disk_initialize 初始化设备的接口。示例代码如下:

  1. DSTATUS disk_initialize (
  2.         BYTE pdrv                                /* Physical drive nmuber to identify the drive */
  3. )
  4. {
  5.         DSTATUS stat=STA_NOINIT;
  6.         switch (pdrv) {
  7.         case  DEV_FLASH :
  8.                 {
  9.                     SPI_FLASH_Init();
  10.                     if(SPI_FLASH_ReadID()==sFLASH_ID)
  11.                       stat&=~STA_NOINIT;
  12.                     else
  13.                       stat=STA_NOINIT;
  14.                 }
  15.                 return stat;
  16.         }
  17.         return STA_NOINIT;
  18. }
复制代码

c.disk_read 读。示例代码如下:

  1. DRESULT disk_read (
  2.         BYTE pdrv,                /* Physical drive nmuber to identify the drive */
  3.         BYTE *buff,                /* Data buffer to store read data */
  4.         LBA_t sector,        /* Start sector in LBA */
  5.         UINT count                /* Number of sectors to read */
  6. )
  7. {
  8.         DRESULT res;
  9.         switch (pdrv) {
  10.         case  DEV_FLASH :
  11.                 {
  12.                         SPI_FLASH_BufferRead(buff, sector*4096, count*4096);
  13.                         res=RES_OK;
  14.                 }
  15.                 return res;
  16.         }
  17.         return RES_PARERR;
  18. }
复制代码


d.disk_write 写。示例代码如下:

  1. DRESULT disk_write (
  2.         BYTE pdrv,                        /* Physical drive nmuber to identify the drive */
  3.         const BYTE *buff,        /* Data to be written */
  4.         LBA_t sector,                /* Start sector in LBA */
  5.         UINT count                        /* Number of sectors to write */
  6. )
  7. {
  8.         DRESULT res;
  9.         switch (pdrv) {
  10.         case  DEV_FLASH :
  11.                 {
  12.                         SPI_FLASH_SectorErase(sector*4096);         
  13.                         SPI_FLASH_BufferWrite((BYTE *)buff, sector*4096, count*4096);
  14.                         res=RES_OK;
  15.                 }
  16.                 return res;
  17.         }
  18.         return RES_PARERR;
  19. }
复制代码

e.disk_ioctl 设备属性接口。示例代码如下:

  1. DRESULT disk_ioctl (
  2.         BYTE pdrv,                /* Physical drive nmuber (0..) */
  3.         BYTE cmd,                /* Control code */
  4.         void *buff                /* Buffer to send/receive control data */
  5. )
  6. {
  7.         DRESULT res;
  8.         switch (pdrv) {
  9.         case  DEV_FLASH :
  10.                 {
  11.                         switch (cmd) {
  12.                                 /* 扇区数量:2048*4096/1024/1024=8(MB) */
  13.                                       case GET_SECTOR_COUNT:
  14.                                      *(DWORD * )buff = 2048;               
  15.                                               break;
  16.                                             /* 扇区大小  */
  17.                                              case GET_SECTOR_SIZE :
  18.                                              *(WORD * )buff = 4096;
  19.                                      break;
  20.                                       /* 同时擦除扇区个数 */
  21.                                         case GET_BLOCK_SIZE :
  22.                                                *(DWORD * )buff = 1;
  23.                                              break;        
  24.                                       }
  25.                         res=RES_OK;
  26.                 }
  27.                 return res;        
  28.         }
  29.         return RES_PARERR;
  30. }
复制代码

f.DWORD get_fattime 获取时间。示例代码如下:

  1. DWORD get_fattime (void)
  2. {
  3.         return ((2021-1980)<<(25)|(1)<<(21)|(1)<<(16)|(1)<<(11)|(1)<<(5)|(2)<<(0));
  4. }
复制代码

六、修改ffconf.h,配置文件系统的具体功能。

a.FF_USE_MKFS        1        使用格式化的功能

b.FF_CODE_PAGE     936        修改编码页以支持中文文件名

c.FF_USE_LFN        0        支持长文件名

d.FF_MAX_SS        4096        修改支持最大的设备扇区大小,与flash设备对应。

七、尝试使用文件系统来测试是否移植成功。

常规使用流程

a.挂在文件系统到一个设备。使用函数:f_mount()

b.打开文件。使用函数:f_open()

c.数据的读取与写入

d.关闭文件。使用函数:f_close()

注意:测试过程可使用返回值判断是否移植成功。在挂载设备时,存在错误FR_NO_FILESYSTEM(13),可采用函数f_mkfs进行解决,该函数用于格式化文件系统。相关代码如下:

  1. printf("\r\nFatFs测试实验\r\n");
  2.         res=f_mount(&flash_fs, "", 1);
  3.          if(res==FR_OK)
  4.         {
  5.                 printf("\r\n文件系统挂载成功!\r\n");
  6.         }
  7.         else
  8.         {
  9.                 printf("\r\nf_mount错误代码:%d\r\n",res);
  10.                 if(res == FR_NO_FILESYSTEM)
  11.                 {
  12.                         res=f_mkfs("", 0, work, sizeof work);
  13.                         if(res==FR_OK)
  14.                         {
  15.                                    printf("\r\n文件系统格式化成功!\r\n");
  16.                         }
  17.                         else
  18.                         {
  19.                                   printf("\r\nf_mkfs错误代码:%d\r\n",res);
  20.                         }
  21.                 }
  22.         }
  23.         //打开文件,写入数据,关闭文件
  24.          res = f_open(&flash_fil, "hello.txt", FA_OPEN_ALWAYS|FA_WRITE|FA_READ);
  25.          if(res==FR_OK)
  26.         {
  27.                 printf("\r\nf_open测试成功!\r\n");
  28.         }
  29.         else
  30.         {
  31.                 printf("\r\nf_open错误代码:%d\r\n",res);
  32.         }
  33.         //
  34.         printf("\r\n正在写入:%s\r\n",Tx_Buffer);
  35.         res=f_write(&flash_fil, Tx_Buffer,BufferSize,&Num_Of_Written);
  36.         if(res==FR_OK)
  37.         {
  38.                 printf("\r\nf_write测试成功!\r\n");
  39.         }
  40.         else
  41.         {
  42.                 printf("\r\nf_write错误代码:%d\r\n",res);
  43.         }
  44.         //
  45.         f_close(&flash_fil);
  46.         //打开文件,读取数据,关闭文件
  47.         res = f_open(&flash_fil, "hello.txt", FA_OPEN_ALWAYS|FA_WRITE|FA_READ);
  48.          if(res==FR_OK)
  49.         {
  50.                 printf("\r\nf_open测试成功!\r\n");
  51.         }
  52.         else
  53.         {
  54.                 printf("\r\nf_open错误代码:%d\r\n",res);
  55.         }
  56.         //
  57.         res=f_read(&flash_fil, Rx_Buffer, BufferSize, &Num_Of_Read);
  58.         printf("\r\n读取完成:%s\r\n",Rx_Buffer);
  59.         if(res==FR_OK)
  60.         {
  61.                 printf("\r\nf_read测试成功!\r\n");
  62.         }
  63.         else
  64.         {
  65.                 printf("\r\nf_read错误代码:%d\r\n",res);
  66.         }
  67.         //
  68.         f_close(&flash_fil);
复制代码


结果展示:

0(J
N[9NXU1~3KU6EIMO9.png


收藏 评论0 发布时间:2022-4-7 18:02

举报

0个回答

所属标签

相似技术帖

官网相关资源

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版