f_mount
The f_mount fucntion registers/unregisters filesystem object to the FatFs module.
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.
Return Values
FR_OK, FR_INVALID_DRIVE, FR_DISK_ERR, FR_NOT_READY, FR_NOT_ENABLED, FR_NO_FILESYSTEM
Description
FatFs needs work area (filesystem object) for each logical drives (FAT volumes). Prior to perform file/directory operations, a filesystem object needs to be registered with f_mount function to the logical drive. The file/directory API functions get ready to work after this procedure. If there is any open object of file or directory on the logical drive, the object will be invalidated by this function.
The f_mount function registers/unregisters a filesystem object to the FatFs module as follows:
- Determines the logical drive which specified by path.
- Clears and unregisters the regsitered work area of the volume if exist.
- Clears and registers the new work area to the volume if fs is not NULL.
- Performs volume mount process to the volume if forced mounting is specified.
If forced mounting is not specified (opt = 0), this function always succeeds regardless of the physical drive status. It only clears (de-initializes) the given work area and registers its address to the internal table and no activity of the physical drive in this function. To unregister the work area, specify a NULL to the fs, and then the work area can be discarded. The volume mount processes, initialize the corresponding physical drive, find the FAT volume in it and then initialize the work area, is performed in the subsequent file/directory functions when either of following conditions is true.
- Filesystem object has not been initialized. It is de-initialized by f_mount function.
- Physical drive is not initialized. It is de-initialized by system reset or media removal.
If the function with forced mounting (opt = 1) failed, it means that the filesystem object has been registered successfully but the volume is currently not ready to work. The volume mount process will be attempted at subsequent file/directroy functions if the filesystem object is not initialized. (delayed mounting)
If implementation of the disk I/O layer lacks asynchronous media change detection, application program needs to perform f_mount function after each media change to force cleared the filesystem object.
QuickInfo
Always available.
Example
int main (void) { FATFS *fs; /* Ponter to the filesystem object */ fs = malloc(sizeof (FATFS)); /* Get work area for the volume */ f_mount(fs, "", 0); /* Mount the default drive */ f_open(... /* Here any file API can be used */ ... f_mount(fs, "", 0); /* Re-mount the default drive to reinitialize the filesystem */ ... f_mount(0, "", 0); /* Unmount the default drive */ free(fs); /* Here the work area can be discarded */ ... }
要特别的注意以下两个地方:
1. opt参数: Initialization option. 0: Do not mount now (to be mounted later), 1: Forcemounted the volume to check if the volume is available
简单的说就是:如果设置opt参数为0,那么会在后面才执行加载操作,也就是说不管你是否外挂了设备,调用f_mount后,都会返回成功的。
如果为1那么就会强制的进行加载操作。
2. 使用这个函数还要注意返回值的含义,在程序调试时很有意义:
FR_OK, FR_INVALID_DRIVE, FR_DISK_ERR, FR_NOT_READY, FR_NO_FILESYSTEM
和以前的版本不同,这里要注意调用方法:
/* 挂载文件系统 */
result = f_mount(&fs, "1:/", 0); /* Mount a logical drive */
/* 卸载文件系统 */
result = f_mount(NULL, "1:/", 0)
static const char * FR_Table[]=
{
"FR_OK:成功", /* (0) Succeeded */
"FR_DISK_ERR:底层硬件错误", /* (1) A hard error occurred in the low level disk I/O layer */
"FR_INT_ERR:断言失败", /* (2) Assertion failed */
"FR_NOT_READY:物理驱动没有工作", /* (3) The physical drive cannot work */
"FR_NO_FILE:文件不存在", /* (4) Could not find the file */
"FR_NO_PATH:路径不存在", /* (5) Could not find the path */
"FR_INVALID_NAME:无效文件名", /* (6) The path name format is invalid */
"FR_DENIED:由于禁止访问或者目录已满访问被拒绝", /* (7) Access denied due to prohibited access or directory full */
"FR_EXIST:由于访问被禁止访问被拒绝", /* (8) Access denied due to prohibited access */
"FR_INVALID_OBJECT:文件或者目录对象无效", /* (9) The file/directory object is invalid */
"FR_WRITE_PROTECTED:物理驱动被写保护", /* (10) The physical drive is write protected */
"FR_INVALID_DRIVE:逻辑驱动号无效", /* (11) The logical drive number is invalid */
"FR_NOT_ENABLED:卷中无工作区", /* (12) The volume has no work area */
"FR_NO_FILESYSTEM:没有有效的FAT卷", /* (13) There is no valid FAT volume */
"FR_MKFS_ABORTED:由于参数错误f_mkfs()被终止", /* (14) The f_mkfs() aborted due to any parameter error */
"FR_TIMEOUT:在规定的时间内无法获得访问卷的许可", /* (15) Could not get a grant to access the volume within defined period */
"FR_LOCKED:由于文件共享策略操作被拒绝", /* (16) The operation is rejected according to the file sharing policy */
"FR_NOT_ENOUGH_CORE:无法分配长文件名工作区", /* (17) LFN working buffer could not be allocated */
"FR_TOO_MANY_OPEN_FILES:当前打开的文件数大于_FS_SHARE", /* (18) Number of open files > _FS_SHARE */
"FR_INVALID_PARAMETER:参数无效" /* (19) Given parameter is invalid */
};
测试函数**************************************************************************************************************************
void Test_f_mount(void)
{
FRESULT result;
FATFS fs;
/* 挂载文件系统 */
// result = f_mount(&fs, "", 1); /* Mount a logical drive */
result = f_mount(&fs, "1:", 1); /* Mount a logical drive */
if (result != FR_OK)
{
printf("挂载文件系统失败 (%s)\r\n", FR_Table[result]);
}
else
{
printf("挂载文件系统成功 (%s)\r\n", FR_Table[result]);
}
/* 卸载文件系统 */
result = f_mount(NULL, "1:", 1);
if (result != FR_OK)
{
printf("卸载文件系统失败 (%s)\r\n", FR_Table[result]);
}
else
{
printf("卸载文件系统成功 (%s)\r\n", FR_Table[result]);
}
}
在fatfs外挂一个设备的时候,通过 f_mount(&fs, "", 1);就可以访问,而在多个设备时,就得指定磁盘号
支持以下两种方式:
f_mount(&fs, "1:", 1);
f_mount(&fs, "1:/", 1);
这种方式不支持:
f_mount(&fs, "1", 1);