Skip to main content
Associate II
May 9, 2024
Question

issue in opening two file in fatfs stmcube

  • May 9, 2024
  • 2 replies
  • 1901 views

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.

    2 replies

    mƎALLEm
    Technical Moderator
    May 9, 2024

    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."
    shefuAuthor
    Associate II
    May 10, 2024

    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
    {

    }
    }

    Visitor II
    June 19, 2025

    Hello , i have the same problem , still didn't get an answer to that ?