FATFS无法初始化SD卡 [英] FATFS can't initialize the SD card
问题描述
因此,我正在使用STM32F407,并尝试使用FATFS写入SD卡.我已经使用CubeMX为FAT层和4位SDIO配置软件生成代码.我在main中添加了以下代码,以测试该代码是否有效.
So I'm using an STM32F407 and trying to write to an SD card with FATFS. I have used the CubeMX to generate the code for both the FAT layer and 4bit SDIO configuration software. I have added the following code to main to test whether the code works.
UINT bw;
f_mount(&FatFs, "", 0); /* Give a work area to the default drive */
/* Create a file */
if(f_open(&Fil, "newfile.txt", FA_WRITE | FA_CREATE_ALWAYS) == FR_OK) {
f_write(&Fil, "It works!\r\n", 11, &bw); /* Write data to the file */
f_close(&Fil); /* Close the file */
if (bw == 11) { /* Lights green LED if data written well */
//Celebrate
}
}
但是我无法初始化SD卡.当我单步执行代码时,将进入磁盘初始化函数,该函数将返回错误.由于我缺乏编写C的经验,因此我不确定以下功能如何工作以及如何调用其他代码来初始化卡,因为我认为卡需要配置为4位模式.
However I can't initialize the SD card. When I step through the code I get to the disk initialization function which returns an error. Due to my lack of experience writing C I'm not sure how to the following function works and how it calls other pieces of code to initialize the card as I assume the card needs to be configured for 4 bit mode.
/**
* @brief Initializes a Drive
* @param pdrv: Physical drive number (0..)
* @retval DSTATUS: Operation status
*/
DSTATUS disk_initialize (
BYTE pdrv /* Physical drive nmuber to identify the drive */
)
{
DSTATUS stat = RES_OK;
if(disk.is_initialized[pdrv] == 0){
disk.is_initialized[pdrv] = 1;
stat = disk.drv[pdrv]->disk_initialize(disk.lun[pdrv]);
}
return stat;
}
在这一点上,由于从未使用过FAT,所以我很固执.我在适配器中使用了2GB的微型SD卡,并已完全格式化为FAT32.我正在使用STM32F4发现板.我已经彻底检查了SD卡与开发板之间的连接.我可以在逻辑分析仪上看到,当调用"disk_initialize"函数时,确实可以进行通信,但是据我所知,这是一个小问题,没有任何东西被发送回微控制器.有人可以建议我寻找什么来帮助我吗?
At this point due to having never got FAT working I'm a but stuck. I'm using a 2GB micro SD card in an adapter and formatted fully with FAT32. I'm using an STM32F4 discovery board. I have checked the connections from the SD card to the board thoroughly. I can see on my logic analyzer that I do get communication when the 'disk_initialize' function is called but as far as I can tell it's one nible and nothing being sent back to the micro. Could someone help me by suggesting what to look for?
谢谢
更新
好的,现在很难看到出了什么问题.如果希望立即安装驱动器或不立即安装驱动器,则在"disk_initialize"处出现错误.逐步执行时,到达以下行:
Okay so now it's hard to see what's going wrong. If want the drive to be mounted immediately or not then I get the error at 'disk_initialize'. When stepping through I get to the line:
stat = disk.drv[pdrv]->disk_initialize(disk.lun[pdrv]);
然后介绍一下它如何立即跳转到:
Then some how it immediately it jumps to:
if((response_r1 & SD_OCR_ILLEGAL_CMD) == SD_OCR_ILLEGAL_CMD)
{
return(SD_ILLEGAL_CMD);
}
然后返回此错误.
我会继续寻找,但会有所帮助.
I'll keep looking but any help is appreciated.
推荐答案
我在自己的主板上也遇到了同样的问题.在将SDIO硬件的CubeMX中的"SDIOCLK时钟分频因子"参数从0设置为4之后,我就可以使用它.
I had the same issue with my own board. I got it to work after seting the "SDIOCLK clock divide factor" parameter in CubeMX from 0 to 4 for the SDIO hardware.
这篇关于FATFS无法初始化SD卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!