Using f_mount to read and write data to text file

5k Views Asked by At

In my Application I need to open, read and write data to a text file using the calls f_open, f_read, and f_write.

It is failing to open the .txt file

res = f_open(&f_header.file, file_path, FA_OPEN_EXISTING | FA_WRITE | FA__WRITTEN | FA_READ | FA_CREATE_NEW  );
                printf("res value after f open %d \n\r",res);
if (res != FR_OK) {
                printf("Failed to open %s, error %d\n\r", file_path, res);
    }

This is giving error:

FR_NOT_ENABLED, /* (12) The volume has no work area */

For solving this error application program needs to perform f_mount function after each media change to force cleared the filesystem object.

How to use f_mount() call in this application to solve this issue? I'm not clear about the 2nd parameter.

I added this f_mount(&fs0, "0://", 1); to solve this issue.

Before the f_open call. It is not taking f_mount() call also.

res=f_mount(&fs0,"0://", 1);

res = f_open(&f_header.file, file_path, FA_OPEN_EXISTING | FA_WRITE | FA__WRITTEN | FA_READ | FA_CREATE_NEW  );

The code is stopping while run time before the f_mount()

Here is the source code for f_mount which I'm using:

FRESULT f_mount (
    FATFS* fs,          /* Pointer to the file system object (NULL:unmount)*/
    const TCHAR* path,  /* Logical drive number to be mounted/unmounted */
    BYTE opt            /* 0:Do not mount (delayed mount), 1:Mount immediately */
)
{
    FATFS *cfs;
    int vol;
    FRESULT res;
    const TCHAR *rp = path;
    vol = get_ldnumber(&rp);
    if (vol < 0) return FR_INVALID_DRIVE;
    cfs = FatFs[vol];                   /* Pointer to fs object */
    if (cfs) {
#if _FS_LOCK
        clear_lock(cfs);
#endif
#if _FS_REENTRANT                       /* Discard sync object of the current volume */
        if (!ff_del_syncobj(cfs->sobj)) return FR_INT_ERR;
#endif
        cfs->fs_type = 0;               /* Clear old fs object */
    }
    if (fs) {
        fs->fs_type = 0;                /* Clear new fs object */
#if _FS_REENTRANT                       /* Create sync object for the new volume */
        if (!ff_cre_syncobj((BYTE)vol, &fs->sobj)) return FR_INT_ERR;
#endif
    }
    FatFs[vol] = fs;            /* Register new fs object */
    if (!fs || opt != 1) return FR_OK;  /* Do not mount now, it will be mounted later */
    res = find_volume(&fs, &path, 0);   /* Force mounted the volume */
    LEAVE_FF(fs, res);
}
  • The code is not showing any error/warnings at the time of make file. I'm sure there is no problem with the code.
  • There is nothing wrong with the code. Is this some problem related to the memory allocation or out of memory in emmc. What are the possible reason for this behaviour.
1

There are 1 best solutions below

7
On BEST ANSWER

According to http://elm-chan.org/fsw/ff/doc/mount.html:

FRESULT f_mount (
  FATFS*       fs,    /* [IN] Filesystem object */
  const TCHAR* path,  /* [IN] Logical drive number */
  BYTE         opt    /* [IN] Initialization option */
);

Parameters
  fs
    Pointer to the filesystem object to be registered and cleared. Null pointer unregisters the registered filesystem object.
  path
    Pointer to the null-terminated string that specifies the logical drive. The string without drive number means the default drive.
  opt
    Mounting option. 0: Do not mount now (to be mounted on the first access to the volume), 1: Force mounted the volume to check if it is ready to work.

In other words, the second parameter is how you want to refer to this particular filesystem when later working with it.

For example, mounting it like so:

f_mount(&fs0, "0://", 1);

you would then be able to open files like this:

f_open(fp, "0://path/to/file", FA_CREATE_ALWAYS);