STM32CubeMX配置SD卡+DMA+Fatfs文件系统
程序员文章站
2022-06-08 20:57:41
...
STM32CubeMX配置SD卡+DMA+Fatfs文件系统
一、设备及软件
1、keil
2、STM32CubeMX
3、正点原子STM32F407探索者开发板
二、配置步骤
1、配置RCC外部晶振和SYS为SW模式(看之前配置方式)
2、配置USART1(调试使用)
3、时钟树配置,SDIO模块输入时钟为48MHZ
4,、SDIO配置
SDIO时钟SDIO_CK = 48MHz/(CLKDIV+2)。即12MHZ
打开SDIO中断
添加DMA
5、配置Fatfs文件系统
支持中文及长文件名
使用DMA模板
SD卡插入检测引脚,探索版不带此功能,引脚这边必须配置一个引脚,否则生成文件会报错,任意设置一引脚使用,生成工程后注释代码
配置检测引脚
6、配置NVIC
SDIO中断优先级必须大于DMA
7、生成代码配置,必须加大堆栈内存,否则可能不够
8、生成代码
三、修改代码
1、打开bsp_driver_sd.c文件,修改__weak uint8_t BSP_SD_IsDetected(void)如下
原因:探索者开发板没有SD卡检测引脚
2、修改usart.c,添加如下代码
3、修改主函数测试代码
添加变量
#define u8 uint8_t
#define u16 uint16_t
#define u32 uint32_t
FATFS fs;
FIL fil;
uint32_t total,mfree; // file objects
uint32_t byteswritten; /* File write counts */
uint32_t bytesread; /* File read counts */
uint8_t wtext[] = "This is STM32 working with FatFs"; /* File write buffer */
uint8_t rtext[100]; /* File read buffers */
char filename[] = "STM32cube.txt";
添加测试代码
u8 exf_getfree(u8 *drv,u32 *total,u32 *free)
{
FATFS *fs1;
u8 res;
u32 fre_clust=0, fre_sect=0, tot_sect=0;
res =(u32)f_getfree((const TCHAR*)drv, (DWORD*)&fre_clust, &fs1);
if(res==0)
{
tot_sect=(fs1->n_fatent-2)*fs1->csize;
fre_sect=fre_clust*fs1->csize;
#if _MAX_SS!=512
tot_sect*=fs1->ssize/512;
fre_sect*=fs1->ssize/512;
#endif
*total=tot_sect>>1;
*free=fre_sect>>1;
}
return res;
}
void Fatfs_RW_test(void)
{
printf("\r\n ****** FatFs Example ******\r\n\r\n");
/*##-1- Register the file system object to the FatFs module ##############*/
retSD = f_mount(&fs, "0", 1);
//f_mkdir("0:dxc");
if(retSD)
{
printf(" mount error : %d \r\n",retSD);
Error_Handler();
}
else
printf(" mount sucess!!! \r\n");
/*##-2- Create and Open new text file objects with write access ######*/
retSD=exf_getfree("0:",&total,&mfree);
if(retSD==0)
printf(" total : %d MB,free : %d MB \r\n",total>>10,mfree>>10);
else
printf(" getfree error!!! \r\n");
retSD = f_open(&fil, filename, FA_CREATE_ALWAYS | FA_WRITE);
if(retSD)
printf(" open file error : %d\r\n",retSD);
else
printf(" open file sucess!!! \r\n");
/*##-3- Write data to the text files ###############################*/
retSD = f_write(&fil, wtext, sizeof(wtext), (void *)&byteswritten);
if(retSD)
printf(" write file error : %d\r\n",retSD);
else
{
printf(" write file sucess!!! \r\n");
printf(" write Data : %s\r\n",wtext);
}
/*##-4- Close the open text files ################################*/
retSD = f_close(&fil);
if(retSD)
printf(" close error : %d\r\n",retSD);
else
printf(" close sucess!!! \r\n");
/*##-5- Open the text files object with read access ##############*/
retSD = f_open(&fil, filename, FA_READ);
if(retSD)
printf(" open file error : %d\r\n",retSD);
else
printf(" open file sucess!!! \r\n");
/*##-6- Read data from the text files ##########################*/
retSD = f_read(&fil, rtext, sizeof(rtext), (UINT*)&bytesread);
if(retSD)
printf(" read error!!! %d\r\n",retSD);
else
{
printf(" read sucess!!! \r\n");
printf(" read Data : %s\r\n",rtext);
}
/*##-7- Close the open text files ############################*/
retSD = f_close(&fil);
if(retSD)
printf(" close error!!! %d\r\n",retSD);
else
printf(" close sucess!!! \r\n");
/*##-8- Compare read data with the expected data ############*/
if(bytesread == byteswritten)
{
printf(" FatFs is working well!!!\r\n");
}
}
main函数添加代码
四、打印测试
成功
注:测试代码为网上复制