当前位置:   article > 正文

STM32CubeMX配置SD卡+DMA+Fatfs文件系统_stm32cubemx fatfs长文件名

stm32cubemx fatfs长文件名

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";
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

添加测试代码

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");
    }
	}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97

main函数添加代码
在这里插入图片描述

四、打印测试

在这里插入图片描述
成功
注:测试代码为网上复制
附件:百度网盘

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
  

闽ICP备14008679号