STM32CubeMx入門教程(10):Fatfs文件系統的應用

2023-07-12 12:04:57 來源:嵌入式開發始站

導語"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.   * @parampData: 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 */ /* canbe 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 == 1DRESULT 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 */

編譯下載進行測試:

第四節效果演示

我們可以看見文件讀寫測試能夠正常使用。

標簽:

上一篇:FPGA與CPU之間是如何通信的?
下一篇:最后一頁