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

【经验分享】从 STM32Cube 库向标准外设库移植FatFs 文件系统

[复制链接]
STMCU小助手 发布时间:2022-3-3 22:09
前言
在很多应用中,文件系统被用来在存储介质上进行存储和管理文件数据。FatFs 作为面向小型嵌入式系统的一种通用 FAT 文件系统,被广泛使用。在 STM32Cube 库中,Middleware 是包含有 FatFs 文件系统的,而且有相关的例程。但是在标准外设库并没有相关的例程,这使得在使用标准外设库在开发项目中使用文件系统的话,需要自己再移植代码。其实,从STM32Cube 库向标准外设库移植 FatFs 文件系统很方便,简单实用。下面我们以 STM32F4 系列为例,来介绍一下 SD 卡的移植。

STM32CubeF4库中的 FatFs
大家都知道,进行 FatFs 移植主要就是在 diskio.c 中添加底层驱动代码。在 STM32CubeF4 库中,可以在\STM32Cube_FW_F4_V1.11.0\Middlewares\Third_Party\FatFs\src 找到 FatFs R0.11 的代码,细心的工程师会发现,里边的文件与从 FatFs 下载的 FatFs R0.11 不太一样,多了 ff_gen_drv.c 和 ff_gen_drv.h 两个文件,并且多了一个包含多种存储介质驱动的 driver 文件夹。这是因为 ST 的工程师在 FatFs 的代码中,已经为大家添加了各种存储介质的驱动代码,以方便大家使用,并使用 ff_gen_drv.c 来进行管理。所以,STM32CubeF4 的 FatFs 中间件模块架构如下:

