【前言】
在前面我分享了几篇SD驱动的文章,这一篇在前面SD驱动好的基础之上进行FatFs文件系统的移植。
STM32F769I-DISC1是没有FatFs的例程的,但是STM32F769I_EVAL有他的基础例程,因此,我先参照他中间的例程进行了学习,但是也不是一帆风顺,折腾许久也没有弄好,首先是考虑是不是我的SD卡有问题,后面我又翻出来我的STM32F746-DISCOVER来验证了一下,发现SD卡没有问题。后面参照STM32F746的例程,就可以成功运行FatFs文件系统了。现记录如下:
【软件环境】
keil mdk 5.3
【参照例程】
STM32Cube_FW_F7_V1.17.2版本的STM32F746的无操作系统FatFs。以及STM32F769I-DSICOVER的BSP库。
【移植方法】
1、复制一份BSP库项目文件库。
2、BSP是全功能的测试工具,包含了SD、LCD、音频、触摸。由于我的开发板没有配官方的屏,所以我先用mdk打开后,只保留了SD的驱动,把LCD、音频、触摸的驱动从工程中删除。
3、将Middlewares\Third_Party\FatFs\src下的三个文件diskio.c、ffc、ffgen_drv.c添加到工程中。

以及option目录下的syscall.c、unicode.c添加进工程中。添加后分组如下图所示:

5、把STM32F746下的示例中的sd_diskio.c/h以及配置文件ffconf.h复制到工程中来,我放到的User分组中。

6、编译后无错误,说明STM32F7系列的包可以通用。
7、在main.c中添加测试代码:
#include "main.h"
#include "stlogo.h"
#include "usart.h"
extern UART_HandleTypeDef huart1;
FATFS SDFatFs; /* File system object for SD card logical drive */
FIL MyFile; /* File object */
char SDPath[4]; /* SD card logical drive path */
uint8_t workBuffer[_MAX_SS];
/** @addtogroup STM32F7xx_HAL_Examples
* @{
*/
/** @addtogroup BSP
* @{
*/
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
__IO uint8_t DemoIndex = 0;
__IO uint8_t NbLoop = 1;
__IO uint32_t SdmmcTest = 0;
__IO uint32_t SdramTest = 0;
/* Private function prototypes -----------------------------------------------*/
static void MPU_Config(void);
void SystemClock_Config(void);
static void CPU_CACHE_Enable(void);
/* Private functions ---------------------------------------------------------*/
/**
* @brief Main program
* @param None
* @retval None
*/
int main(void)
{
FRESULT res; /* FatFs function common result code */
uint32_t byteswritten, bytesread; /* File write/read counts */
uint8_t wtext[] = "This is STM32 working with FatFs"; /* File write buffer */
uint8_t rtext[100];
MPU_Config();
CPU_CACHE_Enable();
HAL_Init();
SystemClock_Config();
BSP_LED_Init(LED_GREEN);
BSP_LED_Init(LED_RED);
BSP_LED_On(LED_GREEN);
MX_USART1_UART_Init();
/*##-1- Link the micro SD disk I/O driver ##################################*/
if(FATFS_LinkDriver(&SD_Driver, SDPath) == 0)
{
/*##-2- Register the file system object to the FatFs module ##############*/
if(f_mount(&SDFatFs, (TCHAR const*)SDPath, 0) != FR_OK)
{
/* FatFs Initialization Error */
Error_Handler();
}
else
{
/*##-3- Create a FAT file system (format) on the logical drive #########*/
/* WARNING: Formatting the uSD card will delete all content on the device */
if(f_mkfs((TCHAR const*)SDPath, FM_ANY, 0, workBuffer, sizeof(workBuffer)) != FR_OK)
{
/* FatFs Format Error */
Error_Handler();
}
else
{
/*##-4- Create and Open a new text file object with write access #####*/
if(f_open(&MyFile, "STM32.TXT", FA_CREATE_ALWAYS | FA_WRITE) != FR_OK)
{
/* 'STM32.TXT' file Open for write Error */
Error_Handler();
}
else
{
/*##-5- Write data to the text file ################################*/
res = f_write(&MyFile, wtext, sizeof(wtext), (void *)&byteswritten);
if((byteswritten == 0) || (res != FR_OK))
{
/* 'STM32.TXT' file Write or EOF Error */
Error_Handler();
}
else
{
/*##-6- Close the open text file #################################*/
f_close(&MyFile);
/*##-7- Open the text file object with read access ###############*/
if(f_open(&MyFile, "STM32.TXT", FA_READ) != FR_OK)
{
/* 'STM32.TXT' file Open for read Error */
Error_Handler();
}
else
{
/*##-8- Read data from the text file ###########################*/
res = f_read(&MyFile, rtext, sizeof(rtext), (UINT*)&bytesread);
if((bytesread == 0) || (res != FR_OK))
{
/* 'STM32.TXT' file Read or EOF Error */
Error_Handler();
}
else
{
/*##-9- Close the open text file #############################*/
f_close(&MyFile);
/*##-10- Compare read data with the expected data ############*/
if((bytesread != byteswritten))
{
/* Read data is different from the expected data */
Error_Handler();
}
else
{
/* Success of the demo: no error occurrence */
BSP_LED_On(LED_RED);
}
}
}
}
}
}
}
}
/*##-11- Unlink the micro SD disk I/O driver ###############################*/
FATFS_UnLinkDriver(SDPath);
// SD_demo();
/* Wait For User inputs */
while (1)
{
}
}把SD卡格式化好后,插入开发板,把程序下载到开发板后。运行程序,发现LED_RED成功点亮,说明运行成功,我再把SD卡插进我的电脑里检查,发现已经成功的建立了STM32.TXT文件,内容也跟写入的一样。

【总结】
我在使用vsocde移植时,卡的信息获取是成功的,但是FatFs总可以挂载SD卡,但是建立文件系统一直不成功,现在用MDK来验证了。注意的是我们使用BSP工程,需要手工把栈空间要设置大一点,要不申请内存是失败,在f_mkfs时返回错误。
后面我将参观这个工程的源码再在vscode代码中验证。
我要赚赏金
