Skip to main content
Graduate
July 16, 2024
Solved

f_open returns FR_OK but doesn't create a file

  • July 16, 2024
  • 5 replies
  • 3356 views

I'm simply trying to do:

res = f_open(&fil, "file.txt", FA_CREATE_ALWAYS | FA_WRITE);

but nothing is created despite the return message. Could it be my SD card? I'm able to read from it in 4B mode.

Any suggestions would be gratefully received.

    This topic has been closed for replies.
    Best answer by WellLeithed

    It turns out that it was as simple as enabling hardware flow control in the MX. I had been unable to get it to mount without the clock divider being 4, but I imagined that may have been creating timing issues as I see the recommended value is 0. May this have caused underrun issues that hardware flow control can prevent?

    Thanks for all your willingness to help.

    5 replies

    Super User
    July 16, 2024

    How do you determine that nothing is created?

    Graduate
    July 16, 2024

    f_stat actually says that the file is created but when I open the SD on my desktop, nothing is visible.

    Super User
    July 17, 2024

    some sort of "caching" issue?

    Are you sure to close the file? I don't think FatFs (necessarily) writes immediately to the card without a close or flush?

    ST Employee
    July 17, 2024

    Hello @WellLeithed ,

    Could you kindly refer to the example provided in our reference firmware package, FatFs_uSD_Standalone, and examine the differences? Or could you please provide the model of the STM32 MCU you are utilizing so that I can attempt to reproduce on my side?

     

     

    Graduate
    July 17, 2024

    Hi, SH.

    I'm using the L476RG.

    I've just done:

    res = f_mount(&FatFs, "", 1);
    if (res != FR_OK)
    return EXIT_FAILURE;
     
    res = f_open(&fil, "file.txt", FA_CREATE_ALWAYS | FA_WRITE);
    if (res != FR_OK)
    return EXIT_FAILURE;
     
    f_write(&fil, "SD card write test\r\n", 512, &count);
    if (res != FR_OK)
    return EXIT_FAILURE;

    res = f_close(&fil);
    if (res != FR_OK)
    return EXIT_FAILURE;

    While it have been able to read from a uSD, when I open, write and then try to close, open and write return ok but the close returns " fr_disk_err".

    I see that FatFs_uSD_Standalone uses f_mkfs and I'm trying to work out if I need to apply this too.

    Super User
    July 18, 2024
    ST Employee
    July 18, 2024

    Hello @WellLeithed ,

    Could you kindly test this code snippet and confirm whether it functions correctly on your end?

     /*##-1- Link the micro SD disk I/O driver ##################################*/
     if(FATFS_LinkDriver(&SD_Driver, SDPath) == 0)
     {
     /*##-2- Register the file system object to the FatFs module ##############*/
     if(f_mount(&SDFatFs, (TCHAR const*)SDPath, 0) != FR_OK)
     {
     /* FatFs Initialization Error */
     Error_Handler();
     }
     else
     {
     /*##-3- Create a FAT file system (format) on the logical drive #########*/
     /* WARNING: Formatting the uSD card will delete all content on the device */
     if(f_mkfs((TCHAR const*)SDPath, FM_ANY, 0, buffer, sizeof(buffer)) != FR_OK)
     {
     /* FatFs Format Error */
     Error_Handler();
     }
     else
     {
     /*##-4- Create and Open a new text file object with write access #####*/
     if(f_open(&MyFile, "STM32.TXT", FA_CREATE_ALWAYS | FA_WRITE) != FR_OK)
     {
     /* 'STM32.TXT' file Open for write Error */
     Error_Handler();
     }
     else
     {
     /*##-5- Write data to the text file ################################*/
     res = f_write(&MyFile, wtext, sizeof(wtext), (void *)&byteswritten);
     
     if((byteswritten == 0) || (res != FR_OK))
     {
     /* 'STM32.TXT' file Write or EOF Error */
     Error_Handler();
     }
     else
     {
     /*##-6- Close the open text file #################################*/
     f_close(&MyFile);
     
     /*##-7- Open the text file object with read access ###############*/
     if(f_open(&MyFile, "STM32.TXT", FA_READ) != FR_OK)
     {
     /* 'STM32.TXT' file Open for read Error */
     Error_Handler();
     }
     else
     {
     /*##-8- Read data from the text file ###########################*/
     res = f_read(&MyFile, rtext, sizeof(rtext), (UINT*)&bytesread);
     
     if((bytesread == 0) || (res != FR_OK))
     {
     /* 'STM32.TXT' file Read or EOF Error */
     Error_Handler();
     }
     else
     {
     /*##-9- Close the open text file #############################*/
     f_close(&MyFile);
     
     /*##-10- Compare read data with the expected data ############*/
     if((bytesread != byteswritten))
     { 
     /* Read data is different from the expected data */
     Error_Handler();
     }
     else
     {
     /* Success of the demo: no error occurrence */
     
     }
     }
     }
     }
     }
     }
     }
     }

     

    BRs,

    Sarra

     

     

    Graduate II
    July 18, 2024

    No reason to use f_mkfs() unless the file system is trashed. Cards come properly formatted. 

    Instrument DISKIO to understand interaction,  failure and success. Validate your read functionality first, then write. Ensure data written is subsequently read back properly. 

    WellLeithedAuthorAnswer
    Graduate
    July 18, 2024

    It turns out that it was as simple as enabling hardware flow control in the MX. I had been unable to get it to mount without the clock divider being 4, but I imagined that may have been creating timing issues as I see the recommended value is 0. May this have caused underrun issues that hardware flow control can prevent?

    Thanks for all your willingness to help.