STM32F405 unable to f_open.
Hi,
I have created my own board on STM32F405RGT6 and I want to use MicroSD card with it.
My problem is that the file system mount is working but not the file open. While f_open() is invoked the board freezes for about 30 seconds and later returns "FR_DISK_ERR" (1).
SDIO is configured to 4-bit wide bus with DMA (rx, tx) requests.
FATFS has enabled DMA template.
So far I tried 4GB and 16GB with cluster sizes 1024B and 4096B set in MAX_SS and MIN_SS properties of FATFS.
Card detection works and initializations seems also but I am unable to open any file on the SD card.
Here is my code written in STM32CubeIDE:
int main(void)
{
FRESULT fres;
char log_path[] = "/TEST.TXT";
char buf[20];
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_DMA_Init();
MX_SDIO_SD_Init();
MX_FATFS_Init();
MX_USB_DEVICE_Init();
while (1)
{
HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_SET);
sprintf(buf, "hello!");
fres = AppendToFile(log_path, strlen(log_path), buf, strlen(buf));
HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);
if (fres != FR_OK)
{
println("not FR_OK");
}
HAL_Delay(1000);
}
FRESULT AppendToFile(char* path, size_t path_len, char* msg, size_t msg_len)
{
FATFS fs;
FIL myFile;
UINT testByte;
FRESULT stat;
if ((path[path_len] != 0) || (msg[msg_len] != 0))
{
println("Invalid name!");
return FR_INVALID_NAME;
}
if (BSP_SD_Init() != MSD_OK)
{
println("BSP init fatal!");
return FR_NOT_READY;
}
if (FATFS_UnLinkDriver(SDPath) != 0)
{
println("unlink failiure!");
return FR_NOT_READY;
}
if (FATFS_LinkDriver(&SD_Driver, SDPath) != 0)
{
println("link failure");
return FR_NOT_READY;
}
stat = f_mount(&fs, SDPath, 0);
if (stat != FR_OK)
{
println("mount fail");
f_mount(0, SDPath, 0);
}
println("f_open start");
// ##### code freezes here \/ ######
stat = f_open(&myFile, path, FA_WRITE | FA_OPEN_APPEND);
println("f_open end");
if (stat != FR_OK) // ##### << stat = FR_DISK_ERR ####
{
println("open fail");
f_mount(0, SDPath, 0);
}
stat = f_write(&myFile, msg, msg_len, &testByte);
if (stat != FR_OK)
{
println("write failiure");
f_mount(0, SDPath, 0);
}
stat = f_close(&myFile);
f_mount(0, SDPath, 0);
return stat;
}