cancel
Showing results for 
Search instead for 
Did you mean: 

issue in opening two file in fatfs stmcube

shefu
Associate II

Hello,

I have been trying to open two files simultaneously in sdcard (one in READ mode and another one in Write mode) using the STMCUBE IDE fatfs library. Since two files requires two file handles so I have defined below mentioned parameters in the fatfs.h file.

In the ffconf.h file I have defined the FF_VOLUME to 2 since I have two drive one is sdcard and other one is USB.

#define MAX_FILES 2 // Define the maximum number of file handles you need

FIL fileHandle[MAX_FILES];
FILINFO finfo;
FRESULT fres, fres1; // result
UINT ptrr, ptrw; // File read/write count

UINT ubr, ubw; // File read/write count for USB

When I open the fileHandle[0] it open successfully and fres is set as FR_OK. when fileHandle[1] is opened it reset the parameters of the fileHandle[0] (specifically obj.id and obj.fs.id is mismatched) and return FR_OK as response. When I try to read from the file1 using fileHandle[0] , it sends the FR_INVALID_OBJECT Errror which is due to filehandle[0] parameter corruption. The debugging window screenshot is attached.

shefu_0-1715236162842.png

 

Why the fileHandle[0] gets corrupted when anotherfile is opened. Please suggest the solution of this problem. I am stuck here.

The code is attached for reference purpose.

3 REPLIES 3
SofLit
ST Employee

Hello,

I'm wondering why you're calling MX_USB_HOST_Process() in two different locations?:

Here:

/* USER CODE BEGIN 0 */
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
	// Check which version of the timer triggered this callback and toggle LED
	  if (htim == &htim2 )
	  {
		 // HAL_GPIO_TogglePin(LED1_GPIO_Port, LED1_Pin);
		  //HAL_GPIO_WritePin(LED1_GPIO_Port, LED1_Pin, SET);
		  MX_USB_HOST_Process();
		 // DebounceSwitch1(&keystate, &kp);
	  }
}

and here:

  while (1)
  {
    /* USER CODE END WHILE */
    MX_USB_HOST_Process();

    /* USER CODE BEGIN 3 */

  }

 

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.

Hello,

As you can see in the main code system_init function is called and it has while loop so while loop in the main function calling MX_USB_HOST_Process() will be never called. I have removed MX_USB_HOST_Process function calling in the main code also but the same issue is observed.


system_init();


/* USER CODE END 2 */

/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
MX_USB_HOST_Process();

/* USER CODE BEGIN 3 */

}

 

void system_init()
{
int i=0;
long file_size=0, rem=0, count=0;
char fl=0, buffer[101];
FILINFO finfo;

UINT ptrr, ptrw; // File read/write count
FIL fileHandle[2];
//********************** FAT system format *********************************
FIL USBHFile; /* File object for USBH */

FILINFO USBHfno;
FRESULT fresult; // result
UINT ubr, ubw; // File read/write count for USB


GLCD_Initialize();
GLCD_ClearScreen();

SdCard_Init();
USB_Keyboard_Init();
fres = f_chdrive((const TCHAR*)&USERPath);
fres = f_chdir("\\");

for(i=0;i<101;i++)
buffer[i] = '\0';

fres = f_open(&fileHandle[0], "HEADER.txt", FA_READ);
if(fres == FR_OK){
fres = f_open(&fileHandle[1], "TEMP.txt", FA_WRITE| FA_OPEN_ALWAYS);
if(fres == FR_OK){
fres = f_lseek(&fileHandle[0],0);
fres = f_read(&fileHandle[0],buffer, 100, &ptrr);
fres = f_write(&fileHandle[1],buffer, 100, &ptrw);
fres = f_close(&fileHandle[1]);
}
fres = f_close(&fileHandle[0]);
}

while(1)  // blocking in the systm init
{

}
}

Hello,

I have removed the USB drive from the fatfs library using the cube mx and only sdcard (user defined)  is enabled in the cubemx fatfs middleware library.

With this I am able to open the two file simultaneously as in the below code but when read operation is performed in the file 1 then handle of the 2nd file gets corrupted.

I have tried all the combinations but not able to identify the solution why the file handle is being corrupted?

fres = f_mount(&USERFatFS, "0:/", 1); //1=mount now
// fres = f_chdrive("1:/");
// fres = f_chdir("\\");

for(i=0;i<101;i++)
buffer[i] = '\0';

fres = f_open(&fileHandle[0], "0:/HEADER.txt", FA_READ);
if(fres == FR_OK){
fres = f_open(&fileHandle[1], "0:/TEMP.txt", FA_WRITE| FA_OPEN_ALWAYS);
if(fres == FR_OK){
fres = f_lseek(&fileHandle[0],0);
fres = f_read(&fileHandle[0],buffer, 100, &ptrr);
fres = f_write(&fileHandle[1],buffer, 100, &ptrw);
fres = f_close(&fileHandle[1]);
}
fres = f_close(&fileHandle[0]);
}

1: fileHandle[0] after open:

shefu_0-1715403243079.png

2: fileHandle[1] after open:

shefu_1-1715403289766.png

3: fileHandle[1] after reading from fileHandle[0]:

shefu_4-1715403343905.png

Please help to find out the issue. I am stuck at this point.