cancel
Showing results for 
Search instead for 
Did you mean: 

f_open returns FR_OK but doesn't create a file

WellLeithed
Associate II

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.

1 ACCEPTED SOLUTION

Accepted Solutions
WellLeithed
Associate II

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.

View solution in original post

10 REPLIES 10
Andrew Neil
Evangelist III

How do you determine that nothing is created?

WellLeithed
Associate II

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

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?

SHs
ST Employee

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?

 

 

Please close this topic by clicking on “Accept as solution" button if it fully answered your question.

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.

SHs
ST Employee

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

 

 

Please close this topic by clicking on “Accept as solution" button if it fully answered your question.

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. 

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

I was able to use return successfully from FATFS_Link Driver by copying:

Disk_drvTypeDef disk = {{0},{0},{0},0};

within FATFS_LinkDriverEx. However, having returned successfully from this function, I'm no longer able to mount. I'm unable to debug beyond:

stat = disk.drv[pdrv]->disk_initialize(disk.lun[pdrv]);

in diskio.c with pdrv = 0