1

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.
VLAZ
  • 26,331
  • 9
  • 49
  • 67
yagami
  • 149
  • 2
  • 12

1 Answers1

2

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);
J_S
  • 2,985
  • 3
  • 15
  • 38
  • I'm storing this files on mmc. Which path i need to give as second parameter? what is the meaning of the second parameter? – yagami Oct 06 '18 at 05:03
  • It doesn't matter. Make it `0:/` for example. It's a logical drive number that you pick. It's the same as `C:/` or `D:/` drive letters in Windows which you can also freely rename. – J_S Oct 06 '18 at 08:24
  • I tried the resolution you suggested. It is not taking the call f_mount. – yagami Oct 06 '18 at 09:17
  • What do you mean "it's not taking the call"? I'd suggest stepping through the FatFS code and see what exactly is wrong. There are comments inside further clarifying what may be wrong when certain error is set. – J_S Oct 06 '18 at 09:20
  • The call to `f_mount` looks correct. Are you sure sure that your disk I/O implementation actually works properly? Personally I'd recommend reading through the documentation (http://elm-chan.org/fsw/ff/00index_e.html). It describes everything we're discussing here, including examples and explanations what certain error codes in certain situations mean. – J_S Oct 06 '18 at 09:41
  • i'm not clear with the " disk I/O implementation" where i need to check this? – yagami Oct 06 '18 at 11:22
  • It's described in the documentation I've linked. You need to provide functions that allow FatFS to access your mmc storage – J_S Oct 06 '18 at 12:00