STM32CubeMx入门教程(10):Fatfs文件系统的应用

2023-09-13  

导语"fatfs是一个小型的文件系统,在小型的嵌入式系统中使用非常的广泛,STM32CubeMx自带该文件系统,我们通过简单的配置就能够使用,将前面的SD卡的读写操作进行修改,将文件系统挂载到SD卡进行操作,通过简单的文件创建、打开、写入、读取、关闭来演示如果使用Fatfs。"

第一节 系统要求

•软件


STM32CubeMX

•硬件

野火指南者开发板,SD卡


第二节 CubeMX配置

本节使用SDIO接口与SD卡连接,利用DMA模式读写SD卡,将FATFS文件系统挂载SD卡中。

1.SDIO配置

图片

图片

图片

1.DMA配置

图片

2.中断配置

图片

3.Fatfs 文件系统的配置

Fatfs配置中我们选在支持中文文件名。选在SD Card,设置为logical drivers 为1.

图片

按照上述配置后点击代码生成。

第三节 MDK 代码编写

使用MDK打开项目,在bspdriversd.c 中修改代码,并包含头文件#include "sdio.h"。

在BSPSDReadBlocks() 函数中修改,使用前面编写好的SDIO DMA读写的函数进行SD卡的读写,Fatfs文件系统将会在SDRead() 和SDWrite()调用该函数。

图片

图片

/* USER CODE END BeforeReadBlocksSection */ 

/** 

  * @brief  Reads block(s) from a specified address in an SD card, in polling mode. 

  * @param  pData: Pointer to the buffer that will contain the data to transmit 

  * @param  ReadAddr: Address from where data is to be read 

  * @param  NumOfBlocks: Number of SD blocks to read 

  * @param  Timeout: Timeout for read operation 

  * @retval SD status 

  */ 

__weak uint8_t BSP_SD_ReadBlocks(uint32_t *pData, uint32_t ReadAddr, uint32_t NumOfBlocks, uint32_t Timeout) 

  uint8_t sd_state = MSD_OK; 



  if (SDIO_ReadBlocks_DMA(&hsd, (uint8_t*)pData,ReadAddr,NumOfBlocks) != HAL_OK) 

  { 

    sd_state = MSD_ERROR; 

  } 



  return sd_state; 



/* USER CODE BEGIN BeforeWriteBlocksSection */ 

/* can be used to modify previous code / undefine following code / add code */ 

/* USER CODE END BeforeWriteBlocksSection */ 

/** 

  * @brief  Writes block(s) to a specified address in an SD card, in polling mode. 

  * @param  pData: Pointer to the buffer that will contain the data to transmit 

  * @param  WriteAddr: Address from where data is to be written 

  * @param  NumOfBlocks: Number of SD blocks to write 

  * @param  Timeout: Timeout for write operation 

  * @retval SD status 

  */ 

__weak uint8_t BSP_SD_WriteBlocks(uint32_t *pData, uint32_t WriteAddr, uint32_t NumOfBlocks, uint32_t Timeout) 

  uint8_t sd_state = MSD_OK; 



  if (SDIO_WriteBlocks_DMA(&hsd, (uint8_t*)pData,WriteAddr,NumOfBlocks) != HAL_OK) 

  { 

    sd_state = MSD_ERROR; 

  } 



  return sd_state; 

}

直接修改SD的读写函数,在内部使用DMA模式。也可以在sddiskio.c 中修改SDread 和SD_Write 但是发现这个文件是只读模式,所以就不修改了。:


/**

  * @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)

{

  DRESULT res = RES_ERROR;

  uint32_t timeout = 100000;



  if(BSP_SD_ReadBlocks((uint32_t*)buff, 

                       (uint32_t) (sector), 

                       count, SD_DATATIMEOUT) == MSD_OK)

  {

    while(BSP_SD_GetCardState()!= MSD_OK)

    {

      if (timeout-- == 0)

      {

        return RES_ERROR;

      }

    }

    res = RES_OK;

  }


  return res;

}



/**

  * @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

  */

#if _USE_WRITE == 1

DRESULT SD_write(BYTE lun, const BYTE *buff, DWORD sector, UINT count)

{

  DRESULT res = RES_ERROR;

  uint32_t timeout = 100000;



  if(BSP_SD_WriteBlocks((uint32_t*)buff, 

                        (uint32_t)(sector), 

                        count, SD_DATATIMEOUT) == MSD_OK)

  {

    while(BSP_SD_GetCardState()!= MSD_OK)

    {

      if (timeout-- == 0)

      {

        return RES_ERROR;

      }

    }    

    res = RES_OK;

  }


  return res;

}

#endif /* _USE_WRITE == 1 */


编译下载进行测试:

第四节效果演示

图片

我们可以看见文件读写测试能够正常使用。


文章来源于:电子工程世界    原文链接
本站所有转载文章系出于传递更多信息之目的,且明确注明来源,不希望被转载的媒体或个人可与我们联系,我们将立即进行删除处理。