无法在 STM32 上使用 FatFS 挂载 SD [英] Cannot mount SD using FatFS on STM32
问题描述
我正在尝试使用 STM32F405 芯片写入 MicroSD 卡.
引脚连接正确,MicroSD 卡插槽上的每个引脚都可以使用 HAL_GPIO_WritePin 写入.(用示波器测量)我正在使用 CubeMX 为 TrueStudio 生成初始化代码,所以希望那里也一切正常.但是当我运行以下代码时,f_mount 返回 FR_DISK_ERR
.MicroSD 卡可以写入和读取.如果我使用不同的设备编号,即1:",我会得到 FR_INVALID_DRIVE
I'm trying to write to a MicroSD-Card using STM32F405 chip.
The pins are connected correctly and each pin on the MicroSD-Card slot can be written to by using HAL_GPIO_WritePin. (Messured with ossciloscope) I'm using CubeMX to generate the init code for TrueStudio, so hopefully everything is ok there as well. But when I run the following code, f_mount returns FR_DISK_ERR
. The MicroSD-Card can be written to and read from. If I use a different Device number, i.e. "1:", I get FR_INVALID_DRIVE
所以我的问题是:除了有故障的 MicroSD 卡之外,什么会导致 FR_DISK_ERR
?
So my question is: what could cause FR_DISK_ERR
except a faulty MicroSD-Card?
这是我目前的代码:
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration----------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* Configure the system clock */
SystemClock_Config();
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_SDIO_SD_Init();
MX_FATFS_Init();
/* USER CODE BEGIN WHILE */
FATFS fileSystem;
FIL testFile;
uint8_t testBuffer[16] = "SD write success";
UINT testBytes;
FRESULT res;
while((res = f_mount(&fileSystem, SD_MOUNT_PATH, 1)) != FR_OK){
printf("%d", res); //used to debug res, only for TrueStudio Debugger
}
uint8_t path[13] = "testfile.txt";
path[12] = '\0';
res = f_open(&testFile, (char*)path, FA_WRITE | FA_CREATE_ALWAYS);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_6, GPIO_PIN_RESET);
res = f_write(&testFile, testBuffer, 16, &testBytes);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_7, GPIO_PIN_RESET);
res = f_close(&testFile);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_RESET);
}
在MX_FATFS_Init()
FATFS_LinkDriver(&SD_Driver, SD_Path)
中被调用并返回0.
in MX_FATFS_Init()
FATFS_LinkDriver(&SD_Driver, SD_Path)
is called and returns 0.
推荐答案
如果没有 SD_MOUNT_PATH 宏,f_mount 调用是否如下所示:f_mount(&fileSystem, "0:", 1)
?'
Without the SD_MOUNT_PATH macro, does the f_mount call look like this:
f_mount(&fileSystem, "0:", 1)
?'
你是说f_open、f_write即使f_mount失败也返回FR_OK?!
Are you saying that f_open, f_write return FR_OK even if f_mount fails?!
FR_DISK_ERR 通常表示 disk_read() 或 disk_write() 失败.尝试在使用 f_mount() 之前以及在 fatfs 函数调用之间提供 100 毫秒或 1 秒的延迟.
FR_DISK_ERR usually means disk_read() or disk_write() failed. Try giving 100ms or 1 sec delay before using f_mount() and between between fatfs function calls.
这篇关于无法在 STM32 上使用 FatFS 挂载 SD的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!