([CW3PA_NHFHXKO5){V4LBR.png

从架构图可知,在 STM32CubeF4 解决方案中,已经添加额外的接口层,可以动态地添加/删除 FatFs 模块的物理介质。如需以底层磁盘 I/O 驱动来连接 FatFs 模块,用户可以使用 FATFS_LinkDriver()和 FATFS_UnLinkDriver() 动态地添加或者删除磁盘 I/O 驱动;应用程序可能需要知道当前连接的磁盘 I/O 驱动数量,这一点可通过 FATFS_GetAttachedDriversNbr() API 来实现。这让我们在管理物理介质更为方便。这三个函数就位于 ff_gen_drv.c 中。以 SD 卡为例,原本的“diskio.c”分化为“diskio.c + ff_gen_drv.c + \drivers\sd_diskio.c”三个文件,虽然文件增加了,但是条理更加清晰。具体的介绍可以参考用户手册 UM1721《在 STM32Cube 上开发 FatFs 相关应用》,这里不作详述。看完 UM1721,接下来可以开始动手实现了。

一实现环境
开发板: STM32439I-EVAL (MCU: STM32F439NIH6)
开发环境: STM32Cube_FW_F4_V1.11.0
STM32F4xx_DSP_StdPeriph_Lib_V1.6.1
IDE: IAR Embedded Workbench for ARM v7.50.1.10273

二实现步骤
1.在标准外设库上建立项目文件,确保 SD 卡可正常工作
1) 在\STM32F4xx_DSP_StdPeriph_Lib_V1.6.1\Project 下复制 STM32F4xx_StdPeriph_Templates 并将文件夹更名为 FatFs_uSD
2) 将\STM32F4xx_DSP_StdPeriph_Lib_V1.6.1\Project\STM32F4xx_StdPeriph_Examples\SDIO\SDIO_uSDCard 下的文件复制到 FatFs_uSD 替换掉原有文件
3) 打开 EWARM 下面的项目文件,在项目文件中添加\Utilities\STM32_EVAL\STM324x9I_EVAL 文件组,并加入stm324x9i_eval.c、stm324x9i_eval_ioe16.c、stm324x9i_eval_sdio_sd.c,如图:

)G]@)7`2NTZ)I6{Y9AA@D2T.png

4) 在 main.h 中将 include 中的代码加入 STM324x9I 的信息,更新为
  1.    #if defined (USE_STM324xG_EVAL)
  2.    #include "stm324xg_eval.h"
  3.    #include "stm324xg_eval_sdio_sd.h"
  4.    #elif defined (USE_STM324x7I_EVAL)
  5.    #include "stm324x7i_eval.h"
  6.    #include "stm324x7i_eval_sdio_sd.h"
  7.    #elif defined (USE_STM324x9I_EVAL)
  8.    #include "stm324x9i_eval.h"
  9.    #include "stm324x9i_eval_sdio_sd.h"
  10.    #else
  11.    #error "Please select first the Evaluation board used in your application (in Project
  12.    Options)"
  13.    #endif
复制代码

5) 编译,成功。下载运行,可见 LED1、LED2 和 LED3 点亮,证明 SD 卡读写正常。此步骤的目的在于首先要确保 SDIO 读写 SD 卡是正常的。

   2.移植 FatFs 代码
1) 将\STM32Cube_FW_F4_V1.11.0\Middlewares\Third_Party\下的 FatFs 文件夹复制到 \STM32F4xx_DSP_StdPeriph_Lib_V1.6.1\Libraries
2) 打开之前的项目,在项目中添加 FatFs 文件组,加入\drivers\sd_diskio.c、diskio.c、ff.c 和 ff_gen_drv.c,如图:

E@BSC54N4VXQNS72Y_$PZAI.png

3) 打开 Project Options,在 C/C++ Compiler 的 Preprocessor 页面添加 FatFs 使用的头文件的地址: $PROJ_DIR$\..\..\..\Libraries\FatFs\src 和$PROJ_DIR$\..\..\..\Libraries\FatFs\src\drivers,如下:

7IVW{U790MXFDS4GCLBT~{4.png

4) 复制\STM32Cube_FW_F4_V1.11.0\Projects\STM324x9I_EVAL\Applications\FatFs\FatFs_uSD\Inc 下的 ffconf.h 到\STM32F4xx_DSP_StdPeriph_Lib_V1.6.1\Project\Fatfs_uSDcard
5) 修改 ffconf.h,删掉第 15 行的 “#include "stm32f4xx_hal.h"”,将第 18 行的“#include "stm324x9i_eval_sd.h"”改为 “#include "stm324x9i_eval_sdio_sd.h"”
6) 修改 sd_diskio.c,这是整个移植最关键的地方。
    a. DSTATUS SD_initialize(BYTE lun)
    STM32CubeF4 中的 BSP_SD_Init()对应于标准外设库的 SD_Init(),MSD_OK 对应于 SD_OK。此函数修改为:
  1.     DSTATUS SD_initialize(BYTE lun)
  2.     {
  3.     Stat = STA_NOINIT;

  4. /* Configure the uSD device */
  5. if(SD_Init() == SD_OK)
  6. {
  7. Stat &= ~STA_NOINIT;
  8. }
  9. return Stat;
  10. }
复制代码

b. DSTATUS SD_status(BYTE lun)
STM32CubeF4 中的 BSP_SD_GetStatus ()对应于标准外设库的 SD_GetStatus() ()。此函数修改为:
  1. DSTATUS SD_status(BYTE lun)
  2. {
  3. Stat = STA_NOINIT;
  4. if(SD_GetStatus() == SD_TRANSFER_OK)
  5. {
  6. Stat &= ~STA_NOINIT;
  7. }

  8. return Stat;
  9. }
复制代码

c. DRESULT SD_read(BYTE lun, BYTE *buff, DWORD sector, UINT count)
在 STM32CubeF4 中,BSP_SD_ReadBlocks()函数调用 HAL_SD_ReadBlocks(),而 HAL_SD_ReadBlocks()已经针对读取单个 Block 和多个 Block 进行了处理。而在标准外设库中没有这样的函数,单个 Block 的读取使用SD_ReadBlock()函数,多个 Block 的读取使用 SD_ReadMultiBlocks()函数。所以,这个函数需要修改为:
  1. DRESULT SD_read(BYTE lun, BYTE *buff, DWORD sector, UINT count)
  2. {
  3. DRESULT res = RES_OK;

  4. if (count > 1)
  5. {
  6. /* Read multiple block */
  7. if(SD_ReadMultiBlocks((uint8_t *)buff,
  8. (uint64_t) (sector * BLOCK_SIZE),
  9. BLOCK_SIZE,
  10. count) != SD_OK)
  11. return RES_ERROR;
  12. /* Check if the Transfer is finished */
  13. if(SD_WaitReadOperation() != SD_OK)
  14. return RES_ERROR;
  15. while(SD_GetStatus() != SD_TRANSFER_OK);
  16. }
  17. else
  18. {
  19. /* Read block */
  20. if(SD_ReadBlock((uint8_t *)buff,
  21. (uint64_t) (sector * BLOCK_SIZE),
  22. BLOCK_SIZE) != SD_OK)
  23. return RES_ERROR;
  24. /* Check if the Transfer is finished */
  25. if(SD_WaitReadOperation() != SD_OK)
  26. return RES_ERROR;
  27. while(SD_GetStatus() != SD_TRANSFER_OK);
  28. }

  29. return res;
  30. }
复制代码

d. DRESULT SD_write(BYTE lun, const BYTE *buff, DWORD sector, UINT count)
SD_write()的修改和 SD_read()一样,需要分别对单个 Block 和多个 Block 进行处理。函数修改为:
  1. DRESULT SD_write(BYTE lun, const BYTE *buff, DWORD sector, UINT count)
  2. {
  3. DRESULT res = RES_OK;

  4. if (count > 1)
  5. {
  6. /* Write multiple block */
  7. if(SD_WriteMultiBlocks((uint8_t *)buff,
  8. (uint64_t) (sector * BLOCK_SIZE),
  9. BLOCK_SIZE,
  10. count) != SD_OK)
  11. return RES_ERROR;
  12. /* Check if the Transfer is finished */
  13. if(SD_WaitWriteOperation() != SD_OK)
  14. return RES_ERROR;
  15. while(SD_GetStatus() != SD_TRANSFER_OK);
  16. }
  17. else
  18. {
  19. /* Write block */
  20. if(SD_WriteBlock((uint8_t *)buff,
  21. (uint64_t) (sector * BLOCK_SIZE),
  22. BLOCK_SIZE) != SD_OK)
  23. return RES_ERROR;
  24. /* Check if the Transfer is finished */
  25. if(SD_WaitWriteOperation() != SD_OK)
  26. return RES_ERROR;
  27. while(SD_GetStatus() != SD_TRANSFER_OK);
  28. }

  29. return res;
  30. }
复制代码

e. DRESULT SD_ioctl(BYTE lun, BYTE cmd, void *buff)
STM32CubeF4 中的 BSP_SD_GetCardInfo()对应于标准外设库的 SD_GetCardInfo()。此函数修改为:
  1. DRESULT SD_ioctl(BYTE lun, BYTE cmd, void *buff)
  2. {
  3. DRESULT res = RES_ERROR;
  4. SD_CardInfo CardInfo;

  5. if (Stat & STA_NOINIT) return RES_NOTRDY;

  6. switch (cmd)
  7. {
  8. /* Make sure that no pending write process */
  9. case CTRL_SYNC :
  10. res = RES_OK;
  11. break;

  12. /* Get number of sectors on the disk (DWORD) */
  13. case GET_SECTOR_COUNT :
  14. SD_GetCardInfo(&CardInfo);
  15. *(DWORD*)buff = CardInfo.CardCapacity / BLOCK_SIZE;
  16. res = RES_OK;
  17. break;

  18. /* Get R/W sector size (WORD) */
  19. case GET_SECTOR_SIZE :
  20. *(WORD*)buff = BLOCK_SIZE;
  21. res = RES_OK;
  22. break;

  23. /* Get erase block size in unit of sector (DWORD) */
  24. case GET_BLOCK_SIZE :
  25. *(DWORD*)buff = BLOCK_SIZE;
  26. break;

  27. default:
  28. res = RES_PARERR;
  29. }

  30. return res;
  31. }
复制代码

至此,编译可通过,FatFs 的移植已算完成。接下来,是修改测试代码。
7) 修改 main.c。参考\STM32Cube_FW_F4_V1.11.0\Projects\STM324x9I_EVAL\Applications\FatFs\FatFs_uSD\Src\main.c,修改此文件,如下:
  1. /* Includes ------------------------------------------------------------------*/
  2. #include "main.h"
  3. /** @addtogroup STM32F4xx_StdPeriph_Examples
  4. * @{
  5. */
  6. /** @addtogroup SDIO_uSDCard
  7. * @{
  8. */
  9. /* Private typedef -----------------------------------------------------------*/
  10. typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus;
  11. /* Private define ------------------------------------------------------------*/
  12. /* Private macro -------------------------------------------------------------*/
  13. /* Private variables ---------------------------------------------------------*/
  14. FATFS SDFatFs; /* File system object for SD card logical drive */
  15. FIL MyFile; /* File object */
  16. char SDPath[4]; /* SD card logical drive path */
  17. /* Private function prototypes -----------------------------------------------*/
  18. static void NVIC_Configuration(void);
  19. static void Error_Handler(void);
  20. /* Private functions ---------------------------------------------------------*/
  21. /**
  22. * @brief Main program
  23. * @param None
  24. * @retval None
  25. */
  26. int main(void)
  27. {
  28. /*!< At this stage the microcontroller clock setting is already configured,
  29. this is done through SystemInit() function which is called from startup
  30. files (startup_stm32f40_41xxx.s/startup_stm32f427_437xx.s)
  31. before to branch to application main. To reconfigure the default setting
  32. of SystemInit() function, refer to system_stm32f4xx.c file
  33. */
  34. FRESULT res; /* FatFs function common result
  35. code */
  36. uint32_t byteswritten, bytesread; /* File write/read counts */
  37. uint8_t wtext[] = "STM32 with FatFs demo @ std lib"; /* File write buffer */
  38. uint8_t rtext[100]; /* File read buffer */
  39. /* Initialize LEDs available on EVAL board */
  40. STM_EVAL_LEDInit(LED1);
  41. STM_EVAL_LEDInit(LED3);
  42. /* NVIC Configuration */
  43. NVIC_Configuration();
  44. /*##-1- Link the micro SD disk I/O driver ##################################*/
  45. if(FATFS_LinkDriver(&SD_Driver, SDPath) == 0)
  46. {
  47. /*##-2- Register the file system object to the FatFs module ##############*/
  48. if(f_mount(&SDFatFs, (TCHAR const*)SDPath, 0) != FR_OK)
  49. {
  50. /* FatFs Initialization Error */
  51. Error_Handler();
  52. }
  53. else
  54. {
  55. /*##-3- Create a FAT file system (format) on the logical drive #########*/
  56. /* WARNING: Formatting the uSD card will delete all content on the device */
  57. if(f_mkfs((TCHAR const*)SDPath, 0, 0) != FR_OK)
  58. {
  59. /* FatFs Format Error */
  60. Error_Handler();
  61. }
  62. else
  63. {
  64. /*##-4- Create and Open a new text file object with write access #####*/
  65. if(f_open(&MyFile, "STM32STD.TXT", FA_CREATE_ALWAYS | FA_WRITE) != FR_OK)
  66. {
  67. /* 'STM32STD.TXT' file Open for write Error */
  68. Error_Handler();
  69. }
  70. else
  71. {
  72. /*##-5- Write data to the text file ################################*/
  73. res = f_write(&MyFile, wtext, sizeof(wtext), (void *)&byteswritten);

  74. if((byteswritten == 0) || (res != FR_OK))
  75. {
  76. /* 'STM32STD.TXT' file Write or EOF Error */
  77. Error_Handler();
  78. }
  79. else
  80. {
  81. /*##-6- Close the open text file #################################*/
  82. f_close(&MyFile);

  83. /*##-7- Open the text file object with read access ###############*/
  84. if(f_open(&MyFile, "STM32STD.TXT", FA_READ) != FR_OK)
  85. {
  86. /* 'STM32STD.TXT' file Open for read Error */
  87. Error_Handler();
  88. }
  89. else
  90. {
  91. /*##-8- Read data from the text file ###########################*/
  92. res = f_read(&MyFile, rtext, sizeof(rtext), (UINT*)&bytesread);

  93. if((bytesread == 0) || (res != FR_OK))
  94. {
  95. /* 'STM32STD.TXT' file Read or EOF Error */
  96. Error_Handler();
  97. }
  98. else
  99. {
  100. /*##-9- Close the open text file #############################*/
  101. f_close(&MyFile);

  102. /*##-10- Compare read data with the expected data ############*/
  103. if((bytesread != byteswritten))
  104. {
  105. /* Read data is different from the expected data */
  106. Error_Handler();
  107. }
  108. else
  109. {
  110. /* Success of the demo: no error occurrence */
  111. STM_EVAL_LEDOn(LED1);
  112. }
  113. }
  114. }
  115. }
  116. }
  117. }
  118. }
  119. }

  120. /*##-11- Unlink the micro SD disk I/O driver ###############################*/
  121. FATFS_UnLinkDriver(SDPath);

  122. /* Infinite loop */
  123. while (1)
  124. {
  125. }
  126. }
  127. /**
  128. * @brief Configures SDIO IRQ channel.
  129. * @param None
  130. * @retval None
  131. */
  132. static void NVIC_Configuration(void)
  133. {
  134. NVIC_InitTypeDef NVIC_InitStructure;
  135. /* Configure the NVIC Preemption Priority Bits */
  136. NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
  137. NVIC_InitStructure.NVIC_IRQChannel = SDIO_IRQn;
  138. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  139. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  140. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  141. NVIC_Init(&NVIC_InitStructure);
  142. NVIC_InitStructure.NVIC_IRQChannel = SD_SDIO_DMA_IRQn;
  143. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  144. NVIC_Init(&NVIC_InitStructure);
  145. }
  146. /**
  147. * @brief This function is executed in case of error occurrence.
  148. * @param None
  149. * @retval None
  150. */
  151. static void Error_Handler(void)
  152. {
  153. /* Turn LED3 on */
  154. STM_EVAL_LEDOn(LED3);
  155. while(1)
  156. {
  157. }
  158. }
  159. #ifdef USE_FULL_ASSERT
  160. /**
  161. * @brief Reports the name of the source file and the source line number
  162. * where the assert_param error has occurred.
  163. * @param file: pointer to the source file name
  164. * @param line: assert_param error line source number
  165. * @retval None
  166. */
  167. void assert_failed(uint8_t* file, uint32_t line)
  168. {
  169. /* User can add his own implementation to report the file name and line number,
  170. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  171. /* Infinite loop */
  172. while (1)
  173. {
  174. }
  175. }
  176. #endif
复制代码
还需要在 main.h 中加入:
  1. /* FatFs includes component */
  2. #include "ff_gen_drv.h"
  3. #include "sd_diskio.h"
复制代码

此主函数实现功能为:创建 STM32STD.TXT 文件,并向其写入“STM32 with FatFs demo @ std lib”
8) 编译,成功。
3.测试
1) 连接 STM32439I-EVAL 到 PC
2) 打开工程,下载代码并运行,LED1 点亮,代表运行通过。
3) 将 SD 卡从 STM32439I-EVAL 取出,放入 PC 进行读取,可见:

RIW0SYF]40H62D6F6G9I(H4.png

4) 打开 STM32STD.TXT,可见:

]LQTA@JB){ZS5Q@]U882S76.png

5) 测试结果:成功。

结论
此文档介绍从 STM32CubeF4 中将 FatFs 文件系统移植到标准外设库的实现过程。另外,对于时间接口函数,有需要的读者可自行进行移植。





















收藏 评论0 发布时间:2022-3-3 22:09

举报

0个回答

所属标签

相似分享

官网相关资源

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