cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F405 unable to f_open.

DLegu.1
Associate II

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

1 ACCEPTED SOLUTION

Accepted Solutions
DLegu.1
Associate II

-_- :face_with_steam_from_nose: After many days of debugging with ST-Link I have found that the code waits for DMA, but does not receive any data. With DMA disabled SDIO_FLAG_RXOVERR error occurs.

I decided not to mess with that, downgrading the IDE did the job...... now I have to work on 1.0.1 :tired_face:

View solution in original post

4 REPLIES 4
PMath.4
Senior III

You are not specifying forced mount so the "mount" is only creating the file system object and not touching the disk

Use;

stat = f_mount(&fs, SDPath, 1);

and you will probably see this is failing

"If forced mounting is not specified (opt = 0), this function always succeeds regardless of the physical drive status. It only clears (de-initializes) the given work area and registers its address to the internal table and no activity of the physical drive in this function. The volume mount process will be attempted on subsequent file/directroy function if the filesystem object is not initialized. (delayed mounting) The volume mount processes, initialize the corresponding physical drive, find the FAT volume in it and then initialize the work area, is performed in the subsequent file/directory functions when either of following conditions is true."

Yes, it's failing now.

During the first AppendToFile() invoke BSP_init(), UnLinkDriver() and LinkDriver() are successful but later breaks on f_mount() returning FR_DISK_ERR.

During the another ones fails right at BSP_init() returning MSD_ERROR.

Do you think both cards might be incompatibile? Does that happen often? I have checked all the connections and everything seems to be ok.

FR_DISK_ERR suggests the DISKIO layer is failing due to some issue reported by the SDIO/SDMMC code. Looking at the top level code will not lead to resolution.

Thoroughly test the DISKIO layer, and instrument failure conditions so you can trace/understand the issue.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
DLegu.1
Associate II

-_- :face_with_steam_from_nose: After many days of debugging with ST-Link I have found that the code waits for DMA, but does not receive any data. With DMA disabled SDIO_FLAG_RXOVERR error occurs.

I decided not to mess with that, downgrading the IDE did the job...... now I have to work on 1.0.1 :tired_face: