基于STM32F407格式化SD卡

  这段时间老师布置了个小课题,如何提高SD卡读取速度。在做测试的过程中,将SD卡内存写满了,
  U盘插电脑上一直读不出SD卡的内容,倒腾了一下午都没有解决。
  最后通过调用FATFS文件系统的格式化函数,将SD卡进行初始化,总算是把数据给清除了
  f_mkfs函数的使用方法可以在http://www.elm-chan.org/fsw/ff/doc/mkfs.html上查看
FIL fil;
FRESULT res;
UINT bww;
char buf[100];

int main(void)
{ 	
 	u32 total,free;
	u8 t=0;	
	u8 res=0;	
	
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置系统中断优先级分组2
	delay_init(168);  //初始化延时函数
	uart_init(115200);		//初始化串口波特率为115200
	LED_Init();					//初始化LED 
	usmart_dev.init(84);		//初始化USMART
// 	LCD_Init();					//LCD初始化  
 	KEY_Init();					//按键初始化 
	W25QXX_Init();				//初始化W25Q128
	my_mem_init(SRAMIN);		//初始化内部内存池 
	my_mem_init(SRAMCCM);		//初始化CCM内存池

 	while(SD_Init())//检测坏絊D卡
	{
	//	LCD_ShowString(30,150,200,16,16,"SD Card Error!");
		printf("SD Card Error!\r\n");
		delay_ms(500);					
	//	LCD_ShowString(30,150,200,16,16,"Please Check! ");
		printf("Please Check!\r\n");
		delay_ms(500);
		LED0=!LED0;//DS0闪烁
	}
		
 	exfuns_init();							//为fatfs相关变量申请内存				 
  	f_mount(fs[0],"0:",1); 					//挂载SD卡 
	
 	res=f_mount(fs[1],"1:",1); 				//挂载FLASH.	
	
	if(res==0X0D)//FLASH磁盘,FAT文件系统错误,重新格式化FLASH
	{
		//LCD_ShowString(30,150,200,16,16,"Flash Disk Formatting...");	//格式化FLASH
		printf("Flash Disk Formatting...\r\n");
		
		res=f_mkfs("1:",1,4096);//格式化FLASH,1,盘符;1,不需要引导区,8个扇区为1个簇
		if(res==0)
		{
			f_setlabel((const TCHAR *)"1:ALIENTEK");	//设置Flash磁盘的名字为:ALIENTEK
			//LCD_ShowString(30,150,200,16,16,"Flash Disk Format Finish");	//格式化完成
			printf("Flash Disk Format Finish\r\n");
			
		}
		else 
		{
			printf("Flash Disk Format Error\r\n");
		}
		
		delay_ms(1000);
	}													    
			
		printf("1");
		res = f_mkfs(0,0,4096);  //格式化SD卡
	while(exf_getfree("0",&total,&free))	//得到SD卡的总容量和剩余容量
	{
		u8 res2;
		res2=exf_getfree("0",&total,&free);
		//LCD_ShowString(30,150,200,16,16,"SD Card Fatfs Error!");
		printf("SD Card Fatfs Error!,返回值: %d\r\n",res2);
		res = f_mkfs(0, 1, 4096);
		printf("SD Card Fatfs !,返回值: %d\r\n",res);
		delay_ms(200);
	//	LCD_Fill(30,150,240,150+16,WHITE);	//清除显示		
	  printf("WHITE!\r\n");
		delay_ms(200);
		LED0=!LED0;//DS0闪烁
	}													  			    		
	printf("FATFS OK!\r\n");
		
}
 只需要res = f_mkfs(0,0,4096); 这一句代码,就可以将SD卡格式化,当然基本的SD卡初始化和挂载SD卡还是需要的。
 如有需要代码的可以私信博主,免费提供
  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值